Deploy Your First Cloud Infrastructure in 15 Minutes
Learn how to use Terraform to create and manage your first cloud infrastructure quickly and efficiently. A practical guide for developers and DevOps engineers who want to master Infrastructure as Code.
In modern development, the ability to create and manage cloud infrastructures programmatically has become a fundamental skill. Terraform, an open-source tool by HashiCorp, allows you to define and provision cloud resources through declarative configuration files, making infrastructure reproducible, versionable, and easily manageable.
This terraform quickstart will guide you through the essential steps to deploy your first cloud infrastructure, from the basics to having a working environment in just 15 minutes.
What is Terraform and Why Use It
Terraform is an Infrastructure as Code (IaC) tool that allows you to define cloud resources and infrastructures through declarative configuration files. Unlike traditional imperative approaches, where you specify exactly how to create each resource, Terraform lets you describe the desired state of your infrastructure.
Main Advantages of Terraform
- Multi-Cloud Support: Supports over 1000 different providers, from AWS to Azure, Google Cloud, and many others
- State Management: Tracks infrastructure state in state files
- Plan and Apply: Shows changes before applying them
- Reusability: Ability to create reusable modules
- Collaboration: Integration with version control systems
Environment Preparation
Installing Terraform
Before starting this terraform quickstart, you need to install Terraform on your system. The process varies depending on the operating system:
For macOS (using Homebrew):
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
For Ubuntu/Debian:
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
For Windows:
Download the binary from the official HashiCorp website and add it to your system PATH.
Configuring Cloud Credentials
For this tutorial we will use AWS as provider. Make sure you have:
- An active AWS account
- AWS CLI installed and configured
- AWS credentials configured (Access Key and Secret Key)
Configure AWS credentials:
aws configure
Creating Your First Terraform Configuration
Project Structure
Let's start by creating a directory for our terraform quickstart project:
mkdir terraform-quickstart
cd terraform-quickstart
Main Configuration File
Create a file called main.tf with the following base configuration:
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.2"
}
# Configure the AWS Provider
provider "aws" {
region = var.aws_region
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "terraform-quickstart-vpc"
}
}
# Create an Internet Gateway
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "terraform-quickstart-igw"
}
}
# Create a public subnet
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true
tags = {
Name = "terraform-quickstart-public-subnet"
}
}
# Get availability zones
data "aws_availability_zones" "available" {
state = "available"
}
Variables File
Create a variables.tf file to define variables:
variable "aws_region" {
description = "AWS region"
type = string
default = "us-west-2"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
variable "key_name" {
description = "AWS key pair name"
type = string
default = ""
}
Outputs File
Create an outputs.tf file to define outputs:
output "vpc_id" {
description = "ID of the VPC"
value = aws_vpc.main.id
}
output "public_subnet_id" {
description = "ID of the public subnet"
value = aws_subnet.public.id
}
output "internet_gateway_id" {
description = "ID of the Internet Gateway"
value = aws_internet_gateway.main.id
}
Adding Compute Resources
Creating an EC2 Instance
Add the following code to the main.tf file to create an EC2 instance:
# Create a route table
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "terraform-quickstart-public-rt"
}
}
# Associate route table with public subnet
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
# Create a security group
resource "aws_security_group" "web" {
name_prefix = "terraform-quickstart-web"
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 = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "terraform-quickstart-web-sg"
}
}
# Get the latest Amazon Linux AMI
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
# Create an EC2 instance
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
subnet_id = aws_subnet.public.id
vpc_security_group_ids = [aws_security_group.web.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello from Terraform!
" > /var/www/html/index.html
EOF
tags = {
Name = "terraform-quickstart-web"
}
}
Deploying the Infrastructure
Initializing Terraform
The first step to use this terraform quickstart is to initialize the project:
terraform init
This command downloads the necessary providers and prepares the working environment.
Validating the Configuration
Verify that the configuration is syntactically correct:
terraform validate
Planning the Deploy
Before applying changes, it's always good practice to visualize what will be created:
terraform plan
Terraform will show a detailed summary of all resources that will be created, modified, or deleted.
Applying the Configuration
Proceed with the actual deploy:
terraform apply
Terraform will ask for confirmation before proceeding. Type "yes" to continue.
Verifying and Testing the Infrastructure
Checking Outputs
Once the deploy is complete, Terraform will show the defined outputs. You can view them again with:
terraform output
Verifying Created Resources
You can verify that resources were created correctly:
- Access the AWS console
- Navigate to the EC2 service
- Verify that the instance is in "running" state
- Copy the public IP address and test it in your browser
State Management
Terraform maintains infrastructure state in the terraform.tfstate file. This file is crucial for Terraform operation and must be handled carefully in production environments.
Terraform Best Practices
Code Organization
For more complex projects, consider this structure:
terraform-project/
├── modules/
│ ├── vpc/
│ ├── ec2/
│ └── security/
├── environments/
│ ├── dev/
│ ├── staging/
│ └── production/
└── shared/
Remote State Management
In production environments, always use a remote backend for state:
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "infrastructure/terraform.tfstate"
region = "us-west-2"
}
}
Security and Compliance
- Never commit credentials in configuration files
- Use environment variables or secrets management systems
- Implement security policies through tools like Sentinel
- Regularly run security scans on Terraform code
Cleanup and Resource Destruction
When you no longer need the infrastructure created in this terraform quickstart, you can remove all resources with a single command:
terraform destroy
Terraform will show a destruction plan and ask for confirmation before proceeding. This is particularly useful for test and development environments to avoid unnecessary costs.
Conclusions
This terraform quickstart has provided you with the basics to start using Terraform for cloud infrastructure management. In just 15 minutes you learned to:
- Install and configure Terraform
- Create configuration files for AWS resources
- Deploy a complete infrastructure with VPC, subnet, security group, and EC2 instance
- Manage infrastructure lifecycle
- Apply fundamental best practices
Terraform is a powerful tool that becomes even more valuable when used in teams and for complex projects. The skills acquired in this tutorial form a solid foundation for exploring more advanced features such as modules, workspaces, and CI/CD pipeline integration.
The natural next step is to explore creating reusable modules and implementing more advanced DevOps practices. Always remember to test your configurations in a development environment before applying them to production.