AutoDeploy

Terraform Demos

Copy and modify these pre-built Terraform configurations for your infrastructure.

AWS

AWS S3 Bucket with Versioning

AWSUploaded

Create an S3 bucket with versioning, encryption, and lifecycle policy.

By System · Storage

main.tf · variables.tf · outputs.tf

Download
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

resource "aws_s3_bucket" "main" {
  bucket = var.bucket_name

  tags = {
    Name        = var.bucket_name
    Environment = var.environment
  }
}

resource "aws_s3_bucket_versioning" "main" {
  bucket = aws_s3_bucket.main.id
  versioning_configuration {
    status = "Enabled"
  }
}

resource "aws_s3_bucket_server_side_encryption_configuration" "main" {
  bucket = aws_s3_bucket.main.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "AES256"
    }
  }
}

resource "aws_s3_bucket_public_access_block" "main" {
  bucket                  = aws_s3_bucket.main.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

variable "bucket_name" {
  description = "S3 bucket name"
  type        = string
}

variable "aws_region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

variable "environment" {
  description = "Environment"
  type        = string
  default     = "dev"
}

Troubleshooting & Solutions

Common Issues

  • Bucket name already exists
  • Cannot access bucket
  • Versioning not enabled

Solutions

  • Use a globally unique bucket name
  • Check IAM permissions
  • Verify versioning configuration

Troubleshooting Steps

  1. Verify bucket name is globally unique
  2. Check IAM permissions for S3
  3. Review bucket policy
  4. Ensure region is correct

Version compatibility: Terraform >= 1.0, AWS Provider >= 4.0

AWS

AWS RDS PostgreSQL Database

AWSUploaded

Create a secure RDS PostgreSQL instance with automated backups.

By System · Storage

main.tf · variables.tf · outputs.tf

Download
resource "aws_db_subnet_group" "main" {
  name       = "${var.app_name}-db-subnets"
  subnet_ids = var.private_subnet_ids
}

resource "aws_security_group" "db" {
  name   = "${var.app_name}-db-sg"
  vpc_id = var.vpc_id

  ingress {
    from_port       = 5432
    to_port         = 5432
    protocol        = "tcp"
    security_groups = [var.app_security_group_id]
  }
}

resource "aws_db_instance" "main" {
  identifier              = "${var.app_name}-db"
  engine                  = "postgres"
  engine_version          = "16"
  instance_class          = var.instance_class
  allocated_storage       = 20
  storage_encrypted       = true
  db_name                 = var.db_name
  username                = var.db_username
  password                = var.db_password
  db_subnet_group_name    = aws_db_subnet_group.main.name
  vpc_security_group_ids  = [aws_security_group.db.id]
  backup_retention_period = 7
  skip_final_snapshot     = false
  final_snapshot_identifier = "${var.app_name}-final-snapshot"
}

variable "app_name" {
  type = string
}

variable "vpc_id" {
  type = string
}

variable "private_subnet_ids" {
  type = list(string)
}

variable "app_security_group_id" {
  type = string
}

variable "instance_class" {
  type    = string
  default = "db.t3.micro"
}

variable "db_name" {
  type = string
}

variable "db_username" {
  type      = string
  sensitive = true
}

variable "db_password" {
  type      = string
  sensitive = true
}

Troubleshooting & Solutions

Common Issues

  • Connection timeout from the app
  • Password shows up in state in plain text
  • Instance stuck in "creating" for a long time

Solutions

  • Confirm the app's security group is allowed in the DB security group's ingress rule
  • Use a secrets manager (AWS Secrets Manager, SSM Parameter Store) instead of a plain .tfvars value for db_password in real deployments
  • Large allocated_storage or Multi-AZ can add 10-20 minutes to creation — this is normal

Troubleshooting Steps

  1. Verify the DB subnet group spans private subnets in at least 2 AZs
  2. Check the app and DB are in the same VPC or have VPC peering
  3. Confirm storage_encrypted is compatible with your KMS key permissions
  4. Review RDS event logs in the console for the specific failure

Version compatibility: Terraform >= 1.0, AWS Provider >= 5.0