I ran into a situation where I had a number of resources in a CloudFormation stack that may already exist, or I might want to make creation of those objects optional.

To achieve this it turns out that CloudFormation actually has a nice facility, although it is a bit awkward.

In this particular case, I have a stack that creates a CloudFront distribution for a wildcard domain. The stack includes creating the certificate, but I realized that when I go to production with this, I will have an existing certificate that will be used, so there won’t be a need to create a new one.

So in my stack, I added a parameter for the ARN of an existing certificate which looks like:

Notice that I set the default to an empty string, which implies my default scenario, I won’t use an existing certificate, but instead create one.

Then I need a condition statement which I can use later in the stack.

What this says is give me a condition that will be true if the parameter of ACMARN is equal to an empty string. I named it CreateCertificate to make it descriptive to the action that this will have. So if there is a value in ACMARN, then CreateCertificate will be false.

With that I update my certificate creation in the Resources section to be optional.

The “Condition” will only create the certificate if the ARN was not set.

So finally I also need to put a condition in the distribution creation that will use the ARN from the parameter, OR the new certificate, which I use an Fn::If for.