如何使用 AWS CDK 将域别名添加到现有 CloudFront 分配

问题描述 投票:0回答:2

我刚刚从 AWS 解决方案部署了 CloudFormation 解决方案。该解决方案包括新的 CloudFront 发行版。我的挑战是我想将自定义域

mysite.example.com
添加到
dxxxxxx.cloudfront.net
发行版中。我已经使用证书管理器创建了别名和证书。我的问题是如何向现有 CloudFront 添加新域。

我知道我们可以使用 Distribution.fromDistributionAttributes 导入现有的发行版。

例如

const distribution = cloudfront.Distribution.fromDistributionAttributes(this, 'ImportedDist', {
  domainName: 'd111111abcdef8.cloudfront.net',
  distributionId: '012345ABCDEF',
});

假设我已准备好使用别名域名和证书 ARN。

const domainName = 'mysite.example.com';
const certificateArn = 'arn:aws:acm:us-east-1: 123456789012:certificate/abcdefgh-1234-5678-9012-abcdefghujkl';

我该去哪里?

amazon-web-services ssl-certificate aws-cloudformation amazon-cloudfront aws-cdk
2个回答
2
投票

通过更新您的“AWS 解决方案”CDK 应用程序来添加您的域和证书。 CDK 应用程序设计为可修改和重新部署。

Distribution 构造 接受

certificate?:ICertificate
domainNames?: string[]
作为构造函数的 props。 实例还公开了一个
addBehavior(pathPattern, origin, behaviorOptions?)
,这看起来很方便。

如果应用程序正在生产中,请注意更新有时会导致资源替换或中断CloudFormation 文档 记录了每个服务属性的更新行为。在快乐的情况下,你会看到

Update requires: No interruption
。运行
cdk diff
命令预览更改 CloudFormation 将充分利用您的资源。

cloudfront.Distribution.fromDistributionAttributes
呢? 许多 CDK 类都有
static from...
方法 获取对现有 AWS 资源的引用。当应用程序之间共享资源时,这些方法很方便(甚至是必要的),但仅当您无法修改原始 CDK 构造时才应使用。


0
投票

您可以使用 CfnRecordSet 来完成,只需按照此操作即可 Cfn记录集 对于 Cloudfront 分发,默认托管区域 ID 为

Z2FDTNDATAQYW2
。然后你可以做这样的事情

route53.CfnRecordSet(
                            self,
                            "AliasRecordSet",
                            name=name,
                            type=record_type,
                            alias_target=route53.CfnRecordSet.AliasTargetProperty(
                                dns_name=dns_name,
                                hosted_zone_id="Z2FDTNDATAQYW2",
                            ),
                            comment=comment,
                            hosted_zone_id=hosted_zone_id,
                        )
© www.soinside.com 2019 - 2024. All rights reserved.