Examples of CloudFormation stack operation commands for the AWS CLI and PowerShell
The following command line examples demonstrate how to perform individual CloudFormation actions with the AWS CLI and PowerShell. These examples include only the most commonly used actions. For a complete list, see cloudformation in the AWS CLI Command Reference.
The examples in this guide use the convention of a backslash (\) to indicate that a long command line continues on the next line.
Topics
Cancel a stack update
Use the cancel-update-stack command command to cancel a stack update. For more information, see Cancel a stack update.
- CLI
-
- AWS CLI
-
To cancel a stack update that is in progress
The following
cancel-update-stackcommand cancels a stack update on themyteststackstack:aws cloudformation cancel-update-stack --stack-namemyteststack-
For API details, see CancelUpdateStack
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Cancels an update on the specified stack.
Stop-CFNUpdateStack -StackName "myStack"-
For API details, see CancelUpdateStack in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Cancels an update on the specified stack.
Stop-CFNUpdateStack -StackName "myStack"-
For API details, see CancelUpdateStack in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Continue rolling back an update
Use the continue-update-rollback command to continue rolling back an update. For more information, see Continue rolling back an update.
- CLI
-
- AWS CLI
-
To retry an update rollback
The following
continue-update-rollbackexample resumes a rollback operation from a previously failed stack update.aws cloudformation continue-update-rollback \ --stack-namemy-stackThis command produces no output.
-
For API details, see ContinueUpdateRollback
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Continues rollback of the named stack, which should be in the state 'UPDATE_ROLLBACK_FAILED'. If the continued rollback is successful, the stack will enter state 'UPDATE_ROLLBACK_COMPLETE'.
Resume-CFNUpdateRollback -StackName "myStack"-
For API details, see ContinueUpdateRollback in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Continues rollback of the named stack, which should be in the state 'UPDATE_ROLLBACK_FAILED'. If the continued rollback is successful, the stack will enter state 'UPDATE_ROLLBACK_COMPLETE'.
Resume-CFNUpdateRollback -StackName "myStack"-
For API details, see ContinueUpdateRollback in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Create a stack
Use the create-stack command to create a stack. You must provide the stack name, the location of a valid template, and any input parameters. The parameter key names are case sensitive. If you mistype a parameter key name, CloudFormation doesn't create the stack and reports that the template doesn't contain that parameter.
The following examples show how to create a new stack with the specified name, template, and input parameters.
- CLI
-
- AWS CLI
-
To create an AWS CloudFormation stack
The following
create-stackscommand creates a stack with the namemyteststackusing thesampletemplate.jsontemplate:aws cloudformation create-stack --stack-namemyteststack--template-bodyfile://sampletemplate.json--parametersParameterKey=KeyPairName,ParameterValue=TestKeyParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2Output:
{ "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896" }For more information, see Stacks in the AWS CloudFormation User Guide.
-
For API details, see CreateStack
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Creates a new stack with the specified name. The template is parsed from the supplied content with customization parameters ('PK1' and 'PK2' represent the names of parameters declared in the template content, 'PV1' and 'PV2' represent the values for those parameters. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will not be rolled back.
New-CFNStack -StackName "myStack" ` -TemplateBody "{TEMPLATE CONTENT HERE}" ` -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) ` -DisableRollback $trueExample 2: Creates a new stack with the specified name. The template is parsed from the supplied content with customization parameters ('PK1' and 'PK2' represent the names of parameters declared in the template content, 'PV1' and 'PV2' represent the values for those parameters. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back.
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter $p1.ParameterKey = "PK1" $p1.ParameterValue = "PV1" $p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter $p2.ParameterKey = "PK2" $p2.ParameterValue = "PV2" New-CFNStack -StackName "myStack" ` -TemplateBody "{TEMPLATE CONTENT HERE}" ` -Parameter @( $p1, $p2 ) ` -OnFailure "ROLLBACK"Example 3: Creates a new stack with the specified name. The template is obtained from the Amazon S3 URL with customization parameters ('PK1' represents the name of a parameter declared in the template content, 'PV1' represents the value for the parameter. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back (same as specifying -DisableRollback $false).
New-CFNStack -StackName "myStack" ` -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template ` -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }Example 4: Creates a new stack with the specified name. The template is obtained from the Amazon S3 URL with customization parameters ('PK1' represents the name of a parameter declared in the template content, 'PV1' represents the value for the parameter. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back (same as specifying -DisableRollback $false). The specified notification AENs will receive published stack-related events.
New-CFNStack -StackName "myStack" ` -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template ` -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } ` -NotificationARN @( "arn1", "arn2" )-
For API details, see CreateStack in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Creates a new stack with the specified name. The template is parsed from the supplied content with customization parameters ('PK1' and 'PK2' represent the names of parameters declared in the template content, 'PV1' and 'PV2' represent the values for those parameters. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will not be rolled back.
New-CFNStack -StackName "myStack" ` -TemplateBody "{TEMPLATE CONTENT HERE}" ` -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) ` -DisableRollback $trueExample 2: Creates a new stack with the specified name. The template is parsed from the supplied content with customization parameters ('PK1' and 'PK2' represent the names of parameters declared in the template content, 'PV1' and 'PV2' represent the values for those parameters. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back.
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter $p1.ParameterKey = "PK1" $p1.ParameterValue = "PV1" $p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter $p2.ParameterKey = "PK2" $p2.ParameterValue = "PV2" New-CFNStack -StackName "myStack" ` -TemplateBody "{TEMPLATE CONTENT HERE}" ` -Parameter @( $p1, $p2 ) ` -OnFailure "ROLLBACK"Example 3: Creates a new stack with the specified name. The template is obtained from the Amazon S3 URL with customization parameters ('PK1' represents the name of a parameter declared in the template content, 'PV1' represents the value for the parameter. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back (same as specifying -DisableRollback $false).
New-CFNStack -StackName "myStack" ` -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template ` -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }Example 4: Creates a new stack with the specified name. The template is obtained from the Amazon S3 URL with customization parameters ('PK1' represents the name of a parameter declared in the template content, 'PV1' represents the value for the parameter. The customization parameters can also be specified using 'Key' and 'Value' instead of 'ParameterKey' and 'ParameterValue'. If creation of the stack fails, it will be rolled back (same as specifying -DisableRollback $false). The specified notification AENs will receive published stack-related events.
New-CFNStack -StackName "myStack" ` -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template ` -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } ` -NotificationARN @( "arn1", "arn2" )-
For API details, see CreateStack in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Note
You can use the AWS CLI --template-url option to specify a template
file location in Amazon S3 or AWS Systems Manager.
For Amazon S3, the URL must begin with https://. S3 static website URLs are not supported.
--template-urlhttps://s3.region-code.amazonaws.com/bucket-name/template-name
For AWS Systems Manager, use the following format:
--template-url"ssm-doc://arn:aws:ssm:region-code:account-id:document/document-name"
Express mode
To create a stack using express mode, add the --deployment-config
parameter. Express mode completes the stack operation as soon as CloudFormation applies
the resource configuration. For more information, see Express mode.
aws cloudformation create-stack --stack-namemyteststack\ --template-bodyfile://sampletemplate.json\ --deployment-config '{"mode": "EXPRESS"}'
Create a stack that includes transforms
Use the deploy
command to create a stack that includes transforms. When you create a stack from a
template that includes transforms, you must use a change set. The deploy
command combines two steps (creating a change set and executing it) into a single
command.
- AWS CLI
-
The following
deploycommand creates a stack with the specified name, template, and input parameters.aws cloudformation deploy --stack-namemyteststack\ --template/path_to_template/my-template.json\ --parameter-overridesKey1=Value1 Key2=Value2
Delete a stack
Use the delete-stack command to delete a stack. For more information, see Delete a stack.
- CLI
-
- AWS CLI
-
To delete a stack
The following
delete-stackexample deletes the specified stack.aws cloudformation delete-stack \ --stack-namemy-stackThis command produces no output.
-
For API details, see DeleteStack
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Deletes the specified stack.
Remove-CFNStack -StackName "myStack"-
For API details, see DeleteStack in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Deletes the specified stack.
Remove-CFNStack -StackName "myStack"-
For API details, see DeleteStack in AWS Tools for PowerShell Cmdlet Reference (V5).
-
If the deletion fails and returns a DELETE_FAILED state, you can choose
to delete the stack by force using the --deletion-mode option. These are
the following values that can be used with deletion-mode:
-
STANDARD: Deletes the stack normally. This is the default deletion mode. -
FORCE_DELETE_STACK: Deletes the stack and skips all resources that are failing to delete.
- AWS CLI
-
The following
delete-stackcommand force deletes thestack using themyteststackFORCE_DELETE_STACKvalue with thedeletion-modeparameter:aws cloudformation delete-stack --stack-namemyteststack\ --deletion-mode FORCE_DELETE_STACKThis command produces no output.
After using FORCE_DELETE_STACK, you can use the
list-stack-resources command to list the resources that were skipped
during the stack deletion process. The retained resources will show a DELETE_SKIPPED
status. For more information, see List stack resources.
Express mode
To delete a stack using express mode, add the --deployment-config
parameter:
aws cloudformation delete-stack --stack-namemyteststack\ --deployment-config '{"mode": "EXPRESS"}'
For more information, see Express mode.
Describe stack events
Use the describe-stack-events command to describe stack events. For more information, see Monitor stack progress.
- CLI
-
- AWS CLI
-
To describe stack events
The following
describe-stack-eventsexample displays the 2 most recent events for the specified stack.aws cloudformation describe-stack-events \ --stack-namemy-stack\ --max-items2{"StackEvents":[{"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "EventId": "4e1516d0-e4d6-xmpl-b94f-0a51958a168c", "StackName": "my-stack", "LogicalResourceId": "my-stack", "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "ResourceType": "AWS::CloudFormation::Stack", "Timestamp": "2019-10-02T05:34:29.556Z", "ResourceStatus":"UPDATE_COMPLETE"},{"StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "EventId": "4dd3c810-e4d6-xmpl-bade-0aaf8b31ab7a", "StackName": "my-stack", "LogicalResourceId": "my-stack", "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "ResourceType": "AWS::CloudFormation::Stack", "Timestamp": "2019-10-02T05:34:29.127Z", "ResourceStatus":"UPDATE_COMPLETE_CLEANUP_IN_PROGRESS"}],"NextToken":"eyJOZXh0VG9XMPLiOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAyfQ=="}-
For API details, see DescribeStackEvents
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Returns all stack related events for the specified stack.
Get-CFNStackEvent -StackName "myStack"-
For API details, see DescribeStackEvents in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Returns all stack related events for the specified stack.
Get-CFNStackEvent -StackName "myStack"-
For API details, see DescribeStackEvents in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Describe a stack resource
- CLI
-
- AWS CLI
-
To get information about a stack resource
The following
describe-stack-resourceexample displays details for the resource namedMyFunctionin the specified stack.aws cloudformation describe-stack-resource \ --stack-nameMyStack\ --logical-resource-idMyFunctionOutput:
{ "StackResourceDetail": { "StackName": "MyStack", "StackId": "arn:aws:cloudformation:us-east-2:123456789012:stack/MyStack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "LogicalResourceId": "MyFunction", "PhysicalResourceId": "my-function-SEZV4XMPL4S5", "ResourceType": "AWS::Lambda::Function", "LastUpdatedTimestamp": "2019-10-02T05:34:27.989Z", "ResourceStatus": "UPDATE_COMPLETE", "Metadata": "{}", "DriftInformation": { "StackResourceDriftStatus": "IN_SYNC" } } }-
For API details, see DescribeStackResource
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Returns the description of a resource identified in the template associated with the specified stack by the logical ID "MyDBInstance".
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"-
For API details, see DescribeStackResource in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Returns the description of a resource identified in the template associated with the specified stack by the logical ID "MyDBInstance".
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"-
For API details, see DescribeStackResource in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Describe stack resources
- CLI
-
- AWS CLI
-
To get information about a stack resource
The following
describe-stack-resourcesexample displays details for the resources in the specified stack.aws cloudformation describe-stack-resources \ --stack-namemy-stackOutput:
{ "StackResources": [ { "StackName": "my-stack", "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "LogicalResourceId": "bucket", "PhysicalResourceId": "my-stack-bucket-1vc62xmplgguf", "ResourceType": "AWS::S3::Bucket", "Timestamp": "2019-10-02T04:34:11.345Z", "ResourceStatus": "CREATE_COMPLETE", "DriftInformation": { "StackResourceDriftStatus": "IN_SYNC" } }, { "StackName": "my-stack", "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "LogicalResourceId": "function", "PhysicalResourceId": "my-function-SEZV4XMPL4S5", "ResourceType": "AWS::Lambda::Function", "Timestamp": "2019-10-02T05:34:27.989Z", "ResourceStatus": "UPDATE_COMPLETE", "DriftInformation": { "StackResourceDriftStatus": "IN_SYNC" } }, { "StackName": "my-stack", "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "LogicalResourceId": "functionRole", "PhysicalResourceId": "my-functionRole-HIZXMPLEOM9E", "ResourceType": "AWS::IAM::Role", "Timestamp": "2019-10-02T04:34:06.350Z", "ResourceStatus": "CREATE_COMPLETE", "DriftInformation": { "StackResourceDriftStatus": "IN_SYNC" } } ] }-
For API details, see DescribeStackResources
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell V4
-
Example 1: Returns the AWS resource descriptions for up to 100 resources associated with the specified stack. To obtain details of all resources associated with a stack use the Get-CFNStackResourceSummary, which also supports manual paging of the results.
Get-CFNStackResourceList -StackName "myStack"Example 2: Returns the description of the Amazon EC2 instance identified in the template associated with the specified stack by the logical ID "Ec2Instance".
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"Example 3: Returns the description of up to 100 resources associated with the stack containing an Amazon EC2 instance identified by instance ID "i-123456". To obtain details of all resources associated with a stack use the Get-CFNStackResourceSummary, which also supports manual paging of the results.
Get-CFNStackResourceList -PhysicalResourceId "i-123456"Example 4: Returns the description of the Amazon EC2 instance identified by the logical ID "Ec2Instance" in the template for a stack. The stack is identified using the physical resource ID of a resource it contains, in this case also an Amazon EC2 instance with instance ID "i-123456". A different physical resource could also be used to identify the stack depending on the template content, for example an Amazon S3 bucket.
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"-
For API details, see DescribeStackResources in AWS Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
Example 1: Returns the AWS resource descriptions for up to 100 resources associated with the specified stack. To obtain details of all resources associated with a stack use the Get-CFNStackResourceSummary, which also supports manual paging of the results.
Get-CFNStackResourceList -StackName "myStack"Example 2: Returns the description of the Amazon EC2 instance identified in the template associated with the specified stack by the logical ID "Ec2Instance".
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"Example 3: Returns the description of up to 100 resources associated with the stack containing an Amazon EC2 instance identified by instance ID "i-123456". To obtain details of all resources associated with a stack use the Get-CFNStackResourceSummary, which also supports manual paging of the results.
Get-CFNStackResourceList -PhysicalResourceId "i-123456"Example 4: Returns the description of the Amazon EC2 instance identified by the logical ID "Ec2Instance" in the template for a stack. The stack is identified using the physical resource ID of a resource it contains, in this case also an Amazon EC2 instance with instance ID "i-123456". A different physical resource could also be used to identify the stack depending on the template content, for example an Amazon S3 bucket.
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"-
For API details, see DescribeStackResources in AWS Tools for PowerShell Cmdlet Reference (V5).
-
Describe stacks
- CLI
-
- AWS CLI
-
To describe AWS CloudFormation stacks
The following
describe-stackscommand shows summary information for themyteststackstack:aws cloudformation describe-stacks --stack-namemyteststackOutput:
{ "Stacks": [ { "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896", "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.", "Tags": [], "Outputs": [ { "Description": "Name of S3 bucket to hold website content", "OutputKey": "BucketName", "OutputValue": "myteststack-s3bucket-jssofi1zie2w" } ], "StackStatusReason": null, "CreationTime": "2013-08-23T01:02:15.422Z", "Capabilities": [], "StackName": "myteststack", "StackStatus": "CREATE_COMPLETE", "DisableRollback": false } ] }For more information, see Stacks in the AWS CloudFormation User Guide.
-
For API details, see DescribeStacks
in AWS CLI Command Reference.
-
- Go