Launching and Managing an Amazon EC2 Instance using Terraform

Launching and Managing an Amazon EC2 Instance using Terraform



06 May 2019

What is Terraform?

Terraform is a tool (IaC) for creating and managing infrastructure. it supports multiple cloud providers including Google Cloud and Amazon AWS. Here you will write configuration files which describe the infrastructure components. For Example an EC2 instance and the AWS VPC.

While creating the infrastructure Terraform generates an execution plan, which describes what it will do to build the desired infrastructure, then executes it to provide the described infrastructure.

 Amazon EC2 Instance using Terraform

Why Terraform?

  1. Share and Reuse Infrastructure:

    Infrastructure is described using the code. This allows your infrastructure configuration versioned and treated as you would any other code. On top of this Infrastructure can be reused and shared.
    Execution Plan: Before applying changes Terraform generates an execution plan which describes what Terraform will do when you call apply. This will help you to avoid any surprises when Terraform manipulates infrastructure.
    Resource Graph: Terraform generates a graph of all the resources in order to identify non-dependent resources, with the help of that graph Terraform builds infrastructure as efficiently as possible. Also, it helps operators to get insight into dependencies in their infrastructure.

  2. Automation:

    Complex changes can be automated and applied to your infrastructure with minimal human interaction. The execution plan and resource graph, let you know exactly what Terraform will change and in which order, avoiding many possible human errors.Here we are going to launch an EC2 instance using Terraform. Before jumping into creating an EC2 Instance let’s check the prerequisite.


  1. Install latest version of Terraform
  2. Read about Amazon EC2
  3. Access to AWS account (console)
  4. AWS Secret Key and AWS access key Id

Let’s get started with creating EC2 instance using Terraform.

Creating the project directory

Create a directory with a suitable name, I have created it with the name ec2-instance.
Create the following directory structure:


– .gitignore

Note: Directory structure is not mandatory, Terraform loads all the .tf files from the directory and decides what to launch or modify.

  • .gitignore

    If you are using git, you must start with .gitignore file and add files into it because we are going to add AWS secret. Keeping credentials file out of Git can keep them more secure and resistant to accidents.
    Please refer the following snippet which we are going to add in the .gitignore file.

# Compiled files

# Module directory

# Sensitive credential Files

    Here we’ll set some variable for AWS secret and we’ll re-use them in the configuration.

# AWS Config

variable "aws_access_key" {

variable "aws_secret_key" {
default = "5RbY2HnT956IrhwbLUsabL1z+Ud9brgvC+tjs1kQ"

variable "aws_region" {
default = "us-west-2"

    In the file, under the resources block we’ll write required configuration to launch an instance. The resultant file will look like as follows:

provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.aws_region}"

resource "aws_instance" "web_server" {
ami = "ami-061392db613a6357b"
instance_type = "t2.small"
tags {
Name = "my_server"

The Providers section is to configure the service provided. Providers are interfaces to the services that maintain infrastructure resources. For example – An EC2 Instance is a Resource provided by the AWS Provider. Since the Terraform supports multiple providers we can configure multiple providers. Here we are focusing only on AWS provider.

aws_instance is a TYPE of resource to be created. In our example it’s an AWS EC2 instance.

web_server is the NAME of a resource that is addressable within terraform configuration file.

Now, we are all set with the project directory, let’s run Terraform to create our instance.Creating the Infrastructure:

Open bash and navigate to the project’s directory to the “ec2-instance” by simply running the command cd ec2-instance

Now run terraform init command. This command will download and install the proper version of the AWS provider for your project and add it in the directory .terraform

You will see output something like this:

Installing an Amazon EC2 Instance using Terraform

Now run terraform plan to make sure configuration is ready to be applied.

Finally, we’ll run terraform apply the command to propagate our configuration to infrastructure. Before running this command we’ll check our AWS console.

Launching an Amazon EC2 Instance using Terraform

We don’t have any instance in our AWS console. Let’s run terraform apply. You will see out see output something like this:

Installing an Amazon EC2 Instance using Terraform

This command shows you what are the changes which are going to be applied to your infrastructure and it will prompt for your confirmation. Enter ‘yes’ when it prompts you for the confirmation.

Here we go, we have created our EC2 instance using terraform. Let’s go back on the AWS console to check the created instance.

Installing an Amazon EC2 Instance using Terraform

Also, you will see terraform.tfstate generated into your project directory. Terraform store a state of your infrastructure and configuration in terraform.tfstate file. Terraform uses this state to map real-world resources to your configuration and to keep track of the metadata.

Modify EC2 instance type

  1. In the file changes EC2 instance type from t2.micro to t2.small.
  2. Again run the terraform plan and then terraform apply to actually apply changes.

Amazon EC2 Instance using Terraform

Go back to AWS console and check changed instance type.

Amazon EC2 Instance using Terraform

Congratulations! You have created your EC2 instance and learned how to change it’s configuration.

Blog Categories
Request a quote