CloudFormation template structure and intrinsics
Describe infrastructure instead of clicking
Infrastructure as code (IaC) stores the desired configuration in reviewable files. CloudFormation is an AWS service that manages a group of resources called a stack from a JSON or YAML template. Imagine a small archive application that needs a private S3 bucket. Instead of asking every operator to remember the same console settings, store those settings in a file, review the file in Git and deploy only after approval. Declarative means describing the desired state, not issuing every underlying API call yourself. It does not mean changes are automatically safe.
Read a complete template
Resources is the required section. Each logical ID, such as Archive, names a resource within the template; its type identifies the AWS resource schema. Parameters accepts deployment inputs; Outputs exposes useful nonsecret values. Mappings stores lookup tables; Conditions controls whether selected resources or outputs exist or which property values are used. The optional format version is not a service API version. Here is a complete static template for review, not deployment. No bucket name is supplied, so AWS would generate one if deployed. Both retention policies are intentional and covered in lesson 3.
Private archive template
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Offline private archive example", "Parameters": { "Environment": { "Type": "String", "AllowedValues": [ "dev", "prod" ], "Default": "dev" } }, "Resources": { "Archive": { "Type": "AWS::S3::Bucket", "DeletionPolicy": "Retain", "UpdateReplacePolicy": "Retain", "Properties": { "PublicAccessBlockConfiguration": { "BlockPublicAcls": true, "BlockPublicPolicy": true, "IgnorePublicAcls": true, "RestrictPublicBuckets": true }, "VersioningConfiguration": { "Status": "Enabled" }, "Tags": [ { "Key": "Environment", "Value": { "Ref": "Environment" } } ] } } }, "Outputs": { "BucketName": { "Value": { "Ref": "Archive" } }, "BucketArn": { "Value": { "Fn::GetAtt": [ "Archive", "Arn" ] } } }}Resolve values and dependencies
An intrinsic function is an expression evaluated by CloudFormation in supported template locations. Ref returns the parameter value or a resource-specific value: for this S3 bucket it returns the bucket name. Fn::GetAtt returns a documented attribute, here its ARN. Fn::Sub substitutes variables, for example {"Fn::Sub":"${Environment}-archive"} becomes dev-archive with the default parameter. Fn::Join combines values with a separator; {"Fn::Join":["-",["dev","archive"]]} gives the same text. Fn::FindInMap reads a mapping and Fn::If selects between two values using a named condition. These expressions are not general-purpose scripting. References to other resources establish implicit dependencies in relevant resource properties; DependsOn adds explicit ordering when needed. Independent resources can be processed in parallel.
Practice and common mistakes
Change the default Environment to prod on paper and predict the tag. The output bucket name is still unknown before creation; the logical ID is not a physical name. Review the S3 resource reference for property types and update behavior. A JSON parser only checks syntax, not AWS resource compatibility or permission. Keep credentials out of templates, parameters committed to Git, metadata and outputs. The next lesson examines how to review a proposed stack change.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CloudFormation template structure and intrinsics — knowledge check
Review a fictional infrastructure change without deploying AWS resources.
Which section declares the S3 bucket?
What does Ref on Archive return in this template?
With Environment set to prod, what value is assigned to the Environment tag?
Which expression retrieves the bucket ARN?
A template parses as JSON. What can you conclude?