AWS EC2 Instance Creation Using Terraform


The blog will help you to learn how to launch an EC2 Instance with the help of Terraform.

Terraform is an infrastructure as a code (Iac) tool that allows you to create, change, and keep infrastructure compliance. Terraform, write code once, and use it many times. If You need a quick and efficient way to automate infrastructure in AWS, you can use Terraform.​​​​​​​


  • AWS Account
  • Terraform installation (Once Terraform is installed, we will configure aws IAM credentials in order to use with AWS)
  1. You can download the Terraform from the Hashi corp website.
  2. To verify installation, run “terraform -help.”
  3. To check Version, run “terraform -V”
  • Code editor (I am using visual studio editor. you can use any editor)

Create EC2 Instance

After successfully installing the terraform, we will create an EC2 instance using Terraform, and we will use the VS code editor to write the script.


  • Open VS code editor and add the Terraform (HashiCorp) extension.
  • Add extension step of Terraform, perform once when you install the VS code editor.


  • Create a project folder and open it in VS code editor.
  • Create a file with a .tf extension.


Provider: Terraform relies on plugins called “providers” to interact with remote systems. Terraform configurations must declare which providers they require so that Terraform can install and use them.

  • Provide the provider information (we are using AWS).

Required Providers: Each Terraform module must declare which providers it requires so that terraform can install and use them.

  • Source – the global source address for the provider you intend to use, such as hashicorp/aws.
  • Version – This specifies which subset of available provider versions the module is compatible with.
					terraform {
 required_providers {
     aws = {
       source  = "hashicorp/aws"
       version = "~> 2.70"

AWS Provider: The Amazon Web Services provider is used to interact with the many resources supported by AWS. The AWS provider needs to be configured with the proper credentials before it can be used.

Static stuff can be provided by adding an access_key and secret_key in-line in the AWS provider block.

					provider "aws" {
 region     = "us-west-2"
 access_key = "my-access-key"
 secret_key = "my-secret-key"

It tells the Terraform that you will be using AWS as your provider and that you want to deploy your setup into the region “us-west-2”.

For every type of provider, there are many different kinds of resources that you can create, such as databases, security groups, instances, VPC and load balancers, etc.


The syntax for creating a resource in Terraform is:

					resource "_" "" {

Resource: The most important element in the Terraform language is the resource. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or higher-level components, such as DNS records.

Provider_type: It is the aws resource type to create in the provider, e.g., “aws_instance.”

Name: It is the aws resource name, e.g., instance name: “example.”

Configuration: Configuration consists of one or more arguments that are specific to the resource.

For creating the EC2 instance, we need ami and instance type.


Now open the terminal of VS code editor or command prompt (cmd). Navigate to that path where you created the file and run the terraform init command.

This command is used to initialize a working directory containing the terraform configuration files. This is the first command that should be run after writing a new Terraform configuration.


After successfully running the “terraform init” command, Run the “terraform plan” command.

The terraform plan command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and determines what actions are necessary to achieve the desired state specified in the configuration files.

This command is an easy way to check whether the execution plan for a set of changes matches your expectations without creating any changes to real resources.
Anything with a sign “+” will be created, anything with a sign “–” will be deleted, and anything with a tilde sign “~” will be modified in place. In the preceding output. You can see that Terraform is creating a single EC2 Instance and nothing else, which is exactly what you want.


To create the EC2 Instance, run the command “terraform apply.”

This command is used to apply the changes required to reach the desired state of the configuration or the pre-determined set of actions generated by a terraform plan execution plan.


You will notice that the apply command shows your plan output and asks you to “Do you want to perform these actions?”.
Write yes and hit Enter to deploy the EC2 Instance.

Now you deployed an AWS EC2 Instance in your AWS account using Terraform. Let’s check whether it’s created or not. Open your AWS Console and go to the EC2 dashboard.


By following this article, you will be able to create your first EC2 Instance creation using Terraform.