#!/usr/bin/env bash ################################################################################ # USAGE: # ./create-acm-certificate.sh [domain] # # DESCRIPTION: # This utility create and validates a certificate in ACM, as well as the # required DNS records for validation. # # The following requirements need to be met in order to use this utility: # - The domain's DNS must be hosted by Route 53. # - The account and region defined in `~/.aws/config` and defined in your # `AWS_PROFILE` environment variable (or lack thereof) will be assumed. ################################################################################ ################################################################################ # Requests a certificate based on the provided domain name from ACM. ################################################################################ ACM_CERTIFICATE_ARN=$(aws acm request-certificate \ --domain-name "$1" \ --subject-alternative-names "*.$1" \ --validation-method DNS \ --query CertificateArn \ --output text) echo "[ACM] Certificate ARN: $ACM_CERTIFICATE_ARN" ################################################################################ # The following commands extract the name and value of the required CNAME record # that needs to be created to confirm ownership of the domain the certificate # will be associated with. ################################################################################ VALIDATION_NAME="$(aws acm describe-certificate \ --certificate-arn "$ACM_CERTIFICATE_ARN" \ --query "Certificate.DomainValidationOptions[?DomainName=='$1'].ResourceRecord.Name" \ --output text)" VALIDATION_VALUE="$(aws acm describe-certificate \ --certificate-arn "$ACM_CERTIFICATE_ARN" \ --query "Certificate.DomainValidationOptions[?DomainName=='$1'].ResourceRecord.Value" \ --output text)" echo "[ACM] Certificate validation record: $VALIDATION_NAME CNAME $VALIDATION_VALUE" ################################################################################ # Request the hosted zone from Route 53 that is associated with the domain that # the validation CNAME record will be associated with. ################################################################################ R53_HOSTED_ZONE_ID="$(aws route53 list-hosted-zones-by-name \ --dns-name "$1" \ --query "HostedZones[?Name=='$1.'].Id" \ --output text)" R53_HOSTED_ZONE=${R53_HOSTED_ZONE_ID##*/} echo "[Route 53] Hosted Zone ID: $R53_HOSTED_ZONE" ################################################################################ # Create the change batch needed to upset the validation record, then run the # command to apply the change batch. ################################################################################ R53_CHANGE_BATCH=$(cat <