AWS 证书管理器构建库
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
AWS 证书管理器( ACM) 处理 SSL/ TLS X. 509 公有和私有证书和密钥的创建、 存储和更新的复杂性 保护您的 AWS 网站和应用程序。 ACM 证书可以安全单个域名,多个特定域名,通配符域,或 这些组合。 ACM通配符证书可以保护无限数量的子域.
此软件包为提供和引用可用于CloudFront和ELB的ACM证书提供了构造.
在请求证书后,您需要证明您拥有该证书 域名后,证书才会被授予。 云阵 部署将等待核查进程完成.
由于等待时间,当使用人工验证方法时,更好 将您的证书与主目录分开提供 服务,或手动提供并导入到您的 CDK 应用程序中.
说明: 对一年之内可在某一账户和区域申请的ACM证书总数有限制. 默认的限额为2000年,但在新的AWS账户上,这个限额可能(很多)较低. 见 https://docs.aws.amazon.com/acm/latest/userguide/acm-limits.html 页面存档备份,存于互联网档案馆 更多信息.
DNS 验证
DNS验证是验证域名的首选方法,因为它比电子邮件验证具有一些优势. 另见 用 DNS 验证 在AWS证书管理器用户指南中.
如果 Amazon Route 53 是您所请求域的 DNS 提供者, DNS 记录可以是 自动创建 :
const myHostedZone = new route53.HostedZone(this, 'HostedZone', {
zoneName: 'example.com',
});
new acm.Certificate(this, 'Certificate', {
domainName: 'hello.example.com',
validation: acm.CertificateValidation.fromDns(myHostedZone),
});
如果路线53不是您的 DNS 提供者, DNS 记录必须手动添加, 堆栈将无法完成 创建直到添加记录.
new acm.Certificate(this, 'Certificate', {
domainName: 'hello.example.com',
validation: acm.CertificateValidation.fromDns(), // Records must be added manually
});
当与多个域合作时,使用 CertificateValidation.fromDnsMultiZone()数字 :
const exampleCom = new route53.HostedZone(this, 'ExampleCom', {
zoneName: 'example.com',
});
const exampleNet = new route53.HostedZone(this, 'ExampleNet', {
zoneName: 'example.net',
});
const cert = new acm.Certificate(this, 'Certificate', {
domainName: 'test.example.com',
subjectAlternativeNames: ['cool.example.com', 'test.example.net'],
validation: acm.CertificateValidation.fromDnsMultiZone({
'test.example.com': exampleCom,
'cool.example.com': exampleCom,
'test.example.net': exampleNet,
}),
});
电子邮件验证
电子邮件验证证书( 默认) 通过接收一个 多个预定义域之一的电子邮件并遵循指令 在电子邮件中.
见 使用电子邮件验证 在AWS证书管理器用户指南中.
new acm.Certificate(this, 'Certificate', {
domainName: 'hello.example.com',
validation: acm.CertificateValidation.fromEmail(), // Optional, this is the default
});
跨区域证书
使用 CloudFront 的ACM 证书 -- -- 或依赖 CloudFront 的更高层次的构造 -- -- 必须在 us-east-1 区域.
这个 DnsValidatedCertificate 构造可以促进创建这些证书。 此资源只能用于
线路53基于DNS验证.
declare const myHostedZone: route53.HostedZone;
new acm.DnsValidatedCertificate(this, 'CrossRegionCertificate', {
domainName: 'hello.example.com',
hostedZone: myHostedZone,
region: 'us-east-1',
});
请求私人证书
AWS 证书管理器可以创建 私人证书 印发的文件清单 私人证书管理局(PCA)无需验证私人证书.
import * as acmpca from '@aws-cdk/aws-acmpca';
new acm.PrivateCertificate(this, 'PrivateCertificate', {
domainName: 'test.example.com',
subjectAlternativeNames: ['cool.example.com', 'test.example.net'], // optional
certificateAuthority: acmpca.CertificateAuthority.fromCertificateAuthorityArn(this, 'CA',
'arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/023077d8-2bfa-4eb0-8f22-05c96deade77'),
});
导入
如果要导入已有的证书,可以从其 ARN 中导入:
const arn = 'arn:aws:...';
const certificate = acm.Certificate.fromCertificateArn(this, 'Certificate', arn);
堆栈之间的共享
在同一 CDK 应用程序的堆栈中共享证书
通过 Certificate 堆栈之间的对象.
计量
这个 DaysToExpiry 测量值可通过 metricDaysToExpiry 方法
所有证书。 此度量由 AWS 证书管理器每一次发布
直到证书有效过期.
可以创建一个提醒来确定证书是否很快到期 更新下列代码:
import * as cloudwatch from '@aws-cdk/aws-cloudwatch';
declare const myHostedZone: route53.HostedZone;
const certificate = new acm.Certificate(this, 'Certificate', {
domainName: 'hello.example.com',
validation: acm.CertificateValidation.fromDns(myHostedZone),
});
certificate.metricDaysToExpiry().createAlarm(this, 'Alarm', {
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD,
evaluationPeriods: 1,
threshold: 45, // Automatic rotation happens between 60 and 45 days before expiry
});