By using AWS re:Post, you agree to the Terms of Use

CloudFormation and cloudfront CNAME


Greetings! I need to add a **cname **to a cloudfront distribution which serves s3 static files each time a CloudFormation stack is run. Now, I am stuck with this because apparently i need to add all the cnames in one go and cannot just add one (which means in my veiw - read the previous cname definitions and add the new one in the array).

Is this how it's supposed to work? How can i achieve this?



3 Answers

Assuming you are passing in the CNAME(s) in as a Parameter, you will need to pass all of them in as a comma-delimited string. Then in the Aliases section use split to create a list of strings.

"Parameters" : {
    "AliasList" : { "Type" : "String" }
"Distribution" : {
    "Type" : "AWS::CloudFront::Distribution",
    "Properties" : {
        "DistributionConfig" : {
            "Aliases" : { "Fn::Split" : [ "," , { "Ref" : "AliasList" } ] }

So, yes, you need to pass in the entire list of CNAMES to add one.

profile picture
answered 9 months ago
  • Cool, this is what i thought, but how would i get the current list of cnames from within CloudFormation? Each customer will execute a CF stack which will deploy the working environment, so each customer will have CNAME pointing to a s3 site which, based on the hostname, will address to the right backend.

    So i'd have to be able to create the array of the previous cnames to then add the new one. How do i achieve this from CF?


Okay, i did not explain myself right. I have a cloudfront distribution which is already set up. Now, every cloud formation run will create an environment, which, amongst other things, will have to edit the cloudfront by adding a CNAME. Apparently this does not work with AWS::CloudFront::Distribution or at least i don't know how to identify the id of the cloud front

How do i edit Cloudfront distribution list form cloudformation to be able to add a cname value?

This seems a good starting point:

CloudFrontDistribution: Type: AWS::CloudFront::Distribution Properties: DistributionConfig: !Split [',,', !Ref CloudFrontAliases]

but there doesn't seem a way to just edit it?

answered 9 months ago
  • Ok, I think I understand. One way to achieve this is to store the current list of Aliases in a Systems Parameter. Then, have a Parameter to the template that has a default referencing this Systems Parameter. Then have another template Parameter of the new Alias to append to the Distribution. You update the Systems Parameter parameter with the new list and also split this string on the Aliases line. You will have to use conditions incase a new Alias is not passed.


Why do you need to change it on each run?

Alternate Domain Names or CNAMEs are in CloudFront to let CloudFront know which domain name will be pointing to its URL.

In the cnames you can add multiple domains which you will use in your DNS to point to your CloudFront Distribution.

For example:

  • you have a subdomain
  • you have CloudFront distribution with URL
  • Now you can add cname to CloudFront:
  • In DNS add CNAME record for pointing to CloudFront distribution URL

If you want to use HTTPS then you need to also attach SSL certificate to your CloudFront distribution.

You can find more about it here.

profile picture
answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions