Creating EC2 Instance using Terraform

In this article we are going to cover Creating EC2 Instance using Terraform, connecting to ec2 instance and destroy the resources.

Prerequisites

  • AWS Account
  • Create IAM User with Admin Access
  • Preinstalled Terraform
  • Create new Key pairs or use existing

Follow this link to create free aws account

Follow below article to create IAM user in AWS and allow admin access

How to Create IAM User in AWS Step by Step and setup IAM Policy to user

Install Terraform on Linux

How to Install Terraform on Ubuntu 20.04/18.04/16.04 LTS

Creating EC2 Instance using Terraform using minimum variable configuration

once all above prerequisites done then create a folder/directory

sudo mkdir ec2instance

change the folder/directory permission

sudo chmod 777 -R ec2instance

Navigate to ec2instance directory

cd ec2instance

then create ec2demo.tf file

sudo nano ec2demo.tf

paste the below configuration into it

provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
  region     = "ap-south-1"
}

resource "aws_instance" "Demo" {
  ami           = "ami-0d758c1134823146a"
  instance_type = "t2.micro"
  key_name = "key_pair_name"
  tags = {
    Name = "Demo"
  }
}

Replace the ACCESS_KEY, SECRET_KEY, ami , instance type, key_name and Instance Name value.

Initialize the working directory for terraform using below command

terraform init

Check the execution plan before applying

terraform plan

Now build the infrastructure and Type “yes” to confirm, it will create ec2 instance in Default VPC, subnets and security groups given by AWS account.

terraform apply

you will see instance is created in AWS

creating aws ec2 instance using terraform

copy the public address , select key pair name , open the SSH port (22) in security group if not enabled.

Creating EC2 Instance using Terraform 1

Connect to EC2 instance using putty

Creating EC2 Instance using Terraform 2

To destroy the resource use below command and Type “Yes

terraform destroy

To destroy the resources without confirmation use below command

terraform destroy -auto-approve

Error: Unsupported block type

Blocks of type “tags” are not expected here. Did you mean to define argument

“tags”? If so, use the equals sign to assign it a value.

Solution:

tags = {
    Name = "value"
  }

Conclusion:

We have covered Creating EC2 Instance using Terraform, connecting to ec2 instance and destroy the resources.

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

5 thoughts on “Creating EC2 Instance using Terraform”

  1. getting error >>with provider[“registry.terraform.io/hashicorp/aws”],
    │ on main.tf line 1, in provider “aws”:
    │ 1: provider “aws” {

    terraform veriosn is Terraform v1.2.4
    on windows_amd64
    + provider registry.terraform.io/hashicorp/aws v4.21.0

    please suggest what to do

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap