AWS CDK Python - 使用 Route53 的证书管理器 DNS 验证

发布时间:2021-03-04 14:01

我一直在尝试使用 Python 中的 CDK 设置 DNS 证书验证。

我的代码如下:

class ApiService(core.Construct):
  def __init__(self, scope: core.Construct, id: str, env: str) -> None:
# set up hosted zone for existing Domain in Route53
    hosted_zone = aws_route53.HostedZone(self, "devHostedZone", zone_name="example.com")
# Create validation from DNS with hosted zone
    cert_validation = CertificateValidation.from_dns(hosted_zone)
    subj_alt_names = ['example.com', '*.example.com']
# DNS Validated certificate in zone, for domain and alternate names in specified region (for edge enabled APIs)
    cert_dns_val = DnsValidatedCertificate(
      self,
      'DnsValidation',
      hosted_zone=hosted_zone,
      domain_name='example.com',
      subject_alternative_names=subj_alt_names,
      region='us-east-1', 
      validation=cert_validation)
# Set up the gateway with domain name settings
    api = apigateway.RestApi(
      self, 
      "My-api", 
      rest_api_name="My API", 
      description="A Lambda that contains the REST API for My API.", 
      domain_name=apigateway.DomainNameOptions(certificate=cert_dns_val, domain_name=env+".example.com")
      )
# Finally create A Record to route incoming requests internally to the API Gateway that was just created
    target = aws_route53.RecordTarget.from_alias(alias.ApiGateway(api))
    record = ARecord(self, 'ARecord', target=target, zone=hosted_zone, record_name=env+".example.com")

我似乎无法理解的问题是

  1. 我如何通过 CDK 告诉 AWS 设置所需的 DNS CNAME 记录以验证证书和
  2. 如何将其默认设置为 TLS 1.2(不是 TLS 1.0)

我看到的问题:

  1. 在控制台的 ACM(AWS 证书管理器)中生成了 3 个证书。 -> 这是错误的,它应该只产生一个
  2. CDK 似乎不会自动添加 CNAME 记录,因此我尝试在 CloudFormation 正在进行时手动添加它。但是,这也不起作用。

enter image description here

重要的是要添加所有 6 个生成的 CNAME 记录都是相同的,因此它始终只需要在 Route53 中托管区域配置中的单个 CNAME 记录(我设置了,但似乎没有什么区别)

回答1

我设法找到了一种方法,以便只生成一个证书。

代替:

# Create validation from DNS with hosted zone
cert_validation = CertificateValidation.from_dns(hosted_zone)
subj_alt_names = ['example.com', '*.example.com']
# DNS Validated certificate in zone, for domain and alternate names in specified region (for edge enabled APIs)
cert_dns_val = DnsValidatedCertificate(
  self,
  'DnsValidation',
  hosted_zone=hosted_zone,
  domain_name='example.com',
  subject_alternative_names=subj_alt_names,
  region='us-east-1', 
  validation=cert_validation)

我写的

cert_dns_val = DnsValidatedCertificate(
  self,
  'DnsValidation',
  hosted_zone=hosted_zone,
  domain_name='*.example.com',
  region='us-east-1')

我不知道或不明白为什么会这样,但无论如何都会接受。

第二个问题是超时。我在以下链接中找到了一些解释为什么会发生这种情况

似乎 AWS CDK 正在使用 Lambda 来运行 CloudFormation。不幸的是,Lambda 的最长存活时间为 9 分 30 秒。然而,DNS 记录可能需要更长的时间,有时会更长(最多 30 分钟)。我不确定如何解决这个问题,但我可能需要创建单独的堆栈(中间有一些等待),因为这会指示单独的 Lambda 工作人员

aws-acm 相关推荐