LoginSignup
6
1

More than 3 years have passed since last update.

AWS SAMテンプレートでAPI Gatewayに独自ドメインを使う

Last updated at Posted at 2021-03-05

SAMテンプレートを使って、API Gateway + Lambda(Python3.8) で作成したAPIに、独自ドメインを使ってアクセスできるようにしたい、ということでカスタムドメインとパスマッピングの設定をしました。

公式ドキュメント
https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/sam-property-api-domainconfiguration.html

前提
- ドメインをRoute53で管理している(HostedZonIdがある)
- SSL証明書をACMで管理している(ARNを持っている)

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: custom domain api

Parameters:
  Stage:
    Type: String
    Default: Prod
  BasePath:
    Type: String
    Default: v1
  DomainName:
    Type: String
  HostedZoneId:
    Type: String
  CertificateArn:
    Type: String

Resources:
  MyApiRole:
    Type: AWS::IAM::Role
    Properties: # 省略

  GetConvertFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: functions/get_hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Role: !GetAtt MyApiRole.Arn
      Events:
        HttpGet:
          Type: Api
          Properties:
            Path: /hello
            Method: get
            RestApiId: !Ref MyApi
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref Stage
      Domain:
        DomainName: !Ref DomainName
        CertificateArn: !Ref CertificateArn
        EndpointConfiguration: EDGE
        Route53:
          HostedZoneId: !Ref HostedZoneId
        BasePath:
          - !Ref BasePath
6
1
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
6
1