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:
Please support our work!
We have published 327 articles, 41 podcast episodes, and 15 videos. It's all free and means a lot of work in our spare time.
Thanks to Alan Leech, Alex DeBrie, e9e4e5f0faef, Goran Opacic, jhoadley, Shawn Tolidano, Thorsten Hoeger, Todd Valentine, Vince Fulco, and all anonymous supporters for your help! We also want to thank all supporters who purchased a cloudonaut t-shirt. It gives us great pleasure to send our t-shirts all over the world.
With your help, we can continue to produce independent & high-quality content focused on AWS. Please support us!Support us
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