この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
困っていた内容
CloudFormation(SAM) を使って、API Gateway の Websocket をデプロイしますが、CloudFormation が実行される際に API Gateway の自動デプロイが機能しておりません。 そのため、毎回マネージメントコンソール上で手動デプロイを実行しております。 CloudFormation が実行した際に、API Gateway の新しいバージョンとしてステージへ自動デプロイする方法を教えてほしいです。
使用したテンプレート
今回は、APIGateway の WebSocket を使った AWS の公式サンプルを利用しました。
どう対応すればいいの?
API Gateway の Websocket のデプロイ後のステージについては、AWS::ApiGatewayV2::Stage で管理されます。
公式のテンプレートでは、DeploymentId がプロパティとして設定されておりますが、DeploymentId を指定したままだと、API Gateway に 変更セットがあったが場合に、API Gateway の自体の変更は CloudFormation 側で検知しますが、ステージへの自動のデプロイは行いません。
Deployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn:
- ConnectRoute
- SendRoute
- DisconnectRoute
Properties:
ApiId: !Ref SimpleChatWebSocket
Stage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: Prod
Description: Prod Stage
DeploymentId: !Ref Deployment
ApiId: !Ref SimpleChatWebSocket
そのため、以下のように DeploymentId をコメントアウトして、ステージへの自動デプロイの設定値である AutoDeploy を true にすることで、スタックの更新 の際にテンプレートで予め指定したステージへ新しいバージョンとして自動でデプロイ実行してくれます。
Deployment:
Type: AWS::ApiGatewayV2::Deployment
DependsOn:
- ConnectRoute
- SendRoute
- DisconnectRoute
Properties:
ApiId: !Ref SimpleChatWebSocket
Stage:
Type: AWS::ApiGatewayV2::Stage
Properties:
StageName: Prod
Description: Prod Stage
# 下記コメントアウト
# DeploymentId: !Ref Deployment
ApiId: !Ref SimpleChatWebSocket
# 下記追加
AutoDeploy: true
参考資料
AutoDeploy - AWS::ApiGatewayV2::Stage
AutoDeploy Specifies whether updates to an API automatically trigger a new deployment. The default value is false.
DeployID - AWS::ApiGatewayV2::Stage
DeploymentId The deployment identifier for the API stage. Can't be updated if autoDeploy is enabled.