15 types
AWS Certificate Manager (ACM) handles the complexity of creating, storing, and renewing public and private SSL/TLS X.509 certificates and keys that protect your AWS websites and applications. ACM certificates can secure singular domain names, multiple specific domain names, wildcard domains, or combinations of these. ACM wildcard certificates can protect an unlimited number of subdomains.
This package provides Constructs for provisioning and referencing ACM certificates which can be used with CloudFront and ELB.
After requesting a certificate, you will need to prove that you own the domain in question before the certificate will be granted. The CloudFormation deployment will wait until this verification process has been completed.
Because of this wait time, when using manual validation methods, it's better to provision your certificates either in a separate stack from your main service, or provision them manually and import them into your CDK application.
Note: There is a limit on total number of ACM certificates that can be requested on an account and region within a year. The default limit is 2000, but this limit may be (much) lower on new AWS accounts. See https://docs.aws.amazon.com/acm/latest/userguide/acm-limits.html for more information.
DNS validation is the preferred method to validate domain ownership, as it has a number of advantages over email validation. See also Validate with DNS in the AWS Certificate Manager User Guide.
If Amazon Route 53 is your DNS provider for the requested domain, the DNS record can be created automatically:
my_hosted_zone = AWSCDK::Route53::HostedZone.new(self, "HostedZone", {
zone_name: "example.com",
})
AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "hello.example.com",
certificate_name: "Hello World Service",
# Optionally provide an certificate name
validation: AWSCDK::CertificateManager::CertificateValidation.from_dns(my_hosted_zone),
})
If Route 53 is not your DNS provider, the DNS records must be added manually and the stack will not complete creating until the records are added.
AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "hello.example.com",
validation: AWSCDK::CertificateManager::CertificateValidation.from_dns,
})
When working with multiple domains, use the CertificateValidation.fromDnsMultiZone():
example_com = AWSCDK::Route53::HostedZone.new(self, "ExampleCom", {
zone_name: "example.com",
})
example_net = AWSCDK::Route53::HostedZone.new(self, "ExampleNet", {
zone_name: "example.net",
})
cert = AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "test.example.com",
subject_alternative_names: ["cool.example.com", "test.example.net"],
validation: AWSCDK::CertificateManager::CertificateValidation.from_dns_multi_zone({
"test.example.com" => example_com,
"cool.example.com" => example_com,
"test.example.net" => example_net,
}),
})
Email-validated certificates (the default) are validated by receiving an email on one of a number of predefined domains and following the instructions in the email.
See Validate with Email in the AWS Certificate Manager User Guide.
AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "hello.example.com",
validation: AWSCDK::CertificateManager::CertificateValidation.from_email,
})
ACM certificates that are used with CloudFront -- or higher-level constructs which rely on CloudFront -- must be in the us-east-1 region.
CloudFormation allows you to create a Stack with a CloudFront distribution in any region. In order
to create an ACM certificate in us-east-1 and reference it in a CloudFront distribution is a
different region, it is recommended to perform a multi stack deployment.
Enable the Stack property cross_region_references
in order to access the cross stack/region certificate.
This feature is currently experimental
require 'aws-cdk-lib'
app = nil # AWSCDK::App
stack1 = AWSCDK::Stack.new(app, "Stack1", {
env: {
region: "us-east-1",
},
cross_region_references: true,
})
cert = AWSCDK::CertificateManager::Certificate.new(stack1, "Cert", {
domain_name: "*.example.com",
validation: AWSCDK::CertificateManager::CertificateValidation.from_dns(AWSCDK::Route53::PublicHostedZone.from_hosted_zone_id(stack1, "Zone", "ZONE_ID")),
})
stack2 = AWSCDK::Stack.new(app, "Stack2", {
env: {
region: "us-east-2",
},
cross_region_references: true,
})
AWSCDK::CloudFront::Distribution.new(stack2, "Distribution", {
default_behavior: {
origin: AWSCDK::CloudFrontOrigins::HttpOrigin.new("example.com"),
},
domain_names: ["dev.example.com"],
certificate: cert,
})
AWS Certificate Manager can create private certificates issued by Private Certificate Authority (PCA). Validation of private certificates is not necessary.
require 'aws-cdk-lib'
AWSCDK::CertificateManager::PrivateCertificate.new(self, "PrivateCertificate", {
domain_name: "test.example.com",
subject_alternative_names: ["cool.example.com", "test.example.net"],
# optional
certificate_authority: AWSCDK::ACMPCA::CertificateAuthority.(self, "CA", "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/023077d8-2bfa-4eb0-8f22-05c96deade77"),
key_algorithm: AWSCDK::CertificateManager::KeyAlgorithm.RSA_2048,
})
AWS Certificate Manager can issue an exportable public certificate. There is a charge at certificate issuance and again when the certificate renews. See opting out of certificate transparency logging for details.
AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "test.example.com",
allow_export: true,
})
Transparency logging can be opted out of for AWS Certificate Manager certificates. See opting out of certificate transparency logging for limits.
AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "test.example.com",
transparency_logging_enabled: false,
})
To specify the algorithm of the public and private key pair that your certificate uses to encrypt data use the key_algorithm property.
Algorithms supported for an ACM certificate request include:
RSA_2048EC_prime256v1EC_secp384r1AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "test.example.com",
key_algorithm: AWSCDK::CertificateManager::KeyAlgorithm.EC_PRIME256V1,
})
Visit Key algorithms for more details.
If you want to import an existing certificate, you can do so from its ARN:
arn = "arn:aws:..."
certificate = AWSCDK::CertificateManager::Certificate.from_certificate_arn(self, "Certificate", arn)
To share the certificate between stacks in the same CDK application, simply
pass the Certificate object between the stacks.
The DaysToExpiry metric is available via the metric_days_to_expiry method for
all certificates. This metric is emitted by AWS Certificates Manager once per
day until the certificate has effectively expired.
An alarm can be created to determine whether a certificate is soon due for renewal using the following code:
require 'aws-cdk-lib'
my_hosted_zone = nil # AWSCDK::Route53::HostedZone
certificate = AWSCDK::CertificateManager::Certificate.new(self, "Certificate", {
domain_name: "hello.example.com",
validation: AWSCDK::CertificateManager::CertificateValidation.from_dns(my_hosted_zone),
})
certificate.metric_days_to_expiry.create_alarm(self, "Alarm", {
comparison_operator: AWSCDK::CloudWatch::ComparisonOperator::LESS_THAN_THRESHOLD,
evaluation_periods: 1,
threshold: 45,
})