AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

API Gateway用のIAMロールをデプロイする手順です。ポリシーは適当にアタッチしています。

エクスポートを2つ指定しています。

template-api.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: API Gateway deploy
Parameters:
  Stage:
    Description: Stage name
    Type: String
    AllowedValues:
      - prod
      - dev
      - test
  ApiRoleName:
    Description: API Gateway role name
    Type: String
Resources:
  ApiGatewayRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: !Ref ApiRoleName
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - apigateway.amazonaws.com
          Action:
            - sts:AssumeRole
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
      Tags:
        - Key: Name
          Value: !Sub
            - ${Stage}${ApiRoleName}
            - { Stage: !Ref Stage,ApiRoleName: !Ref ApiRoleName}
        - Key: Stage
          Value: !Ref Stage
Outputs:
  ApiGatewayRoleID:
    Description: ApiGatewayRole ID
    Value: !Ref ApiGatewayRole
    Export:
      Name: !Sub
        - ${Stage}ApiGatewayRoleID
        - { Stage: !Ref Stage }
  ApiGatewayRoleARN:
    Description: ApiGatewayRole ARN
    Value: !GetAtt ApiGatewayRole.Arn
    Export:
      Name: !Sub
        - ${Stage}ApiGatewayRoleARN
        - { Stage: !Ref Stage }

aws cloudformation packageコマンドでテンプレートを吐き出します。

C:\tmp>aws cloudformation package --template-file template-api.yml --output-template-file template-api-out.yml --s3-bucket バケット名 --profile=default

Successfully packaged artifacts and wrote output template to file template-api-out.yml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file C:\tmp\template-api-out.yml --stack-name <YOUR STACK NAME>

template-api-out.ymlファイルが作成されていると思います。

このファイルをもとに、aws cloudformation deployコマンドを実行してapi-roleというstackを作成します。

C:\tmp>aws cloudformation deploy --template-file template-api-out.yml --stack-name api-role --region ap-northeast-1 --parameter-overrides Stage=dev ApiRoleName=ApiSampleRole --profile=default --capabilities CAPABILITY_NAMED_IAM

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - api-role

CloudFormationにapi-roleが作成されていることを確認します。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

IAMロールの確認

IAMロールがApiSampleRoleという名前で作成されているか確認します。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

テンプレートファイルで指定したポリシーがアタッチされているIAMロールが作成されていることが確認できます。

スタックのエクスポート名

スタックの出力ページの一番右にエクスポート名という欄があります。

このエクスポート名は、他のyamlで!ImportValue エクスポート名で指定することが出来ます。

例えばLambdaデプロイ時にRole: !ImportValue エクスポート名とすることが可能になります。

API Gatewayデプロイ

API Gatewayをデプロイするymlで!ImportValueでエクスポート値を参照します。

「ApiGatewayRoleARN」がエクスポートに指定されているとします。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

api.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Test
Resources:
  api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionUri: ./oai.yml
      OpenApiVersion: 3.0.1
  apigatewayrole:
    Type: AWS::ApiGateway::Account
    Properties: 
      CloudWatchRoleArn: !ImportValue ApiGatewayRoleARN

aws cloudformation packageコマンドを実行します。

aws cloudformation package \
  --template-file ./api.yml \
  --s3-bucket <バケット名> \
  --output-template-file api_out.yml

aws cloudformation deployコマンドを実行します。

aws cloudformation deploy \
  --template-file ./api_out.yml \
--stack-name <スタック名>

API Gatewayデプロイすると同時にRoleも設定されます。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

参考サイト

AWS::ApiGateway::Account - AWS CloudFormation
Use the AWS CloudFormation AWS::ApiGateway::Account resource for ApiGateway.

CloudWatch ログを有効化

API Gatewayデプロイ時に「CloudWatch ログを有効化」にチェックを入れます。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

api.yml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: Test
Resources:
  api:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionUri: ./oai.yml
      OpenApiVersion: 3.0.1
      MethodSettings:
        - DataTraceEnabled: true
          LoggingLevel: 'INFO'
          ResourcePath: '/*'
          HttpMethod: '*'
  iamrole:
    Type: AWS::ApiGateway::Account
    Properties: 
      CloudWatchRoleArn: !ImportValue ApiGatewayRoleARN

MethodSettings部分の5行を追加します。あとはpackageしてdeployするという流れです。

aws cloudformation packageコマンドを実行します。

aws cloudformation package \
  --template-file ./api.yml \
  --s3-bucket <バケット名> \
  --output-template-file api_out.yml

aws cloudformation deployコマンドを実行します。

aws cloudformation deploy \
  --template-file ./api_out.yml \
--stack-name <スタック名>

これで「CloudWatch ログを有効化」にチェックが入ります。

AWS SAM CloudFormationでAPI GatewayのIAMロールをデプロイする方法

参考サイト

https://github.com/aws/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessapi
SAM で API Gateway の CloudWatch Logs を有効にしたい - Qiita
この記事は、AWS Serverless Application Model (AWS SAM) で API Gateway の CloudWatch Logs を有効化しようとした際の記録です。…

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました