Provision Infrastructure with Terraform
What you'll build
A Terraform configuration that creates an AWS S3 bucket with versioning enabled, using declarative Infrastructure as Code.
Goals
- Understand Terraform configuration syntax (HCL)
- Write a Terraform configuration to create an S3 bucket
- Run terraform plan and apply to provision resources
- Understand Terraform state management
Prerequisites
- Terraform installed on your machine
- An AWS account with access keys configured
- Basic understanding of cloud resources
Steps
- 1
Configure the AWS provider
Create a
main.tffile that configures the AWS provider and sets the region. This tells Terraform which cloud to use.Codeprovider "aws" { region = "us-east-1" }Expected result
A main.tf file with the AWS provider configuration.
- 2
Define the S3 bucket resource
Add a resource block that creates an S3 bucket with a unique name and versioning enabled.
Coderesource "aws_s3_bucket" "my_bucket" { bucket = "my-terraform-bucket-12345" } resource "aws_s3_bucket_versioning" "my_bucket" { bucket = aws_s3_bucket.my_bucket.id versioning_configuration { status = "Enabled" } }Expected result
The Terraform configuration now defines an S3 bucket with versioning.
- 3
Plan and apply
Run
terraform initto initialize,terraform planto see what will be created, thenterraform applyto create the bucket.Codeterraform init terraform plan terraform applyExpected result
Terraform creates the S3 bucket in your AWS account. Verify in the AWS console.
- 4
Clean up
When you are done, run
terraform destroyto remove the bucket and avoid charges.Codeterraform destroyExpected result
The S3 bucket is deleted from your AWS account.
- 5
Add a health check endpoint
Add a /health endpoint returning 200 OK for orchestration systems (Kubernetes, load balancers) to probe liveness. Health checks are foundational for production deployment.
Code# app.py (Flask example) from flask import Flask, jsonify app = Flask(__name__) @app.route("/health") def health(): return jsonify({"status": "ok"}), 200 @app.route("/ready") def ready(): # Check DB connection etc. return jsonify({"ready": True}), 200Expected result
curl localhost:5000/health returns {"status":"ok"} with a 200 status code.
Try it yourself
What you learned
You used Terraform to provision cloud infrastructure as code. This approach makes infrastructure reproducible, version-controlled, and easy to manage.
Related Resources
Related Glossary
- Terraform
Infrastructure as Code tool
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- Terraform
Infrastructure as Code tool
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.