DEV Community

Palak Bhawsar
Palak Bhawsar

Posted on • Originally published at palak-bhawsar.hashnode.dev on

Launch an EC2 Instance using Terraform

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision infrastructure using a high-level configuration language known as HCL (Hashicorp Configuration Language). Terraform is an IAC tool, used primarily by DevOps teams to automate various infrastructure tasks. The provisioning of cloud resources, for instance, is one of the main use cases of Terraform. It's a cloud-agnostic, open-source provisioning tool written in the Go language and created by HashiCorp.

IAC or Infrastructure as Code enables us to manage infrastructure in the form of code. Using Terraform we can automate the process of provisioning and destroying infrastructure.

Let's get started. . .

1. Download Terraform on a Linux machine

Open your Linux terminal and run the following commands to Install Terraform.

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform

Enter fullscreen mode Exit fullscreen mode

Check the downloaded version of Terraform using the below command:

terraform version

Enter fullscreen mode Exit fullscreen mode

2. Create AWS User with permission

Create a User and give it permission to interact with AWS from the local machine. Login to the AWS account and in services search for IAM. In side pane select Users and click Add users , enter name of the user and check the box Access key - Programmatic access and click next:Permissions

image.png

Select Attach existing policies directly from the top menu and check AmazonEC2FullAccess and click Next: Tags , Review , and Add user.

image.png

Copy and keep the Access key ID and Secret access key safe for later use or Download the CSV file.

image.png

3. Configure AWS keys

Install AWS CLI using the below commands:

sudo apt install awscli


aws configure

Enter fullscreen mode Exit fullscreen mode

Enter the region and access key details when prompted:

AWS Access Key ID :
AWS Secret Access key:
Default region: us-east-1
Default Output: json

Enter fullscreen mode Exit fullscreen mode

Configure profile to store access key and secret access key. Cat config file located at path ~/.aws/

Edit the file and enter the access key and secret access key and give a name to profile

[default]
aws_access_key_id = <ACCESS_KEY_ID>
aws_secret_access_key = <SECRET_ACCESS_KEY>
region = us-east-1

Enter fullscreen mode Exit fullscreen mode

4. Create terraform configuration file

A Terraform configuration is a complete document in the Terraform language that tells Terraform how to manage a given collection of infrastructure. A configuration can consist of multiple files and directories. Create a configuration file with a .tf extension in any editor or IDE and save the file.

provider "aws" {
   region = "us-east-1"
   }

resource "aws_instance" "my_ec2_instance" {
  ami = "ami-08c40ec9ead489470"
  instance_type = "t2.micro"

  tags = {
    Name = "FirstEC2Instnace"
  }
}

Enter fullscreen mode Exit fullscreen mode

To format the configuration file run the below command in the terminal in the same directory where the .tf file is present.

terraform fmt

Enter fullscreen mode Exit fullscreen mode

Initialize terraform using the below command. It's the same as the git init command in GIT. This command will initialize the working directory containing Terraform configuration files and install any required plugins

terraform init

Enter fullscreen mode Exit fullscreen mode

To check that the configuration file is error-free, run the following command after terraform init:

terraform validate

Enter fullscreen mode Exit fullscreen mode

Run the below which lets you preview the actions Terraform would take to modify your infrastructure or save a speculative plan which you can apply later.

terraform plan

Enter fullscreen mode Exit fullscreen mode

Finally, run the below command. This is the same as git push. This command executes the actions proposed in a terraform plan. It is used to deploy your infrastructure. Typically apply should be run after terraform init and terraform plan. Enter yes when prompted "Enter a value:".

terraform apply

Enter fullscreen mode Exit fullscreen mode

You can see the instance is running in your AWS Console. You will see terraform.tfstate file is created after running the terraform apply command. This state is used by Terraform to map real-world resources to your configuration, keep track of metadata, and improve performance for large infrastructures. This state is stored by default in a local file named "terraform. tfstate", but it can also be stored remotely, which works better in a team environment.

5. Destroy infrastructure

To destroy the resources you have just created run the below command. The terraform destroy command terminates resources managed by your Terraform project. This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state. It does not destroy resources running elsewhere that are not managed by the current Terraform project.

terraform destroy

Enter fullscreen mode Exit fullscreen mode

Top comments (0)