Anatomy of a Template
Parameters, Resources, Outputs, and the intrinsic functions that tie them together.
1 min read
A template is YAML (or JSON) with a few top-level sections. Only Resources is required.
Try it
template.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: NoteSnap storage
Parameters:
Stage:
Type: String
AllowedValues: [dev, prod]
Default: dev
Resources:
UploadsBucket:
Type: AWS::S3::Bucket
Properties:
VersioningConfiguration:
Status: Enabled
SummariesTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain # keep the data if the stack is deleted
Properties:
TableName: !Sub "notesnap-summaries-${Stage}"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: noteId
AttributeType: S
KeySchema:
- AttributeName: noteId
KeyType: HASH
Outputs:
BucketName:
Value: !Ref UploadsBucket
TableArn:
Value: !GetAtt SummariesTable.ArnReading it
- Parameters: inputs you choose at deploy time, like
Stage. - Resources: each has a logical ID (
UploadsBucket), a Type, and Properties. - Outputs: values to show or share with other stacks.
Intrinsic functions
| Function | Returns | Example |
|---|---|---|
!Ref | A parameter's value, or a resource's main ID | !Ref UploadsBucket → bucket name |
!GetAtt | A specific attribute of a resource | !GetAtt SummariesTable.Arn |
!Sub | A string with ${…} substituted | !Sub "notesnap-${Stage}" |
Tip
Notice we didn't name the bucket. CloudFormation generates a unique name, which avoids global bucket-name collisions and lets you deploy the same template many times.
Key takeaways
- Templates have Parameters, Resources (required), and Outputs.
- !Ref, !GetAtt, and !Sub connect resources together.
- DeletionPolicy: Retain protects data when a stack is deleted.
Check your understanding
Pick an answer to see the explanation.
1. You need a DynamoDB table's ARN inside your template. Which function?