AWS

How to Install Terraform in EC2

Terraform is an open-source tool mostly used by DevOps, as it provides the facility to build, manage, and update resources on cloud platforms by coding, AWS is an advanced network partner of Terraform. Terraform’s state allows its users to track the changes in their resources through the development process.

This post will provide a procedural guide to install Terraform in the EC2 instance, even including the steps to create an EC2 instance first.

Install Terraform in EC2 Instance

In the Amazon management console, search EC2 and open EC2 Dashboard, open the Launch instance drop-down list, and click on Launch instance:


In Launch an instance wizard, provide a name for this EC2 instance:


Select the Amazon Machine Image as per your preference, let’s select Ubuntu for this post:


Select an already existing key pair or create a new key pair:


Leave rest of the options as default and click on Launch instance button:


On the screen, a success message will appear after the successful creation of the EC2 instance, click on Connect to instance button:


Connect to instance wizard will open, go to SSH client tab and copy the provided SSH command:


Open the Command Prompt or PowerShell and paste the SSH command:


In the output above, it is visible that the EC2 is logged in successfully using the SSH Client.

Type this update command:

$ sudo apt-get update

 

The next step is to install a package to manage the repositories that are installed from common, install it by typing:

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

 

Add the key by typing:

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

 

After this, its time to add the repository of Terraform using this command:

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

 

Hit Enter when a command for continuity appears while adding the repository.

Again use the update command to refresh the list of repositories by typing:

$ sudo apt-get update

 

After the updation, the EC2 machine is ready for the installation of Terraform so install it by typing:

$ sudo apt install terraform

 

Once the installation process completes, check its version using the command:

$ terraform -v

 

In the output above, it is clear that installation of terraform was successful as it is showing an installed version.

Type this command to initialize the working directory of terraform:

$ terraform init

 

In the output above, it is visible that the working directory is initialized and does not have any configuration file now, you are ready to use your Terraform for configurations now.

Conclusion

If you are comfortable in the command line interface and coding, then terraform is a great choice to do cloud configuration using code. To install Terraform in EC2c create an EC2 instance and add the key and repository of terraform. Finally, use the install command to install Terraform and the init command to initialize its working directory, and you can start using Terraform for your cloud configurations.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.