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が作成されていることを確認します。
IAMロールの確認
IAMロールがApiSampleRoleという名前で作成されているか確認します。
テンプレートファイルで指定したポリシーがアタッチされているIAMロールが作成されていることが確認できます。
スタックのエクスポート名
スタックの出力ページの一番右にエクスポート名という欄があります。
このエクスポート名は、他のyamlで!ImportValue エクスポート名で指定することが出来ます。
例えばLambdaデプロイ時にRole: !ImportValue エクスポート名とすることが可能になります。
API Gatewayデプロイ
API Gatewayをデプロイするymlで!ImportValueでエクスポート値を参照します。
「ApiGatewayRoleARN」がエクスポートに指定されているとします。
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も設定されます。
参考サイト
CloudWatch ログを有効化
API Gatewayデプロイ時に「CloudWatch ログを有効化」にチェックを入れます。
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 ログを有効化」にチェックが入ります。
参考サイト


KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^






コメント