502 — Provision AWS Infrastructure with Terraform

Intermediate

Use Terraform to provision an IAM role, S3 bucket, Lambda function, and CloudWatch log group on AWS, inspect the plan, apply the configuration, verify outputs, update safely, and destroy everything cleanly.

Learning Objectives

1
Explain how Terraform configuration, providers, state, and plans fit together
2
Write Terraform configuration for AWS IAM, S3, Lambda, and CloudWatch Logs resources
3
Initialize the AWS provider with terraform init
4
Format and validate Terraform configuration before planning
5
Use terraform plan to preview resource creation before apply
6
Apply the configuration and inspect Terraform outputs
7
Verify the deployed Lambda function through the AWS CLI
8
Preview and apply a Terraform update using variables
9
Destroy Terraform-managed AWS resources and understand why state must be preserved until cleanup is complete
Step 1

Understand the Terraform model

Before writing any files, understand how Terraform manages AWS resources differently from CloudFormation.

Instructions

No commands in this step — read the Terraform model before writing configuration

What This Does

Terraform uses declarative configuration files to describe the desired infrastructure state.

The AWS provider translates Terraform resources such as aws_s3_bucket, aws_iam_role, and aws_lambda_function into AWS API calls.

The core workflow is: - terraform init downloads providers and prepares the working directory - terraform fmt formats configuration consistently - terraform validate checks configuration syntax and provider schema validity - terraform plan previews the actions Terraform would take - terraform apply performs those actions and records the result in state - terraform destroy removes resources tracked in state

The most important difference from CloudFormation is state ownership.

CloudFormation stores stack state inside AWS.

Terraform stores state in a state file, usually terraform.tfstate for local learning projects.

That file maps Terraform resource addresses to real AWS resource IDs.

Do not delete terraform.tfstate before cleanup — without state, Terraform no longer knows which live resources it created.

Expected Outcome

No output — this step is conceptual.

Proceed to step 2 to create an isolated Terraform working directory.

Pro Tips

  • 1
    Billing callout: Terraform itself is free. You pay only for the AWS resources it creates. This lesson creates IAM, an empty S3 bucket, Lambda, and a small CloudWatch Logs log group with short retention — negligible cost on any account type for this exercise.
  • 2
    Terraform plan is the closest Terraform equivalent to a CloudFormation change set. Both show proposed changes before execution, but Terraform compares configuration plus state against provider-read live infrastructure.
  • 3
    For production, use remote state with locking. For this lesson, local state keeps the workflow simple and visible.
Was this step helpful?

All Steps (0 / 13 completed)