Skip to content

Provision Infrastructure with Terraform

Beginner ~25 min

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. 1

    Configure the AWS provider

    Create a main.tf file that configures the AWS provider and sets the region. This tells Terraform which cloud to use.

    Code
    provider "aws" {
      region = "us-east-1"
    }

    Expected result

    A main.tf file with the AWS provider configuration.

  2. 2

    Define the S3 bucket resource

    Add a resource block that creates an S3 bucket with a unique name and versioning enabled.

    Code
    resource "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. 3

    Plan and apply

    Run terraform init to initialize, terraform plan to see what will be created, then terraform apply to create the bucket.

    Code
    terraform init
    terraform plan
    terraform apply

    Expected result

    Terraform creates the S3 bucket in your AWS account. Verify in the AWS console.

  4. 4

    Clean up

    When you are done, run terraform destroy to remove the bucket and avoid charges.

    Code
    terraform destroy

    Expected result

    The S3 bucket is deleted from your AWS account.

  5. 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}), 200

    Expected 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

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic