Optional Parameter in CloudFormation
Sometimes you want a CloudFormation Parameter to be optional. Unfortunately a blank Parameter contains an empty string. If you pass this empty string to e.g. the
KeyName Property of an EC2 Instance or Launch Configuration you end up with a validation error.
Imagine the following CloudFormation template:
If you leave the
KeyName blank you will get the following error:
Hej, Andreas & Michael here!
We launched the cloudonaut blog in 2015. Since then, we have published 325 articles: small tips and tricks, best practices, and service reviews. We enjoy writing about all things AWS a lot.
Do you like our blog posts and podcast episodes? Have you learned something new? Consider supporting us create in-depth and independent AWS content. Please help us with a monthly or one-time payment through GitHub Sponsors.Start supporting us today!
Parameter validation failed: parameter value for parameter name
KeyName does not exist. Rollback requested by user.
The solution is the make use of CloudFormation
Conditions, the Condition Function
Fn::If and the Pseudo Parameter
Now you can leave the
KeyName parameter blank to not add a EC2 KeyPair to the