AWS DevOps Blog

Passing Parameters to CloudFormation Stacks with the AWS CLI and Powershell

by Evan Brown | on | in How-To* | Permalink | Comments |  Share

In this guest post, AWS Solution Architect Grace Mollison discusses options for passing stack parameters when using the AWS CLI or AWS Tools for PowerShell.

AWS has two relatively new Command line tools, including the Python-based AWS Command Line Interface and the AWS Tools for Windows PowerShell

In this short post I’ll describe how you can pass parameters when creating or updating stacks using these two command line tools.

Using the AWS CLI

You can pass parameters to the create-stack or update-stack command via the --parameters argument. When passed this way, parameters are a list of key-value pairs separated by a space. For example, passing your SSH key and a valid instance type using the AWS CLI might look like:

aws cloudformation create-stack  --stack-name startmyinstance  
    --template-body file://home/ec2-user/templates/startmyinstance.json 
    --parameters  ParameterKey=KeyPairName,ParameterValue=MyKey ParameterKey=InstanceType,ParameterValue=t1.micro

You can also pass parameters to the create-stack or update-stack commands by saving the params as a JSON document in either a local or remote (i.e., S3) file. This is useful in a number of scenarios, including when you have many parameters to pass; if you want to share launch params across a team; if you want to version contorl launch params; etc.

An example JSON document using the same parameters as the example above is:

[
  {
    "ParameterKey": "KeyPairName",
    "ParameterValue": "MyKey"
  }, 
  {
    "ParameterKey": "InstanceType",
    "ParameterValue": "m1.micro"
  }
]

Passing a parameters stored as JSON in a local file looks like:

aws cloudformation create-stack --stackname startmyinstance  
    --template-body file:///some/local/path/templates/startmyinstance.json 
    --parameters file:///some/local/path/params/startmyinstance-parameters.json

If those parameters were stored remotely in S3, the same command would be:

aws cloudformation create-stack --stackname startmyinstance  
    --template-body file:///some/local/path/templates/startmyinstance.json 
    --parameters https://your-bucket-name.s3.amazonaws.com/params/startmyinstance-parameters.json

Using the AWS Tools for PowerShell

When using the AWS Tools for PowerShell (available at http://aws.amazon.com/powershell/), the New-CFNStack cmdlet is used to launch new CloudFormation stacks. You can pass parameters as an array to the `New-CFNStack cmdlet:

# First parameter
$p1 = new-object Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "KeyPairName"
$p1.ParameterValue = "MyKey"

# Second parameter
$p2 = new-object Amazon.CloudFormation.Model.Parameter    
$p2.ParameterKey = "InstanceType"
$p2.ParameterValue = "m1.micro"

# Create
$stack = New-CFNStack -Stackname StartMyInstance `
-TemplateURL "https://your-bucket-name.s3.amazonaws.com/templates/startmyinstance.json" `
-Parameters @( $p1, $p2 )

Alternatively (and just like the AWS CLI), you can pass in a valid JSON document containing the parameters.

Helpful Links:

About CloudFormation Parameters

Using the AWS CLI with CloudFormation

AWS CLI’s aws create-stack ... Documentation

PowerShell’s New-CFNStack ... Documentation