3

我一直在尝试在 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 Certificate Manager) 中生成了 3 个证书。-> 这是错误的,它应该只产生一个
  2. CDK 似乎不会自动添加 CNAME 记录,所以我尝试在 CloudFormation 正在进行时手动添加它。但是,这也不起作用。

在此处输入图像描述

重要的是要补充一点,所有 6 条生成的 CNAME 记录都是相同的,因此在 Route53 的托管区域配置中始终只需要一条 CNAME 记录(我设置了,但似乎没有区别)

4

1 回答 1

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 工作人员

于 2021-03-04T17:29:52.047 回答