简体   繁体   English

如何在Cloudformation中获取区域AWS API网关的自定义域的目标域名?

[英]How to get the target domain name of a custom domain for Regional AWS API Gateway in Cloudformation?

I'm trying to create a multi-region serverless application on AWS. 我正在尝试在AWS上创建一个多区域无服务器应用程序。 I've followed the instructions given here . 我按照这里给出的说明进行操作。 I'm using Serverless framework, which uses Cloudformation scripts for creating all the resources on AWS. 我正在使用无服务器框架,它使用Cloudformation脚本在AWS上创建所有资源。

I want to create a custom domain for API gateway as a Regional Endpoint. 我想为API网关创建一个自定义域作为区域端点。 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? 我想知道如何在Cloudformation脚本中获取目标域的值?

When I create an Edge optimized Endpoint, I get the value of the CloudFront deployment by using the DistributionDomainName attribute. 当我创建Edge优化端点时,我使用DistributionDomainName属性获取CloudFront部署的值。 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 . 我尝试使用区域端点的DistributionDomainName属性,但它会抛出一个错误,表明没有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 UPDATE

Cloudformation now returns the regional domain name through RegionalDomainName property. Cloudformation现在通过RegionalDomainName属性返回区域域名。 It could be used as Fn:GetAtt : [customDomain, RegionalDomainName] . 它可以用作Fn:GetAtt : [customDomain, RegionalDomainName]

This is not possible at the moment. 目前这是不可能的。

As you mentioned, the only exposed parameter is DistributionDomainName and this works only for edge-optimized endpoints. 如您所述,唯一公开的参数是DistributionDomainName ,这仅适用于边缘优化端点。

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. 作为一种解决方法(直到它将在CloudFormation中实现),您可以使用由您自己的Lambda函数备份的CustomResource来返回regionalDomainName属性。

Here's a sample CloudFormation YAML code that does this: 这是一个示例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]}

I am having the same Issue scripting it in the Cloud formation. 我在云编队中编写了相同的问题脚本。 When I read more about it There is no supported Return Value for the Regional Endpoint as of now from what I have researched. 当我阅读更多相关信息时,目前我所研究的区域端点没有支持的返回值。

You can use this to read the Regional Endpoint using Python Script create a Custom Resource using AWS Lambda so that your automation wont break due to the unavailability of the return value. 您可以使用此脚本使用Python脚本读取区域端点使用AWS Lambda创建自定义资源,以便您的自动化不会因返回值不可用而中断。

client = boto3.client('apigateway') response = client.get_domain_name( domainName='api.example.com' ) print(response['regionalDomainName'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM