0
0
AWScloud~7 mins

CloudFormation vs Terraform awareness in AWS - CLI Comparison

Choose your learning style9 modes available
Introduction
Managing cloud resources can be complex. CloudFormation and Terraform help by letting you describe your resources in files, so you can create and update them easily and reliably.
When you want to create AWS resources using code to avoid manual clicks in the console.
When you need to keep track of changes to your cloud setup over time.
When you want to reuse the same setup in different environments like testing and production.
When you want to automate resource creation as part of your deployment process.
When you want to manage resources across multiple cloud providers or just AWS.
Commands
This command creates a new AWS CloudFormation stack named 'example-stack' using the template file 'template.yaml'. It sets up resources defined in the template.
Terminal
aws cloudformation create-stack --stack-name example-stack --template-body file://template.yaml
Expected OutputExpected
An identifier for the stack creation request, such as: { "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/example-stack/abcd1234-5678-90ef-ghij-klmnopqrstuv" }
--stack-name - Names the CloudFormation stack to create
--template-body - Specifies the local template file to use
This command checks the status and details of the 'example-stack' CloudFormation stack to confirm it was created successfully.
Terminal
aws cloudformation describe-stacks --stack-name example-stack
Expected OutputExpected
A JSON output showing stack details, including status like CREATE_COMPLETE, for example: { "Stacks": [ { "StackName": "example-stack", "StackStatus": "CREATE_COMPLETE", "CreationTime": "2024-06-01T12:00:00.000Z" } ] }
--stack-name - Specifies which stack to describe
This command initializes a Terraform working directory, downloading necessary provider plugins and preparing for deployment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command applies the Terraform configuration to create or update cloud resources automatically without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate an execution plan. Plan: 1 to add, 0 to change, 0 to destroy. aws_instance.example: Creating... aws_instance.example: Creation complete after 10s [id=i-1234567890abcdef0] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval before applying changes
This command displays the current state of resources managed by Terraform, showing what exists in the cloud.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-1234567890abcdef0 ami = ami-0abcdef1234567890 instance_type = t2.micro
Key Concept

If you remember nothing else, remember: CloudFormation is AWS-specific and built-in, while Terraform works across many clouds and has its own workflow.

Common Mistakes
Trying to use Terraform commands without running 'terraform init' first
Terraform needs to download providers and set up the environment before applying changes, so commands fail without initialization.
Always run 'terraform init' once before 'terraform apply' or other commands.
Using CloudFormation create-stack without waiting for stack creation to complete before checking status
The stack creation is asynchronous; checking status too soon may show incomplete or failed status.
Wait a few moments or use 'aws cloudformation wait stack-create-complete' before describing the stack.
Confusing CloudFormation template syntax with Terraform configuration syntax
They use different languages and formats; templates won't work if used in the wrong tool.
Use YAML or JSON for CloudFormation templates and HCL for Terraform files.
Summary
Use 'aws cloudformation create-stack' to create AWS resources with CloudFormation templates.
Use 'terraform init' and 'terraform apply' to manage resources with Terraform configurations.
Check resource status with 'aws cloudformation describe-stacks' for CloudFormation and 'terraform show' for Terraform.