AWS CloudFormation Command Line Operations: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 17: Line 17:


  aws cloudformation create-stack --capabilities CAPABILITY_NAMED_IAM ...
  aws cloudformation create-stack --capabilities CAPABILITY_NAMED_IAM ...
For more details on capabilities, see: {{Internal|AWS_CloudFormation_Concepts#Capabilities|CloudFormation Concepts - Capabilities}}


A stack can only be created once, a subsequent invocation of the 'create-stack' command will result in:
A stack can only be created once, a subsequent invocation of the 'create-stack' command will result in:
Line 25: Line 27:
{{External|[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-direct.html Updating Stacks Directly]}}
{{External|[https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-direct.html Updating Stacks Directly]}}


  aws cloudformation update-stack --stack-name ''stack-name'' --template-body file://.../''stack-template''.yml [--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2]
  aws cloudformation update-stack \
  --stack-name ''stack-name'' \
  [--use-previous-template|--template-body file://.../''stack-template''.yml] \
  [--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2]


Updates  a stack as specified in the template. After the call completes successfully, the stack update starts.
A stack can be updated in two modes: if there are template changes, and in this case --template-body option should be used, or if there are no template changes, only parameter changes, and in this case, --use-previous-template option should be used, along with the updated parameters. Updates  a stack as specified in the template. After the call completes successfully, the stack update starts.


  aws cloudformation update-stack --stack-name mystack --use-previous-template --parameters ...
  aws cloudformation update-stack --stack-name mystack --use-previous-template --parameters ...
Line 43: Line 48:
=Describe a Stack=
=Describe a Stack=


  aws cloudformation describe-stacks [--stack-name ''stackname'']
All stacks from the region (or the default region):
 
  aws [-region ''region-name''] cloudformation describe-stacks  
 
Just one stack from the region (or the default region):
 
aws [-region ''region-name''] cloudformation describe-stacks --stack-name ''stackname''
 
=Listing Exports=
 
aws [--region ca-central-1] cloudformation  list-exports
 
Result:
 
<syntaxhighlight lang='json'>
{
    "Exports": [
        {
            "ExportingStackId": "arn:aws:cloudformation:ca-central-1:777777777777:stack/infinity-microworld/293e2a60-688e-11e9-8ce9-0a20f171189a",
            "Value": "arn:aws:iam:: 777777777777:role/service-role/ca-central-1-infinity-apigateway-lambda-invoker-service-role",
            "Name": "infinity-apigateway-lambda-invoker-service-role-arn"
        },
        ...
        {
            "ExportingStackId": "arn:aws:cloudformation:ca-central-1: 777777777777:stack/infinity-microworld/293e2a60-688e-11e9-8ce9-0a20f171189a",
            "Value": "nat-77705cc2c057b226b",
            "Name": "infinity-nat-id"
        }
    ]
}
</syntaxhighlight>
 
=Listing Stacks that Use a Specific Export=
 
aws cloudformation  list-imports --export-name ''export-name''
 
If the export name does not exist:
 
aws --region ca-central-1 cloudformation  list-imports --export-name something; echo $?
An error occurred (ValidationError) when calling the ListImports operation: Export 'something' does not exist.
255


=Obtaining the Template of an Existing Stack=
=Obtaining the Template of an Existing Stack=

Latest revision as of 02:49, 28 April 2019

Internal

Validate Templates

https://docs.aws.amazon.com/cli/latest/reference/cloudformation/validate-template.html
https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_ValidateTemplate.html
aws cloudformation validate-template --template-body file://.../stack-template.yml

Create a Stack

aws cloudformation create-stack --stack-name stack-name --template-body file://.../stack-template.yml [--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2]

If the template is supposed to create IAM roles, the command line will fail with a message similar to "An error occurred (InsufficientCapabilitiesException) when calling the CreateStack operation: Requires capabilities : [CAPABILITY_NAMED_IAM]", unless the capability is explicitly provided on command line with:

aws cloudformation create-stack --capabilities CAPABILITY_NAMED_IAM ...

For more details on capabilities, see:

CloudFormation Concepts - Capabilities

A stack can only be created once, a subsequent invocation of the 'create-stack' command will result in:

An error occurred (AlreadyExistsException) when calling the CreateStack operation: Stack [api-experiments] already exists

Update a Stack

Updating Stacks Directly
aws cloudformation update-stack \
  --stack-name stack-name \
  [--use-previous-template|--template-body file://.../stack-template.yml] \
  [--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2]

A stack can be updated in two modes: if there are template changes, and in this case --template-body option should be used, or if there are no template changes, only parameter changes, and in this case, --use-previous-template option should be used, along with the updated parameters. Updates a stack as specified in the template. After the call completes successfully, the stack update starts.

aws cloudformation update-stack --stack-name mystack --use-previous-template --parameters ...

Cancel an Update

Canceling a Stack Update
aws cloudformation cancel-update-stack

Delete a Stack

aws cloudformation delete-stack --stack-name stack-name

Describe a Stack

All stacks from the region (or the default region):

aws [-region region-name] cloudformation describe-stacks 

Just one stack from the region (or the default region):

aws [-region region-name] cloudformation describe-stacks --stack-name stackname

Listing Exports

aws [--region ca-central-1] cloudformation  list-exports

Result:

{
    "Exports": [
        {
            "ExportingStackId": "arn:aws:cloudformation:ca-central-1:777777777777:stack/infinity-microworld/293e2a60-688e-11e9-8ce9-0a20f171189a",
            "Value": "arn:aws:iam:: 777777777777:role/service-role/ca-central-1-infinity-apigateway-lambda-invoker-service-role",
            "Name": "infinity-apigateway-lambda-invoker-service-role-arn"
        },
        ...
        {
            "ExportingStackId": "arn:aws:cloudformation:ca-central-1: 777777777777:stack/infinity-microworld/293e2a60-688e-11e9-8ce9-0a20f171189a",
            "Value": "nat-77705cc2c057b226b",
            "Name": "infinity-nat-id"
        }
    ]
}

Listing Stacks that Use a Specific Export

aws cloudformation  list-imports --export-name export-name

If the export name does not exist:

aws --region ca-central-1 cloudformation  list-imports --export-name something; echo $?
An error occurred (ValidationError) when calling the ListImports operation: Export 'something' does not exist.
255

Obtaining the Template of an Existing Stack

This is useful when we want to apply an update, but we don't have the original template document.

aws cloudformation get-template

Package

Uploading Local Artifacts to an S3 Bucket
aws cloudformation package --template-file deployment-stack.yaml --s3-bucket ${TARGET_BUCKET} --output-template-file new-deployment-stack.yaml

The command packages the local artifacts (local paths) the CloudFormation template references. The command uploads local artifacts, such as source code for an AWS Lambda function or a Swagger file for an API Gateway REST API to an S3 bucket. The command returns a copy of the template, replacing references to local artifacts with the S3 location where the command uploaded the artifacts. Use this command to quickly upload local artifacts that might be required by your template. After you package your template's artifacts, run the aws cloudformation deploy command to deploy the returned template. This command can upload local artifacts specified by following properties of a resource: BodyS3Location for a AWS::ApiGateway::RestApi resource, Code property for an AWS::Lambda::Function resource or a TemplateURL property of a AWS::CloudFormation::Stack resource.

Deploy

aws cloudformation deploy