Infrastructure as Code with Terraform

Hiransanjeewaa
2 min readAug 4, 2023

--

In this blog article we will look at what is infrastructure as a code and what is Terraform . Also, we will create an EC2 instance using Terraform .

Infrastructure as a code allows us to manage Infrastructure using configuration files . So, we don’t need to depend on the graphical user interfaces. This mechanism solved many problems in Infrastructure management. Cloud providers offer APIs to interact with the cloud resources, but these APIs are different from cloud provider to cloud provider . What Terraform does is it act as a intermediate between cloud provider and us. It uses the concept called ‘APIs as a code’ . For us it is Infrastructure as a Code tool. There are Some key advantages of using Terraform.

1. Terraform can manage multiple cloud provider’s same time.

2. When migrating from one cloud provider to another we only need to do minimum changes in the configuration’s files. Same language with template can be used.

3. Terraform’s state file allows you to track resource changes throughout your deployments.

4. We can move our terraform script file in a GitHub repository. And work collaboratively. But we need to store the state file in a remote backend.

More importantly we can automate the infrastructure management using terraform with CI/CD tools. Let’s Create an EC2 instance using terraform. To do that we need to install terraform on our working machine. And we need to install AWS CLI and and next set up configuration using ‘ aws configure ‘ to do that we need to create AWS access key in AWS security credentials. Run ‘aws configure list’ to check if AWS configuration is successfully and ‘terraform -v ’ to ensure terraform installed perfectly.

How to install terraform , How to install and configure AWS Cli

Create folder called Terraform , and inside the folder create file called main.tf

terraform{

required_providers { // can have multiple providers
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">=1.2.0"
}

# Configure the AWS Provider , By default it takes us-east-1
provider "aws" {
region = "us-east-1"
}


# Create a VPC
resource "aws_instance" "example_server" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"

tags = {
Name = "First_Terraform_EC2"
}
}

Run terraform init → terraform plan -> terraform apply (Terraform lifecycle), three commands one by one , got to AWS console check if ec2 instance is created.

you can run ‘ls’ command and you will find another file called ‘terraform.tfstate’ which holds all the data of the infrastructure . This file by default will not be commited to github repo , since it has too much sensitive information . It should be stored in a remote backend such as S3 Bucket with DynamoDb to lock the file when work collaboratively. Thank you for reading.

--

--

Hiransanjeewaa
Hiransanjeewaa

Written by Hiransanjeewaa

Software Engineering Undergraduate — University of Kelaniya

No responses yet