Deploy a CloudFormation stack
Deploy, update and delete a CloudFormation or SAM template against pogi with the AWS CLI.
pogi runs CloudFormation templates and creates each resource in its own service. You'll deploy a template with a queue and a topic, deploy a SAM template with a function, then update and delete a stack.
Before you start
-
Start pogi in another terminal:
pogi start -
Load the settings that point your tools at pogi:
eval "$(pogi env)"
You need the AWS CLI. The SAM section runs a Python function, so it needs python3 on your machine (or Docker). You don't need the SAM CLI: aws cloudformation deploy runs SAM templates too.
Deploy a template
-
Write a template with an SQS queue and an SNS topic. The queue's visibility timeout (how long a received message stays hidden) is a parameter, so you can change it later:
cat > template.yaml <<'YAML' AWSTemplateFormatVersion: "2010-09-09" Parameters: VisibilityTimeout: Type: Number Default: 30 Resources: Jobs: Type: AWS::SQS::Queue Properties: VisibilityTimeout: !Ref VisibilityTimeout Alerts: Type: AWS::SNS::Topic Outputs: QueueUrl: Value: !Ref Jobs YAML -
Deploy it as a stack called
demo:aws cloudformation deploy --template-file template.yaml --stack-name demoOutput Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - demo -
List the stack's resources:
aws cloudformation describe-stack-resources --stack-name demo \ --query 'StackResources[].[LogicalResourceId,ResourceType,ResourceStatus]' --output textOutput Alerts AWS::SNS::Topic CREATE_COMPLETE Jobs AWS::SQS::Queue CREATE_COMPLETE -
See the queue in SQS. CloudFormation named it for you:
aws sqs list-queues --query QueueUrls --output textOutput http://localhost:7644/000000000000/demo-Jobs-ERK6F9QHM3FI
Deploy a SAM template
A SAM (Serverless Application Model) template is a CloudFormation template with a Transform line. The transform turns one AWS::Serverless::Function into a function and its role.
-
Write a template with one function. The code is inline:
cat > sam.yaml <<'YAML' AWSTemplateFormatVersion: "2010-09-09" Transform: AWS::Serverless-2016-10-31 Resources: Hello: Type: AWS::Serverless::Function Properties: Runtime: python3.12 Handler: index.handler InlineCode: | def handler(event, context): return {"hello": event.get("name", "world")} YAML -
Deploy it. The two capabilities allow the transform and the role it creates:
aws cloudformation deploy --template-file sam.yaml --stack-name sam-demo \ --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPANDOutput Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - sam-demo -
List the resources. The function and its role are both there:
aws cloudformation describe-stack-resources --stack-name sam-demo \ --query 'StackResources[].[LogicalResourceId,ResourceType,ResourceStatus]' --output textOutput Hello AWS::Lambda::Function CREATE_COMPLETE HelloRole AWS::IAM::Role CREATE_COMPLETE -
Call the function:
FUNCTION=$(aws lambda list-functions --query 'Functions[0].FunctionName' --output text) aws lambda invoke --function-name $FUNCTION --cli-binary-format raw-in-base64-out \ --payload '{"name":"pogi"}' result.json --query StatusCode cat result.json; echoOutput 200 {"hello": "pogi"}
Update and delete a stack
-
Deploy
template.yamlagain with a new value for the parameter. CloudFormation updates the stack:aws cloudformation deploy --template-file template.yaml --stack-name demo \ --parameter-overrides VisibilityTimeout=60Output Waiting for changeset to be created.. Waiting for stack create/update to complete Successfully created/updated stack - demo -
Check the queue:
QUEUE=$(aws cloudformation describe-stacks --stack-name demo --query 'Stacks[0].Outputs[0].OutputValue' --output text) aws sqs get-queue-attributes --queue-url $QUEUE --attribute-names VisibilityTimeout --query AttributesOutput { "VisibilityTimeout": "60" } -
Delete both stacks and wait until they are gone. Deleting a stack deletes its resources:
aws cloudformation delete-stack --stack-name demo aws cloudformation delete-stack --stack-name sam-demo aws cloudformation wait stack-delete-complete --stack-name demo aws cloudformation wait stack-delete-complete --stack-name sam-demo -
The queue is gone. This prints nothing, because no queues are left:
aws sqs list-queues --query QueueUrls --output text
Limits
- A template that uses a resource type pogi can't create is refused with an error that names the type. The coverage page lists the types it can create.
- SAM connectors, applications and deployment preferences are not supported yet.
- StackSets and macros are not supported yet.
- Drift detection always reports a stack as in sync.
See the CloudFormation row in the coverage page.