serverless-multi-regions
Version:
Deploy an API Gateway service in multiple regions with a global CloudFront distribution and health checks
147 lines (145 loc) • 4.25 kB
YAML
Resources:
ApiGatewayStubDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: ${self:custom.gatewayMethodDependency}
Properties:
Description: Stub Gateway Deployment
RestApiId:
Ref: ApiGatewayRestApi
StageName: ${opt:stage}
ApiRegionalDomainName:
Type: AWS::ApiGateway::DomainName
Properties:
DomainName: ${self:custom.dns.regionalDomainName}
RegionalCertificateArn: ${self:custom.dns.${opt:region}.acmCertificateArn}
EndpointConfiguration:
Types:
- REGIONAL
ApiRegionalBasePathMapping:
Type: AWS::ApiGateway::BasePathMapping
DependsOn: ApiGatewayStubDeployment
Properties:
DomainName:
Ref: ApiRegionalDomainName
RestApiId:
Ref: ApiGatewayRestApi
Stage: ${opt:stage}
ApiRegionalHealthCheck:
Type: AWS::Route53::HealthCheck
DependsOn: ApiGatewayStubDeployment
Properties:
HealthCheckConfig:
Type: HTTPS
ResourcePath: /${opt:stage}/healthcheck
FullyQualifiedDomainName:
Fn::Join:
- ''
- - Ref: ApiGatewayRestApi
- '.execute-api.'
- Ref: AWS::Region
- '.amazonaws.com'
RequestInterval: 30
FailureThreshold: 3
Regions: [us-east-1, ap-southeast-1, eu-west-1]
ApiRegionalEndpointRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: ${self:custom.dns.hostedZoneId}
HostedZoneName: ${self:custom.dns.hostedZoneName}
Name:
Fn::Join:
- ''
- - Ref: ApiRegionalDomainName
- .
Region: ${opt:region}
SetIdentifier: ${opt:region}
HealthCheckId:
Ref: ApiRegionalHealthCheck
Type: A
AliasTarget:
HostedZoneId:
Fn::GetAtt:
- ApiRegionalDomainName
- RegionalHostedZoneId
DNSName:
Fn::GetAtt:
- ApiRegionalDomainName
- RegionalDomainName
ApiDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Comment: ${opt:stage}-${self:service} (${opt:region})
Origins:
- Id: ApiGateway
DomainName: ${self:custom.dns.regionalDomainName}
CustomOriginConfig:
HTTPSPort: 443
OriginProtocolPolicy: https-only
OriginSSLProtocols: [TLSv1.2]
Enabled: true
HttpVersion: http2
Aliases: ${self:custom.cdn.aliases}
PriceClass: ${self:custom.cdn.priceClass}
DefaultCacheBehavior:
TargetOriginId: ApiGateway
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
CachedMethods:
- HEAD
- GET
- OPTIONS
Compress: true
ForwardedValues:
QueryString: true
Headers: ${self:custom.cdn.headers}
Cookies:
Forward: all
MinTTL: 0
DefaultTTL: 0
ViewerProtocolPolicy: https-only
ViewerCertificate:
AcmCertificateArn: ${self:custom.cdn.acmCertificateArn}
SslSupportMethod: sni-only
MinimumProtocolVersion: 'TLSv1.2_2018'
Logging:
IncludeCookies: true
Bucket: ${self:custom.cdn.logging.bucket}
Prefix: ${self:custom.cdn.logging.prefix}
WebACLId: ${self:custom.cdn.webACLId}
ApiGlobalEndpointRecord:
Type: AWS::Route53::RecordSet
Properties:
HostedZoneId: ${self:custom.dns.hostedZoneId}
HostedZoneName: ${self:custom.dns.hostedZoneName}
Name: ${self:custom.dns.domainName}.
Type: A
AliasTarget:
HostedZoneId: Z2FDTNDATAQYW2
DNSName:
Fn::GetAtt:
- ApiDistribution
- DomainName
Outputs:
ApiDistribution:
Value:
Fn::GetAtt: [ApiDistribution, DomainName]
RegionalEndpoint:
Value:
Fn::Join:
- ''
- - https://
- Ref: ApiRegionalDomainName
GlobalEndpoint:
Value:
Fn::Join:
- ''
- - https://
- ${self:custom.dns.domainName}