Building infrastructure for AWS using terraform

What is terraform?

Terraform is an open-source infrastructure as code, software tool created by HashiCorp. It enables users to define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Terraform supports all cloud providers.

In this article i will show how to build simple architecture in aws using terraform.

First install terraform on your system. Based on your Operating system download terrafrom.

Let’s start with ec2-creation. I am using ubuntu 18.04 as base OS and instance type t2.micro which falls under free-tier.

Ec2-resource creation:

resource "aws_instance" "MyFirstInstance"{
#add ami,instance,tag values to create instance
ami = "ami-02354e95b39ca8dec"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.allow_ssh.id}"]
subnet_id = "${aws_subnet.PublicSubnet.id}"
tags = {
Name = "MyEc2Instance"
}


Elastic ip creation:

resource "aws_eip" "EipForInstance"{ instance = "${aws_instance.MyFirstInstance.id}" vpc = true tags = { Name = "Ec2_instance_ip" }}

VPC Creation: Here for demo i have allowed all ips for ssh. Don’t follow this practice in dev/production server

resource "aws_vpc" "mainvpc" { cidr_block = "10.0.0.0/16" instance_tenancy = "default" enable_dns_hostnames = true
tags = { Name = "VPC_TF" }
}

You can find code in my repo . Kindly check the readme file for running terraform file.