Bird
Raised Fist0
AWScloud~7 mins

CloudFormation vs Terraform awareness in AWS - CLI Comparison

Choose your learning style10 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.

Practice

(1/5)
1. Which statement best describes the main difference between AWS CloudFormation and Terraform?
easy
A. CloudFormation works only with AWS, while Terraform supports multiple cloud providers.
B. Terraform is only for AWS, CloudFormation supports all clouds.
C. Both tools only work with AWS services.
D. CloudFormation supports multiple clouds, Terraform is AWS-only.

Solution

  1. Step 1: Understand CloudFormation scope

    CloudFormation is designed specifically for AWS and manages AWS resources only.
  2. Step 2: Understand Terraform scope

    Terraform supports many cloud providers like AWS, Azure, Google Cloud, and more.
  3. Final Answer:

    CloudFormation works only with AWS, while Terraform supports multiple cloud providers. -> Option A
  4. Quick Check:

    CloudFormation = AWS-only, Terraform = multi-cloud [OK]
Hint: CloudFormation = AWS-only; Terraform = many clouds [OK]
Common Mistakes:
  • Thinking Terraform only works with AWS
  • Believing CloudFormation supports multiple clouds
  • Confusing both as identical tools
2. Which of the following is the correct way to define a resource in AWS CloudFormation?
easy
A. resource "aws_ec2_instance" "web" {}
B. resource "aws_instance" "web" {}
C. AWS::EC2::Instance:
D. resource "ec2_instance" "web" {}

Solution

  1. Step 1: Identify CloudFormation syntax

    CloudFormation uses YAML or JSON with resource types like 'AWS::EC2::Instance'.
  2. Step 2: Recognize Terraform syntax

    Terraform uses 'resource "aws_instance" "name" {}'.
  3. Final Answer:

    CloudFormation resource types use 'AWS::Service::Resource' format. -> Option C
  4. Quick Check:

    CloudFormation uses AWS:: notation [OK]
Hint: CloudFormation uses AWS::Service::Resource format [OK]
Common Mistakes:
  • Confusing Terraform resource syntax with CloudFormation
  • Using Terraform resource blocks in CloudFormation
  • Misnaming resource types
3. Given this Terraform snippet:
resource "aws_s3_bucket" "bucket" {
  bucket = "my-unique-bucket"
  acl    = "private"
}

What will happen when you run terraform apply?
medium
A. Creates a public S3 bucket named 'my-unique-bucket' in AWS.
B. Fails because bucket names cannot be unique.
C. Throws a syntax error due to missing region.
D. Creates a private S3 bucket named 'my-unique-bucket' in AWS.

Solution

  1. Step 1: Analyze Terraform resource block

    The resource defines an AWS S3 bucket with name 'my-unique-bucket' and ACL set to 'private'.
  2. Step 2: Understand Terraform apply behavior

    Terraform will create the bucket with the specified name and private access control.
  3. Final Answer:

    Creates a private S3 bucket named 'my-unique-bucket' in AWS. -> Option D
  4. Quick Check:

    ACL 'private' means private bucket [OK]
Hint: ACL 'private' means bucket is private [OK]
Common Mistakes:
  • Assuming default ACL is public
  • Expecting syntax error without region (region is set elsewhere)
  • Thinking bucket names cannot be unique (they must be unique)
4. You wrote this CloudFormation snippet:
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-bucket
      AccessControl: PublicRead

But deployment fails with an error about bucket name. What is the likely cause?
medium
A. BucketName must be globally unique and 'my-bucket' is already taken.
B. AccessControl property is invalid in CloudFormation.
C. Resource type AWS::S3::Bucket is incorrect.
D. BucketName cannot be specified in CloudFormation.

Solution

  1. Step 1: Understand S3 bucket naming rules

    S3 bucket names must be globally unique across all AWS accounts.
  2. Step 2: Analyze error cause

    If 'my-bucket' is already used, deployment fails due to name conflict.
  3. Final Answer:

    BucketName must be globally unique and 'my-bucket' is already taken. -> Option A
  4. Quick Check:

    S3 bucket names are global and unique [OK]
Hint: S3 bucket names must be globally unique [OK]
Common Mistakes:
  • Thinking AccessControl is invalid (it is valid)
  • Assuming resource type is wrong
  • Believing BucketName is optional or disallowed
5. Your team wants to manage infrastructure across AWS, Azure, and Google Cloud with one tool. Which approach is best?
hard
A. Use AWS CloudFormation for all clouds to keep consistency.
B. Use Terraform because it supports multiple cloud providers.
C. Use separate CloudFormation stacks for each cloud provider.
D. Use manual console setup for each cloud to avoid tool complexity.

Solution

  1. Step 1: Identify tool cloud support

    CloudFormation only supports AWS, so it cannot manage Azure or Google Cloud.
  2. Step 2: Choose tool for multi-cloud management

    Terraform supports AWS, Azure, Google Cloud, and others in one tool.
  3. Step 3: Evaluate other options

    Separate CloudFormation stacks or manual setup increase complexity and reduce automation benefits.
  4. Final Answer:

    Use Terraform because it supports multiple cloud providers. -> Option B
  5. Quick Check:

    Terraform = multi-cloud support [OK]
Hint: Terraform supports many clouds; CloudFormation is AWS-only [OK]
Common Mistakes:
  • Assuming CloudFormation works outside AWS
  • Thinking manual setup is simpler for multi-cloud
  • Believing separate CloudFormation stacks can manage non-AWS clouds
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard