Amazon web services 如何在CloudInformation中为区域AWS API网关获取自定义域的目标域名?
我正在尝试在AWS上创建一个多区域无服务器应用程序。我已经按照指示做了。我使用的是无服务器框架,它使用Cloudformation脚本在AWS上创建所有资源 我想为API网关创建一个自定义域作为区域端点。当它创建一个区域端点时,它会生成一个目标域。我想知道如何在Cloudformation脚本中获得目标域的值 当我创建一个边缘优化端点时,我通过使用Amazon web services 如何在CloudInformation中为区域AWS API网关获取自定义域的目标域名?,amazon-web-services,aws-api-gateway,amazon-cloudformation,serverless-framework,regional,Amazon Web Services,Aws Api Gateway,Amazon Cloudformation,Serverless Framework,Regional,我正在尝试在AWS上创建一个多区域无服务器应用程序。我已经按照指示做了。我使用的是无服务器框架,它使用Cloudformation脚本在AWS上创建所有资源 我想为API网关创建一个自定义域作为区域端点。当它创建一个区域端点时,它会生成一个目标域。我想知道如何在Cloudformation脚本中获得目标域的值 当我创建一个边缘优化端点时,我通过使用DistributionDomainName属性获得CloudFront部署的值。但在创建区域端点时,我看不到目标域名的任何属性。我尝试对区域端点使用
DistributionDomainName
属性获得CloudFront部署的值。但在创建区域端点时,我看不到目标域名的任何属性。我尝试对区域端点使用DistributionDomainName
属性,但它抛出了一个错误,表示没有DistributionDomainName
下面是我脚本的一部分-
# 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]
请帮忙。谢谢
更新
Cloudformation现在通过
RegionalDomainName
属性返回区域域名。它可以用作Fn:GetAtt:[customDomain,RegionalDomainName]
我在云计算中遇到了同样的问题。当我读到更多关于它的内容时,根据我的研究,到目前为止,区域端点没有支持的返回值
您可以使用它使用Python脚本读取区域端点,并使用AWS Lambda创建自定义资源,这样您的自动化不会因为返回值不可用而中断
client=boto3.client('apigateway'))
response=client.get_domain_name(domainName='api.example.com')print(response['regionalDomainName']))
目前这是不可能的
正如您所提到的,唯一公开的参数是,这仅适用于边缘优化的端点
作为一种解决方法(直到它在CloudFormation中实现为止),您可以使用一个由自己的Lambda函数备份的函数来返回该属性
下面是一个示例CloudFormation YAML代码:
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]}
区域API网关rest API的域基于API id和区域:{restapi id}.execute API.{region}.amazonaws.com请参阅RegionalDomainName