CloudFormation and cloudfront CNAME

0

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?

Thanks

Valerio

3 Answers
0

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 pictureAWS
EXPERT
kentrad
answered 2 years 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?

0

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?

LordVee
answered 2 years 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.

0

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 cf.abc.com
  • you have CloudFront distribution with URL
  • Now you can add cname to CloudFront: cf.abc.com
  • In DNS add CNAME record for cf.abc.com 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
MG
answered 2 years 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