Loading...
Loading...
Infrastructure-as-Code fundamentals for data engineers using Terraform to provision AWS resources (S3, EC2, IAM)
npx skill4agent add aradotso/data-skills iac-terraform-data-engineeringSkill by ara.so — Data Skills collection.
aws configureterraform/
├── main.tf # Main Terraform configuration
├── variables.tf # Input variables (if present)
├── outputs.tf # Output values (if present)
└── terraform.tfstate # State file (generated)terraform -chdir=terraform initterraform -chdir=terraform validateterraform -chdir=terraform fmtterraform -chdir=terraform planterraform -chdir=terraform applyyesterraform -chdir=terraform apply -auto-approveterraform -chdir=terraform destroyterraform/main.tf# terraform/main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
# S3 bucket for data storage
resource "aws_s3_bucket" "data_bucket" {
bucket = "my-unique-data-engineering-bucket-12345"
tags = {
Name = "Data Engineering Bucket"
Environment = "dev"
ManagedBy = "Terraform"
}
}
# EC2 instance for data processing
resource "aws_instance" "data_processor" {
ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2
instance_type = "t2.micro"
tags = {
Name = "Data Processor"
Environment = "dev"
ManagedBy = "Terraform"
}
}
# IAM role for EC2 instance
resource "aws_iam_role" "ec2_s3_role" {
name = "ec2-s3-access-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
}
]
})
}terraform/variables.tfvariable "aws_region" {
description = "AWS region for resources"
type = string
default = "us-east-1"
}
variable "environment" {
description = "Environment name"
type = string
default = "dev"
}
variable "bucket_name" {
description = "S3 bucket name for data storage"
type = string
# Set via terraform.tfvars or -var flag
}main.tfprovider "aws" {
region = var.aws_region
}
resource "aws_s3_bucket" "data_bucket" {
bucket = var.bucket_name
tags = {
Environment = var.environment
}
}terraform/terraform.tfvarsbucket_name = "my-unique-bucket-name-2026"
aws_region = "us-west-2"
environment = "production"terraform -chdir=terraform state listcat terraform/terraform.tfstate | jq -r '.resources[] | [.type, .name] | join(",")'# terraform/backend.tf
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "data-platform/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-state-lock"
}
}terraform -chdir=terraform init -backend-config="bucket=${TERRAFORM_STATE_BUCKET}"aws s3 lsaws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query 'Reservations[].Instances[].{ID:InstanceId, Name:Tags[?Key==`Name`].Value, Type:InstanceType, State:State.Name, PublicIP:PublicIpAddress, PrivateIP:PrivateIpAddress}' \
--output tableterraform -chdir=terraform show aws_s3_bucket.data_bucket# Raw data bucket
resource "aws_s3_bucket" "raw_data" {
bucket = "my-data-lake-raw-${var.environment}"
}
# Processed data bucket
resource "aws_s3_bucket" "processed_data" {
bucket = "my-data-lake-processed-${var.environment}"
}
# Enable versioning for data lineage
resource "aws_s3_bucket_versioning" "raw_data_versioning" {
bucket = aws_s3_bucket.raw_data.id
versioning_configuration {
status = "Enabled"
}
}
# Lifecycle rules for cost optimization
resource "aws_s3_bucket_lifecycle_configuration" "raw_data_lifecycle" {
bucket = aws_s3_bucket.raw_data.id
rule {
id = "archive-old-data"
status = "Enabled"
transition {
days = 90
storage_class = "GLACIER"
}
}
}# Security group for data processor
resource "aws_security_group" "data_processor_sg" {
name = "data-processor-sg"
description = "Security group for data processing instances"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Restrict in production
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# EC2 instance with user data for setup
resource "aws_instance" "data_processor" {
ami = var.ami_id
instance_type = "t3.medium"
vpc_security_group_ids = [aws_security_group.data_processor_sg.id]
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y python3 python3-pip
pip3 install pandas boto3
EOF
tags = {
Name = "Data Processor Instance"
}
}
# IAM instance profile
resource "aws_iam_instance_profile" "ec2_profile" {
name = "ec2-data-processor-profile"
role = aws_iam_role.ec2_s3_role.name
}# terraform/outputs.tf
output "s3_bucket_name" {
description = "Name of the S3 bucket"
value = aws_s3_bucket.data_bucket.id
}
output "s3_bucket_arn" {
description = "ARN of the S3 bucket"
value = aws_s3_bucket.data_bucket.arn
}
output "ec2_instance_id" {
description = "ID of the EC2 instance"
value = aws_instance.data_processor.id
}
output "ec2_public_ip" {
description = "Public IP of the EC2 instance"
value = aws_instance.data_processor.public_ip
}terraform -chdir=terraform output
terraform -chdir=terraform output -json | jq -r '.s3_bucket_name.value'# Force unlock (use with caution)
terraform -chdir=terraform force-unlock <LOCK_ID>main.tfresource "aws_s3_bucket" "data_bucket" {
bucket = "my-unique-name-${random_id.bucket_suffix.hex}"
}
resource "random_id" "bucket_suffix" {
byte_length = 4
}{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:*",
"ec2:*",
"iam:*"
],
"Resource": "*"
}
]
}terraform -chdir=terraform refreshterraform -chdir=terraform import aws_s3_bucket.data_bucket my-existing-bucket# 1. Configure AWS credentials
export AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID}"
export AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY}"
export AWS_DEFAULT_REGION="us-east-1"
# 2. Customize configuration
cd terraform
# Edit main.tf to set unique bucket name
# 3. Initialize Terraform
terraform init
# 4. Validate configuration
terraform validate
# 5. Format code
terraform fmt
# 6. Preview changes
terraform plan
# 7. Apply configuration
terraform apply
# 8. Verify resources
aws s3 ls
aws ec2 describe-instances --output table
# 9. When done, clean up
terraform destroy.tfstate