やりたいこと
Serverless FrameworkでAPIをデプロイしますが、その際に生成される a1bcdef2gh
みたいなAPIキーはURLとして利用されていたり、APIを利用する際に必要になる場合があります。
この情報をServerless Frameworkで sls deploy
した際に自動でSSM Parameter Storeに保存する方法です。
対応
以下のServerless Frameworkの定義の通り、生成されたリソースIDを resources
の部分に記載したCloudformationでSSM Parameter Storeに保存すると Serverless Frameworkで完結します。
一度デプロイしてみて、論理コンポーネント名を確認して Value
のところで参照するとスムーズです。 (
!Ref ApiGatewayRestApi
の部分)
frameworkVersion: '3'
# ---
# 省略
# ---
functions:
api:
handler: app/api.handler
events:
- http:
path: /
method: ANY
- http:
path: /{proxy+}
method: ANY
resources:
Resources:
ApiKeyParameter:
Type: AWS::SSM::Parameter
Properties:
Name: "/api/${self:custom.stage}/api-key"
Type: String
Value: !Ref ApiGatewayRestApi
Description: "API Key"