Infrastructure as Code (IaC) With Terraform
Infrastructure as Code (IaC) With Terraform
Verify Install:
Step 2.
Create dir (mkdir casestudy_terraform) Initialize provider plugins using command terraform init and the below file (See
below Linux screenshot)
provider "aws"{
region = "us-east-2"
}
Marianne Gleason Page 2
Step 3.
Add IAM Role and get access key ID and secret access key (I had demonstrated how to create an IAM Role is a previous
post)
User: terraform
Provide AdministratorAccess
Here you will see AWS, Azure, Google Cloud Platform, Kubernetes, Alibaba, and Oracle Cloud Infrastructure.
For the purposes of this project select on AWS then click on Documentation. Here you will see a resource page and AWS
document for AWS services.
We are now creating a resource called an AWS EC2 instance. If you remember when creating a EC2 instance manually
we need the following as inputs: Choose AMI (Amazon Machine Image), instance type, configure settings, add storage,
add tags, and configure security group. That’s a lot of work. Below I will show you how you can incorporate all of this
into a terraform script.
So to do this edit main.tf and include the below HCL (HashiCorp Configuration Language) code by using the command:
ami = "ami-0960ab670c8bb45f3"
instance_type = "t2.micro"
tags = {
"Name" = "terraform-instance"
terraform apply – this will apply all of your changes in the script and an AWS instance was successfully created
#!/bin/sh
sudo apt-get update
sudo apt-get -y install apache2
provider "aws"{
region = "us-east-2"
access_key = "AKIA6DAOEEIAJEXPTS7B"
secret_key = "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"
}
ami = "ami-0960ab670c8bb45f3"
instance_type = "t2.micro"
tags = {
"Name" = "terraform-instance"
}
user_data = file("./install.sh")
--------------------------------------------------------------------------------------------------------------------
Step 7.
provider "aws"{
region = "us-east-2"
access_key = "AKIA6DAOEEIAJEXPTS7B"
secret_key = "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"
}
# declare a VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "My VPC"
}
}
terraform plan
terraform apply
Step 8.
tags = {
Name = "Public Subnet1"
}
}
tags = {
Name = "Public Subnet2"
}
}
Run Commands:
terraform plan
terraform apply
Step 9.
tags = {
Name = "My VPC - Internet Gateway"
}
}
Run Commands:
terraform plan
terraform apply
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_vpc_igw.id
}
tags = {
Name = "Public Subnet Route Table"
}
}
Run Commands:
terraform plan
terraform apply
Run Commands:
terraform plan
terraform apply
Output: Subnet Associations were created successfully and associations were made with subnets.
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port =0
tags = {
Name = "allow_ssh_sg"
}
}
Run Commands:
terraform plan
terraform apply
Step 13.
tags = {
Name = "My Instance1"
}
}
tags = {
Name = "My Instance2"
}
}
Run Commands:
terraform plan
terraform apply
Now validate that your site is up and running correctly by using the Public IP address (3.25.43.237) of your Bastion Host,
EC2 instance, terraform.
Step 15.
Now instead of deleting all of your resources by hand, use the below command in the command line and all resources
will be deleted.
Command:
terraform destroy
Project was completed successfully, deployed at a much faster pace with one script versus creating all the resources
manually, and all resources were deleted successfully as expected.
Note: I could, as well, have created a variables file to pass the access keys and the secret keys. This is a preferred
method and a best practice, but I wanted to illustrate how the keys were being used in the code.