Example
The following code will provide a simple example. The first bit of code will create a VPC with one public subnet. We’ll have our declaration of using Terraform Remote State in that very same folder. We will also create an “output” to our Terraform Remote State that provides the “VPC ID” and “Public Subnet ID.”
The second bit of code will create an EC2 resource in the public subnet. It knows the VPC already deployed and subnet information to place the EC2 resource by using a data statement to get remote state information to get the VPC ID and public subnet ID.
Requirements
- Must have an AWS account
- Install AWS CLI, Configure AWS CLI, and Install Terraform on your computer or development environment
- Must create an S3 bucket that will hold Terraform Remote State before running the following code
- AWS Administrator account or an account with the following permissions:
- create, edit and delete S3 buckets
- create VPC, subnets, routing, and security groups
- create EC2 Instances and manage EC2 resources
Create the VPC
So for our example, we’ll need two folders. One folder is named VPC. The second folder is named “EC2”.
VPC.tf
Place the following code into the VPC folder.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
# ---------- Establish remote state -----------------
terraform {
backend "s3" {
bucket = "unique-name-terraform-states"
key = "example-terraform.tfstate"
region = "us-west-1"
}
}
# ---------- Region -----------------
provider "aws" {
region = "us-west-1"
}
data "aws_region" "current" {}
# ------------------ Create the VPC -----------------------
resource "aws_vpc" "my-vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
}
# ----------------- Internet Gateway -----------------------
resource "aws_internet_gateway" "test-igw" {
vpc_id = aws_vpc.my-vpc.id
}
# ------------------ Setup Route table to IGW -----------------
resource "aws_route_table" "public-route" {
vpc_id = aws_vpc.my-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.test-igw.id
}
}
# ******************** Public Subnet **********************
# --------------------- Public Subnet #1 -------------------
resource "aws_subnet" "public-1" {
vpc_id = aws_vpc.my-vpc.id
map_public_ip_on_launch = true
availability_zone = "us-west-1a"
cidr_block = "10.0.1.0/24"
}
# ----------- Associate route to IGW for public subnet #1 -----
resource "aws_route_table_association" "public-1-assoc" {
subnet_id = aws_subnet.public-1.id
route_table_id = aws_route_table.public-route.id
}
# ******************** OUTPUTS ********************************
output "aws_region" {
description = "AWS region"
value = data.aws_region.current.name
}
output "vpc_id" {
description = "VPC ID"
value = aws_vpc.my-vpc.id
}
output "public_subnet_1" {
description = "Public Subnet 1"
value = aws_subnet.public-1.id
}
EC2 resource
Now place the following code in a different folder.
EC2.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
#---------------- State terraform backend location-----
data "terraform_remote_state" "pull-backend-data" {
backend = "s3"
config = {
bucket = "unique-name-terraform-states"
key = "example-terraform.tfstate"
region = "us-west-1"
}
}
# ------------ Determine region from backend data --------------
provider "aws" {
region = data.terraform_remote_state.pull-backend-data.outputs.aws_region
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
# Creating controller node
resource "aws_instance" "controller" {
ami = data.aws_ami.amazon_linux.id
instance_type = "t2.micro"
subnet_id = data.terraform_remote_state.pull-backend-data.outputs.public_subnet_1
}
An S3 bucket must exist before launching each of the codes above.
Be sure to edit the parameters in each of the above files, and change the bucket, key, and region!
In your terminal, go to the VPC folder and execute the following commands:
Terraform initterraform validateTerraform apply
Once the VPC is up and running, go to the EC2 resource folder and execute the same commands as shown above. I you successfully created an S3 bucket prior to using the above code and correctly renamed the parameters appropriate for your region, you should have an EC2 resource created from modular code.
One thought on “An example of creating and implementing modular code with Terraform”