【AWS】CloudformationでAPI GatewayをHttps化
1. 使用するサービス
(AWS)
- Cloudformation
- Cloudfront
- Route53
2. 概要
CLoudformationでAPI GateawyのHttps化を行います。正確には作成済のAPI Gawewayに対してドメインを作成し、HTTPSで配信可能にします。その際、デプロイするAPIとそのステージをマッピングします。
3. 実装
Parameters: Project: Description: "Project Name" Default: "demo" Type: String Stage: Description: "Environment stage" Default: dev Type: String AllowedValues: [dev, staging, prod] APIDomainName: Description: "DomainName, format of which is api-Project-Stage" Default: api Type: String WebContentsBucketBaseName: Description: "WebContents S3 Bucket Name, fomat of which is Project-Stage" Default: webcontents Type: String HostedZoneName: Description: "DomainName" Default: example.com Type: String Resources: #API Gatewayのドメインを作成します。 #事前に作成済のドメインに合うようなドメインにします。 ApiGatewayDomainName: Type: 'AWS::ApiGateway::DomainName' Properties: #証明書のArn CertificateArn: !Sub "arn:aws:acm:us-east-1:${AWS::AccountId}:certificate/111111111111111111" DomainName: !Sub "${Project}-${Stage}-${APIDomainName}.${HostedZoneName}" #API Gatewayのマッピング ApiGatewayDomainMapping: Type: 'AWS::ApiGateway::BasePathMapping' Properties: DomainName: !Ref ApiGatewayDomainName Stage: !Ref Stage RestApiId: !Ref RestApi #ドメインレコードを登録して、名前解決出来るようにする Route53RecordSetForAPI: Type: 'AWS::Route53::RecordSetGroup' Properties: HostedZoneName: !Sub "${HostedZoneName}." RecordSets: - Name: !Sub "${Project}-${Stage}-${APIDomainName}.${HostedZoneName}." Type: A AliasTarget: HostedZoneId: Z2FDTNDATAQYW2 DNSName: !GetAtt ApiGatewayDomainName.DistributionDomainName