📘

SAMでAPIキーを作成してApiGatewayに設定するサンプル

2025/03/12に公開

備忘録です。
最小限のサンプルです。

以前、リソースポリシーを設定しました。
https://zenn.dev/osachi/articles/f5795f469cfee3

今回はAPIキーを設定する方法です。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sample
Globals:
  Api:
    OpenApiVersion: 3.0.3

  SampleApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        ApiKeyRequired: true
        UsagePlan:
          CreateUsagePlan: SHARED
          Description: "Usage plan for my API"
          Quota:
            Limit: 1000
            Period: MONTH
          Throttle:
            BurstLimit: 100
            RateLimit: 50

  SampleFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.lambda_handler
      Runtime: python3.9
      CodeUri: sample/function/
      Architectures:
      - x86_64
      Events:
        CatchAll:
          Type: Api
          Properties:
            Path: /sample
            Method: GET
            RestApiId: 
              Ref: SampleApi
            Auth:
              ApiKeyRequired: true

Discussion