Setting Up AWS Credentials
Before provisioning, configure AWS access:
# Option 1: Environment variables
export AWS_ACCESS_KEY_ID="your-key"
export AWS_SECRET_ACCESS_KEY="your-secret"
# Option 2: AWS CLI profile
aws configure --profile terraformprovider "aws" {
region = "eu-west-1"
profile = "terraform"
}Creating a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "eu-west-1a"
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
}Master this topic with hands-on labs
Go beyond reading — build real projects in sandboxed environments with expert video guidance.
Browse Courses →Launching an EC2 Instance
resource "aws_security_group" "web" {
name = "web-sg"
description = "Allow HTTP and SSH"
vpc_id = aws_vpc.main.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["YOUR_IP/32"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
vpc_security_group_ids = [aws_security_group.web.id]
tags = {
Name = "web-server"
}
}Creating an S3 Bucket
resource "aws_s3_bucket" "assets" {
bucket = "myapp-assets-${random_id.bucket.hex}"
}
resource "aws_s3_bucket_versioning" "assets" {
bucket = aws_s3_bucket.assets.id
versioning_configuration {
status = "Enabled"
}
}
resource "random_id" "bucket" {
byte_length = 4
}Deploying
terraform init
terraform plan -out=tfplan
terraform apply tfplanGet weekly IT automation tips
Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.
Subscribe Free →Outputs
output "web_public_ip" {
value = aws_instance.web.public_ip
}
output "bucket_name" {
value = aws_s3_bucket.assets.bucket
}Clean Up
terraform destroyLearn More
Deploy real AWS infrastructure hands-on in our Terraform for Beginners course.
---
Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.
Ready to learn by doing?
Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.
Related Articles
Terraform AWS Exercises for Practice
Hands-on Terraform AWS exercises from beginner to advanced. Practice EC2, S3, VPC, RDS provisioning with real infrastructure.
Terraform Course: What You Learn
What a Terraform course teaches you. Skills, exercises, AWS provisioning, and career benefits of learning Terraform for IaC.
Terraform State Management Guide
Master Terraform state management. Remote backends, state locking, import commands, state moves, and disaster recovery procedures.
Terraform Backend Configuration
Configure Terraform remote backends. S3 with DynamoDB locking, Azure Blob, GCS, Terraform Cloud, and state migration steps.
Terraform for Beginners Guide
Everything you need to know to start using Terraform for Infrastructure as Code. From installation to your first deployment on AWS.
Terraform CDK vs HCL Comparison
Terraform CDK lets you write infrastructure in TypeScript, Python, or Go instead of HCL. Compare CDKTF and HCL for real-world use cases and learn when each.
Explore topics
Browse more articles on the topics covered here.