I'm trying to create a multi-region serverless application on AWS. I've followed the instructions given here. I'm using Serverless framework, which uses Cloudformation scripts for creating all the resources on AWS.
I want to create a custom domain for API gateway as a Regional Endpoint. When it creates a Regional endpoint, it generates a target domain. I would like to know how can I get the value of the target domain in my Cloudformation script?
When I create an Edge optimized Endpoint, I get the value of the CloudFront deployment by using the DistributionDomainName
attribute. But I don't see any attribute for the target domain name when a Regional Endpoint is created. I tried using the DistributionDomainName
attribute for a Regional endpoint, but it throws an error which says that there is no DistributionDomainName
.
Below is a part of my script -
# Creates a custom domain for the ApiGateway
customDomain:
Type: 'AWS::ApiGateway::DomainName'
Properties:
DomainName: ${self:custom.domain}
EndpointConfiguration:
Types:
- REGIONAL
RegionalCertificateArn: ${self:custom.certificateArn}
# Insert a DNS record in route53 hosted zone to redirect from the custom domain to CF distribution
dnsRecord:
Type: AWS::Route53::RecordSet
Properties:
Region: ${self:provider.region}
SetIdentifier: ${self:provider.region}
HostedZoneId: ${self:custom.hostedZoneId}
Name: ${self:custom.domain}
Type: CNAME
TTL: 60
ResourceRecords:
- "Fn::GetAtt": [customDomain, DistributionDomainName]
Please help. Thanks!
UPDATE
Cloudformation now returns the regional domain name through RegionalDomainName
property. It could be used as Fn:GetAtt : [customDomain, RegionalDomainName]
.
Request or import an SSL/TLS certificate Before creating a custom domain name for your API, you must do one of the following: Request an SSL/TLS certificate from AWS Certificate Manager (ACM). Import an SSL/TLS certificate into ACM. Note: For more information, see Getting certificates ready in AWS Certificate Manager.
Custom domain names are simpler and more intuitive URLs that you can provide to your API users. After deploying your API, you (and your customers) can invoke the API using the default base URL of the following format: https:// api-id .execute-api. region .amazonaws.com/ stage.
Steps to create API Gateway Custom Domain using CloudFormation? 1 Create certificate for your domain 2 Create Custom Domain name 3 Create Route53 record to map API gateway cutom domain name with your url 4 Create a BasePathMapping for each microservice you want to map with this domain More ...
Attach the NameCheap domain with the AWS API Gateway Back to the interesting part. Go to “Custom Domain Names” section of AWS API Gateway and click “Create Custom Domain Name”. Enter the NameCheap domain name along with any subdomain name you want in the field “Domain Name”.
You can use a custom domain name to provide a URL that's more intuitive and easier to recall. For more information about using custom domain names, see Set up Custom Domain Name for an API in API Gateway in the API Gateway Developer Guide . To declare this entity in your AWS CloudFormation template, use the following syntax:
When you create a custom domain name for a regional API, API Gateway creates a regional domain name for the API. You must set up a DNS record to map the custom domain name to the regional domain name for API requests bound for the custom domain name to be routed to API Gateway through the mapped regional API endpoint.
This is not possible at the moment.
As you mentioned, the only exposed parameter is DistributionDomainName and this works only for edge-optimized endpoints.
As a workaround (until it will be implemented in CloudFormation) you could use a CustomResource backed up by your own Lambda function to return the regionalDomainName attribute.
Here's a sample CloudFormation YAML code that does this:
Resources:
# The workaround Lambda that returns the regionalDomainName property
RegionalDomainLambda:
Type: AWS::Lambda::Function
Properties:
Runtime: python2.7
Handler: index.handler
Role:
'Fn::GetAtt': [YOUR_ROLE_GOES_HERE, Arn] # make sure you include apigateway:GET
Timeout: 50
Code:
ZipFile: |
import cfnresponse
import json
import boto3
client = boto3.client('apigateway')
def handler(event, context):
response_data = {}
try:
domainName = event['ResourceProperties']['DomainName']
regional_domain_name = client.get_domain_name(domainName=domainName)['regionalDomainName']
response_data['value'] = regional_domain_name
cfnresponse.send(event, context, cfnresponse.SUCCESS,response_data, "RegionalDomainNameString")
except Exception as e:
response_data['exception'] = e
cfnresponse.send(event, context, cfnresponse.FAILED, response_data, "RegionalDomainNameString")
# The resource that serves as a placeholder
RegionalDomain:
Type: Custom::CustomResource
Properties:
ServiceToken:
'Fn::GetAtt': [RegionalDomainLambda, Arn]
DomainName: {Ref: YOUR_API_GATEWAY_DOMAIN_NAME_GOES_HERE}
# And here's how to use it
SomeOtherResource:
SomeOtherProperty: {'Fn::GetAtt': [RegionalDomain, value]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With