0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Serverless FrameworkでAPIキーとなるリソースIDをSSM Parameter Storeに保存する

Posted at

やりたいこと

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"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?