Automating AWS-EC2 via Terraform

AWS-EC2 Creation using Terraform

Using terraform to provision AWS Elastic Cloud Compute instances is a quiet simple process to follow. In this example we will be creating an EC2 instance using our terraform configuration. We will be creating a very simple and basic EC2 instance using our example.

  1. Setup AWS Command Line Inerface (CLI)
$ terraform --version
terraform version
# Create a new instance of the latest Ubuntu 20.04 on an
# t2.micro node with an AWS Tag naming it "HelloWorld"
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
# Setting up the EC2 instace named as testingweb
resource "aws_instance" "testingweb" {
ami = "ami-0dba2cb6798deb6d8"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
  • provider “aws”, this is to set the service provider, you are using. So here we are setting up EC2, so we have write aws here.
  • resource “aws_instance” “testingweb” resource is to tell what service of AWS we are using, here we are using EC2 which is an AWS Instance so aws_instance would be hardcoded and testingweb is the name we have assign to it.
terraform init
terraform init
terraform validate
terraform validate
terraform plan
terraform plan
terraform apply
terraform apply

Further Options to Explore

We have used very minimalist options in our terraform configuration filein order to just run the EC2 instance. There are alot of options for an EC2 instance which we can control and modify using our terraform configuration like managin security groups, applying roles, controlling the AMI, hardware configuration etc etc.

AWS EC2 terraform options

In a Nutshell

Concluding the thing, terraform is indeed a very powerful provisioning tool through which one can easily control, manage and update the whole infrastructure and cloud system using few simple configuration files.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Syed Saad Ahmed

Syed Saad Ahmed

Python, DevOps, Cryptography, Infra-Structure Automation. https://syedsaadahmed.com/