CloudFormation & Terraform for $0 on the AWS Emulator

Test CloudFormation change sets, rollback and drift for $0 on the AWS Emulator, plus tfemu: a tflocal-style fix that makes Terraform S3 work.

Change sets, DeletionPolicy, rollback and drift detection, all tested locally. Then I made Terraform work on the emulator too, including the S3 bucket that fails out of the box.



TL;DR

  • The new AWS Emulator (launched Nov 11, 2026) runs CloudFormation well enough to practise every behaviour SAA-C03 asks about, with no account and no cost.
  • I built  env.sh , an on/off switch that shows  (aws-emu)  in your prompt, because one forgotten  source  sent my lab to a real AWS account.
  • I built  tfemu , a "tflocal" for the AWS Emulator. Plain Terraform fails to create an S3 bucket on the emulator (issue #56), and  tfemu  fixes it in one line.
  • Bonus: I ran the same drift test on both tools. CloudFormation reports drift; Terraform fixes it.
Who this is for: anyone studying for AWS Solutions Architect Associate, or anyone who wants to try IaC without a bill. New to the emulator itself? Start with the excellent intro "AWS Emulator in practice: test AWS on your machine, with no account and no cost" here on Builder Center, then come back for the IaC part.
You need: Linux, macOS or WSL, the  aws-emulator  binary, AWS CLI v2 and (for part 2) Terraform.



Part 0: Setup

Start the emulator

#!/bin/bash
# start.sh - start the AWS Emulator in the background
PORT=${AWS_EMU_PORT:-9297}
if pgrep -x aws-emulator >/dev/null; then echo "Already running."; exit 0; fi
nohup aws-emulator -p "$PORT" --telemetry off > "$HOME/aws-emu/emulator.log" 2>&1 &
for i in $(seq 30); do
curl -sf --cacert "$HOME/.aws/emulator/$PORT/ca.pem" "https://127.0.0.1:$PORT/__admin/health" >/dev/null 2>&1 \
&& { echo "AWS Emulator running on https://127.0.0.1:$PORT"; exit 0; }
sleep 1
done
echo "Did not come up in 30s. See ~/aws-emu/emulator.log"; exit 1

Tool 1:  env.sh , the on/off switch

Diagram: with source env.sh the terminal shows (aws-emu) and talks to the AWS Emulator (account 0000

The emulator gives you an endpoint. It doesn't stop a new terminal from quietly using your real AWS profile. So I wrote this:
# env.sh - point this shell at the local AWS Emulator.
# Usage: source env.sh (ON)
# source env.sh off (OFF, back to real AWS)
PORT=${AWS_EMU_PORT:-9297}
if [ "$1" = "off" ]; then
unset AWS_ENDPOINT_URL AWS_SHARED_CREDENTIALS_FILE AWS_CONFIG_FILE AWS_CA_BUNDLE \
AWS_EC2_METADATA_DISABLED AWS_DISABLE_HOST_PREFIX_INJECTION AWS_REGION AWS_DEFAULT_REGION
PS1=${PS1#"(aws-emu) "}; unset AWS_EMU_OLD_PS1
echo "Back to normal: this shell now talks to REAL AWS."
return 0 2>/dev/null || exit 0
fi
# Remove anything that could fall through to a real account
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE
export AWS_ENDPOINT_URL=https://127.0.0.1:$PORT
export AWS_SHARED_CREDENTIALS_FILE=$HOME/.aws/emulator/$PORT/emulator_credentials
export AWS_CONFIG_FILE=$HOME/.aws/emulator/$PORT/config
export AWS_CA_BUNDLE=$HOME/.aws/emulator/$PORT/ca.pem
export AWS_EC2_METADATA_DISABLED=true
export AWS_DISABLE_HOST_PREFIX_INJECTION=true
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
# Show where we are in the prompt
case "$PS1" in "(aws-emu) "*) ;; *) AWS_EMU_OLD_PS1="$PS1"; PS1="(aws-emu) $PS1";; esac
if curl -sf --cacert "$AWS_CA_BUNDLE" "$AWS_ENDPOINT_URL/__admin/health" >/dev/null 2>&1; then
echo "AWS Emulator is up on port $PORT. This shell now talks to the EMULATOR."
else
echo "Env set, but the emulator is not running. Start it with: ~/aws-emu/start.sh"
fi
What it does:
  • Clears  AWS_PROFILE  and any real keys, so nothing falls through to a real account
  • Points the endpoint, credentials, config and CA bundle at the emulator
  • Adds  (aws-emu)  to your prompt
  • Runs a health check
The one check to run before every lab:
(aws-emu) $ aws sts get-caller-identity --query Account --output text
000000000000
 000000000000  = emulator = safe. Any other number means stop.

Why I needed it (the honest part)

Halfway through, I opened a new terminal and forgot to  source env.sh . My  ~/.bashrc  sets a default AWS profile, so my drift-detection practice stack was created in a real AWS account instead of the emulator. It was one small SQS queue. I deleted it straight away and checked nothing else was left. But it's exactly the mistake a local emulator is meant to prevent.
Lesson: an emulator only protects you if your terminal is actually pointed at it. Make it visible, and check  sts get-caller-identity  every time.



Part 1: CloudFormation labs

Lab 1: Create a stack + preview with a change set

AWSTemplateFormatVersion: '2010-09-09'
Description: My first CloudFormation stack (protected data)

Resources:
OrdersQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: orders-queue

ReportsBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: yash-reports-bucket
VersioningConfiguration:
Status: Enabled

OrdersTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
TableName: orders-table
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: orderId
AttributeType: S
KeySchema:
- AttributeName: orderId
KeyType: HASH
Instead of updating blindly, create a change set first:
aws cloudformation create-change-set --stack-name my-stack \
--change-set-name add-table --template-body file://template.yaml
aws cloudformation describe-change-set --stack-name my-stack --change-set-name add-table
aws cloudformation execute-change-set --stack-name my-stack --change-set-name add-table
The preview showed Add for  OrdersTable  and Modify for  ReportsBucket  with  Replacement: False .
💡 Always check  Replacement .  True  means CloudFormation will delete the resource and create a new one, which is how data gets lost.

Lab 2:  DeletionPolicy: Retain 

aws cloudformation delete-stack --stack-name my-stack
The queue was deleted. The bucket and table survived, because they had  Retain .
Policy
What happens on delete
(none)
Resource is deleted
 Retain 
Resource is kept
 Snapshot 
Final backup, then deleted (RDS, EBS and similar)
 UpdateReplacePolicy 
Same idea, but for when an update replaces the resource

Lab 3: Rollback, all or nothing

AWSTemplateFormatVersion: '2010-09-09'
Description: Rollback demo

Resources:
GoodQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: good-queue

BadQueue:
Type: AWS::SQS::Queue
DependsOn: GoodQueue
Properties:
QueueName: bad-queue
MessageRetentionPeriod: 10 # invalid: minimum is 60
 GoodQueue  was created,  BadQueue  failed, and then CloudFormation deleted  GoodQueue  again. Final status:  ROLLBACK_COMPLETE .

Lab 4: Drift detection

Resources:
MyQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: drift-queue
VisibilityTimeout: 30
Change it by hand, then ask CloudFormation:
URL=$(aws sqs get-queue-url --queue-name drift-queue --query QueueUrl --output text)
aws sqs set-queue-attributes --queue-url "$URL" --attributes VisibilityTimeout=99
aws cloudformation detect-stack-drift --stack-name drift-demo
aws cloudformation describe-stack-resource-drifts --stack-name drift-demo
The result:  MyQueue  is MODIFIED,  VisibilityTimeout  expected  30 , actual  99 .
💡 Drift detection only checks properties you wrote in the template, and it only reports. It doesn't fix anything. For automatic checks, use AWS Config.



Part 2: Terraform on the emulator

The problem

Diagram: plain Terraform fails because S3 Control calls 000000000000.127.0.0.1; tfemu checks the acc

CloudFormation worked for everything. Terraform didn't.  aws_s3_bucket  failed because the AWS provider (6.23+) calls S3 Control, which puts the account ID in front of the hostname:
000000000000.127.0.0.1 ← not a valid host, so the request fails
This is emulator issue #56. LocalStack solves this kind of thing with  tflocal . The AWS Emulator has no equivalent yet, and it doesn't accept code contributions, so I built one.

Tool 2:  tfemu 

#!/bin/bash
# tfemu = "tflocal" for the AWS Emulator.
# Usage: tfemu init | plan | apply | destroy ... (same args as terraform)
# Adds a temporary provider override so the AWS provider talks to the local
# emulator, runs terraform, then removes the override again.
PORT=${AWS_EMU_PORT:-9297}
pgrep -x aws-emulator >/dev/null || ~/aws-emu/start.sh >/dev/null || { echo "tfemu: emulator failed to start"; exit 1; }
source ~/aws-emu/env.sh >/dev/null 2>&1

# Safety: refuse to run unless we are really talking to the emulator.
ACCT=$(aws sts get-caller-identity --query Account --output text 2>/dev/null)
if [ "$ACCT" != "000000000000" ]; then
echo "tfemu: STOP - account is '$ACCT', not the emulator (000000000000)."; exit 1
fi

# An override file needs an existing provider "aws" block; otherwise write a normal one.
if grep -qsE '^\s*provider\s+"aws"' *.tf 2>/dev/null; then F=_emu_override.tf; else F=_emu_provider.tf; fi
cat > "$F" <<TF
# Auto-generated by tfemu. Removed after each run.
provider "aws" {
region = "us-east-1"
insecure = true # emulator uses a local self-signed cert
skip_credentials_validation = true
skip_requesting_account_id = true
skip_metadata_api_check = true
s3_use_path_style = true
endpoints {
# Fix for issue #56: S3 Control puts the account id in front of the host
# (000000000000.<host>). localtest.me is public wildcard DNS -> 127.0.0.1.
s3control = "https://localtest.me:${PORT}"
}
}
TF
trap 'rm -f "$F"' EXIT
terraform "$@"
The key trick:  localtest.me  is public DNS that sends every name ending in  .localtest.me  to  127.0.0.1 . So  000000000000.localtest.me  is a valid host that still reaches the emulator.
How it stays safe:
  1. Refuses to run unless the account is  000000000000 
  1. Writes the override only for this run, then deletes it, so plain  terraform  in that folder still behaves normally
Install it:
chmod +x ~/aws-emu/tfemu
echo 'export PATH="$HOME/aws-emu:$PATH"' >> ~/.bashrc && source ~/.bashrc

Lab 5: Create an S3 bucket + SQS queue

resource "aws_s3_bucket" "photos" {
bucket = "yash-photos-bucket"
}

resource "aws_sqs_queue" "jobs" {
name = "photo-jobs"
visibility_timeout_seconds = 30
}

output "bucket_name" {
value = aws_s3_bucket.photos.bucket
}

output "queue_url" {
value = aws_sqs_queue.jobs.url
}
No  provider  block is needed, because  tfemu  adds it.
tfemu init
tfemu plan # Plan: 2 to add, 0 to change, 0 to destroy.
tfemu apply
aws_s3_bucket.photos: Creation complete after 0s [id=yash-photos-bucket]
aws_sqs_queue.jobs: Creation complete after 25s [id=https://sqs.us-east-1.amazonaws.com/000000000000/photo-jobs]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
✅ The S3 bucket that used to fail now gets created in 0 seconds.

Lab 6: The same drift test, Terraform style

aws sqs set-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/000000000000/photo-jobs \
--attributes VisibilityTimeout=99
tfemu plan
# aws_sqs_queue.jobs will be updated in-place
~ resource "aws_sqs_queue" "jobs" {
~ visibility_timeout_seconds = 99 -> 30
}

Plan: 0 to add, 1 to change, 0 to destroy.
tfemu apply # puts it back
aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/000000000000/photo-jobs \
--attribute-names VisibilityTimeout
{ "Attributes": { "VisibilityTimeout": "30" } }
💡 Same drift, different reaction. CloudFormation said "MODIFIED" and stopped. Terraform said "99 → 30" and fixed it.

Clean up

tfemu destroy # Destroy complete! Resources: 2 destroyed.



CloudFormation vs Terraform: what I actually saw


CloudFormation
Terraform
Preview
Change set
 plan 
Drift
Detects, reports only
 plan  detects,  apply  fixes
A resource fails
Automatic rollback (all or nothing)
No rollback: keeps what was created, you fix it and re-apply
State
Managed by AWS
 terraform.tfstate  (use S3 + locking for teams)
Keep data on delete
 DeletionPolicy: Retain 
 prevent_destroy  (blocks the destroy instead)
Clouds
AWS only
Many
On the SAA exam
✅ Yes
❌ No
Works on the AWS Emulator
✅ Out of the box
⚠️ Needs  tfemu  for S3



SAA-C03 cheat sheet

Situation
Answer
Preview what an update will do
Change set
Keep data when a resource/stack is deleted
DeletionPolicy: Retain / Snapshot
Keep the old resource when an update replaces it
UpdateReplacePolicy
Block updates from replacing/deleting a resource
Stack policy
Stop someone deleting the whole stack
Termination protection
Someone changed things by hand
Drift detection (+ AWS Config)
One template → many accounts / Regions
StackSets
Reuse a template inside another
Nested stacks
Serverless shorthand
SAM
Rebuild everything in a DR Region
CloudFormation templates + data replication



Known emulator gaps (as of Nov 2026)

Issue
What breaks
Workaround
#56
Terraform  aws_s3_bucket 
✅  tfemu  (this post)
#46
Terraform  aws_security_group 
none yet
#422
T2/T3  aws_instance 
none yet
Note: the  tfemu  S3 fix needs internet access, so DNS can resolve  localtest.me .



Takeaway

The AWS Emulator is a great free place to see CloudFormation behaviour instead of memorising it. The two things missing for me, a clear on/off switch and Terraform support, each took one small script.
Just remember the rule:  (aws-emu)  in the prompt and  000000000000  from  sts , or don't run it.
Tried this, or found another emulator gap? Tell me in the comments.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article