Use Terraform to launch ec2 instance and deploy web application on top of Amazon AWS

In this topic, i will go through how to automate a deployment of web application on amazon AWS. To achieve this goal, i will explain you from very zero the process of written terraform configuration file.

At the end, you will be able to write your own terraform script to launch ec2 instance, install and configure a web server and connect with your browser.

My plan is to:

  • Create a security group that allow port 80
  • Create a public key
  • launch an Amazon ec2 instance
  • install apache web server

Before go in too much in details, let’s start to understand some of the key concept.

First question come in mind, what is terraform ?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

For more details, go and check the official documentation

What’s the process to launch ec2 instance on amazon aws ?

To launch amazon ami (Amazon Machine Instance), we have to follows this step:

Step 1: Create an amazon aws account and sign in

Step 2: Go to services, choose ec2 or with “alt+S” search for ec2

Step 3: From amazon ec2 dashboard, choose launch instance

Follow the different step to launch an amazon ec2 instance.

❗️ ❗️ Note: In this blogs we have to automate this process by using terraform.

Download and install terraform

To install terraform, find appropriate package for you system and download it as a zip archive.

We are going to install terraform on windows machine. To perform installation follows this :

curl -O

After download completed, unzip file and make sure terraform is available on PATH.

Verified terraform is successful install by using this command:

terraform --version 

output will be:

Download and install aws cli

For amazon aws-cli installation, follow this links to download latest version of aws cli:

After installation complete, make sure aws cli are install successfully

aws --version

😍 We have successful install terraform and aws cli 😍

Now, we’ll configure an amazon IAM to interact with AWS. Go to your cmd and execute this command

aws configure

output will be:

All setup need are successful done, let’s jump to build our infrastructure


  • create a working directory

Each terraform configuration must be in its own working directory. Create directory for your configuration.

mkdir terraform-workstation
  • change into the directory
cd terraform-workstation

Create an ec2 instance using Terraform

I will use notepad on windows to write my code:

  • create a provider file

In provider file, we specified the provider. In this case aws. A provider is a plugin that terraform uses to create and manage resource.

Use command “notepad” to create “” file:


Paste the following line:

provider "aws" {
region ="ap-south-1"
profile ="default"

Region = amazon aws region

default = amazon IAM profile

  • create an ec2 terraform file

On same directory, create an ec2 terraform file that will be use to launch an amazon ami.


Paste following line

resource "aws_instance" "instance"{
ami = "ami-010aff33ed5991201"
instance_type = "t2.micro"
security_groups = ["webport-allow"]
key_name = "terraform-key"
tags = {
Name = "Web server by TerraForm"
output "my-public-ip"{
value= aws_instance.instance.public_ip

ami = AMI use for the instance, in this case we are using amazon linux ami

instance_type = Type of instance to be used

Security_goups = Name of security group or security group ID

Key_name = Key name of the key Pair to use for the instance, which can be managed using aws_key_pair resource

⚠️Noticed: Security group (webport-allow) and public key are already create on amazon management console. ⚠️

  • Web server installation

Before install any package on our ec2 instance, we have to connect by ssh remotely and install all package need by using provisionner resource .

let’s create a file call “”


In this file paste following line.

resource "null_resource" "remote"{connection {
type = "ssh"
user = "ec2-user"
private_key = file("F:/terraform-workstation/terraform-key.pem")
host = aws_instance.instance.public_ip
provisioner "remote-exec" {
inline = [
"sudo yum install httpd -y",
"sudo yum install git -y",
"sudo systemctl enable httpd",
"sudo git clone /var/www/html/web/",
"sudo systemctl start httpd"

For more details refers to terraform official documentation.

Before run all terraform file, on our amazon ec2 dashboard there is no image launch

  • Run terraform script

We haveto follow some steps:

Initialize the directory

Initialize configuration directory by downloading and install the provides defined in the configuration, which in this case is the aws provider.

❗️ ❗️ make sure you are in same directory create in above and execute this command ❗️ ❗️

terraform init

output will be:

Let’s verified, content of working directory

Like mentioned after successful initialization, try to run “terraform plan” to see if any changes that are required.

terraform plan


Run “Terraform apply” to execute all terraform file

terraform apply

output will be

😍 ☺ Terraform configuration file run ️successful 😍

Test connectivity to web server


Verified that ec2 instance is launch :

Hope you all have enjoy this topic!!!!!!!!!!

Patrice and let me know in comment if any doubt

Cloud / Cybersecurity enthusiast. CehV10, RHCSA, RHCE, Comptia Sec +, ITF +, AWS Architect