| by Arround The Web | No comments

How to Deploy Terraform in AWS EC2 Instance?

Terraform is an IAC (Infrastructure-as-code) platform developed by Hashicorp, mostly preferred by DevOps for the configurations of Cloud resources and services, as it is a command line interface supported by AWS.

This post will teach how to deploy Terraform in AWS. As Terraform will be deployed on EC2 Instance in AWS, this post will also provide you with the procedure for Creating an EC2 instance and deploying Terraform on it.

Note: Create an EC2 instance, if you don’t have one already, else skip this step.

Create an EC2 Instance (Optional)

In the EC2 dashboard, open the “Launch instance” drop-down list, and select “Launch instance”:

Name” the EC2 instance:

Select “Ubuntu” as an Amazon Machine Image:

Either select a key pair or create new one by clicking on “Create a new key pair”:

Leave the rest of the settings as default and press the “Launch instance” button:

You will get a success message upon the creation of an EC2 instance, click on the “Connect to instance” button:

Select the “SSH” tab and copy the SSH command:

Go to Command Prompt or PowerShell and paste the SSH command, type “yes” when you will get a continuity message:

In the output above, it is visible that you have successfully logged in to the Ubuntu AMI using this SSH Client.

Deploy Terraform in AWS

Before installing Terraform install its dependencies and add its GPG key and repository.

Type this update command in Ubuntu:

$ sudo apt-get update

After the successful installation, install a package using this command, to manage the repositories that are installed from common:

$ sudo apt-get install -y gnupg software-properties-common

The next step is to add Hashicorp GPG using this command:

$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

After that add Hashicorp repository, by typing:

$ sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Run the update command to refresh the list of system repositories:

$ sudo apt update

Install the Terraform by typing:

$ sudo apt install terraform

After the successful installation, To get help about Terraform type:

$ terraform -help

For detailed help, use this command:

$ terraform -help plan

To install the auto-completion package, first ensure that the “bashrc” file exists by typing:

$ touch ~/.bashrc

Type this command to install the auto-complete package for Terraform:

$ terraform -install-autocomplete

To initialize the working directory type:

$ terraform init

In the output above, you can see that currently, the working directory is empty, create configuration files for creating, and managing and deploying your Cloud resources and services with a file extension “.tf”.

After creating the configuration file, you can run it by typing:

> terraform apply

To stop the Terraform container, type:

> terraform destroy

You have successfully deployed Terraform in AWS, and have learned a few commands of Terraform.

Conclusion

To deploy Terraform in AWS, create an EC2 instance with Ubuntu AMI and login into it using the SSH Client. Install the common package and add the GPG key and repository of Hashicorp. After that install Terraform by typing “sudo apt install terraform”. On the successful deployment of Terraform, create and run your cloud configurations securely.

Share Button

Source: linuxhint.com

Leave a Reply