We launched the cloudonaut blog in 2015. Since then, we have published 390 articles, 91 podcast episodes, and 99 videos. Our weekly newsletter keeps you up-to-date. Subscribe now!.
Subscribe
Our weekly newsletter keeps you up-to-date. Subscribe now! It's all free.
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:
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Optional Parameters in CloudFormation: Problem", "Parameters": { "KeyName": { "Description": "Provides the name of the EC2 key pair", "Type": "String" } }, "Resources": { "LaunchConfiguration": { "Type": "AWS::AutoScaling::LaunchConfiguration", "Properties": { "KeyName": {"Ref": "KeyName"} [...] } } } }
If you leave the KeyName blank you will get the following error:
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 AWS::NoValue.
Now you can leave the KeyName parameter blank to not add a EC2 KeyPair to the LaunchConfiguration.
Michael Wittig
I’ve been building on AWS since 2012 together with my brother Andreas. We are sharing our insights into all things AWS on cloudonaut and have written the book AWS in Action. Besides that, we’re currently working on bucketAV, HyperEnv for GitHub Actions, and marbot.
Here are the contact options for feedback and questions.