目次 †
概要 †カスタムドメインを使用するAPI Gateway のCloudFormationテンプレートのサンプル。 ドメインの取得 †任意のドメインレジストラ、もしくは Route53 でドメインを取得しておく。 CloudFormation テンプレート †template.yml AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
...
SampleCertificate:
Type: 'AWS::CertificateManager::Certificate'
Properties:
DomainName: example.mydomain.com
ValidationMethod: EMAIL
DomainName:
Type: 'AWS::ApiGateway::DomainName'
Properties:
CertificateArn: !Ref SampleCertificate
DomainName: example.mydomain.com
Mapping:
Type: 'AWS::ApiGateway::BasePathMapping'
Properties:
DomainName: !Ref DomainName
RestApiId: !Ref SampleApi
#BasePath: sample
SampleApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionBody:
swagger: 2.0
info:
title:
Ref: AWS::StackName
paths:
/:
get:
x-amazon-apigateway-integration:
httpMethod: POST # 統合リクエストで使用されるHTTP メソッド。Lambdaの場合はPOSTを指定
type: aws_proxy # Lambdaプロキシ統合
uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SampleFunction1.Arn}/invocations"
responses: {}
'/{id}':
get:
x-amazon-apigateway-integration:
httpMethod: POST
type: aws_proxy
uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SampleFunction1.Arn}/invocations"
responses: {}
# Lambda本体
SampleFunction1:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: SampleFunction1
Runtime: python3.6
Handler: index.handler
CodeUri: ./src
MemorySize: 128
Timeout: 60
Role: !GetAtt SampleFunction1Role.Arn
# Lambdaのロール
SampleFunction1Role:
Type: "AWS::IAM::Role"
Properties:
RoleName: SampleFunction1Role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "lambda.amazonaws.com"
Action: "sts:AssumeRole"
Policies:
- PolicyName: "SampleFunction1Policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
補足 †証明書検証が済むまでは、スタックは CREATE_IN_PROGRESS 状態のままとなる。 関連: 独自ドメイン名で API Gateway または EC2にアクセスする の 「ドメイン名の検証について」を参照。 |