DEV Community

Cover image for Install Terraform and deploy an EC2 instance on AWS with code
R o r d a n ♨️
R o r d a n ♨️

Posted on • Edited on

Install Terraform and deploy an EC2 instance on AWS with code

Infrastructure as a Code is a common DevOps practice used to automate the provisioning of cloud resources. Terraform is a third-party open source software that enables deployment and connectivity to the vast majority of Cloud Service Providers.

Image description

To begin using Terraform, it's first necessary to create a terraform file within your Integrated Development Environment (IDE).

The touch command creates a file, it can be named anything as long as the suffix of '.tf' is appended to the end indicating it will be written in terraform. Then entering code before your file name will open the file onto your main workspace.
Image description

Next install a Terraform extension for autocompletion and code highlighting. I went with the extension offered by the creators of Terraform, HashiCorp.
Image description

There are two methods to provide authentication, your access key and secret access key from AWS can either be exported with the following commands via the AWS CLI;

export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
Enter fullscreen mode Exit fullscreen mode

Or directly hardcoded into your script as such;
Image description

From the Terraform documentation there is an example template listed under AWS which will use JSON code to enable automation for the creation of an ec2 instance.

Image description

The only change I made to this section of code is my Amazon Machine Image selection. Via the AWS AMI Catalog, which can be found under AWS EC2 in the console, I went with the latest Ubuntu image.
Image description

And proceeded to copy and paste the AMI id into its respective line of code.
Image description

Next, to install Terraform from your Linux CLI, use the following commands;

wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Enter fullscreen mode Exit fullscreen mode

Troubleshooting
If the installation does not work when all three commands are pasted at once, retry the installation line by line instead.

If there is an error message regarding a source list not being read, remove the hashicorp.list file with sudo rm /etc/apt/sources.list.d/hashicorp.list .

Image description

And if an issue regarding a missing package named lsb_release is shown, use sudo apt update && apt install -y lsb-release to download the necessary package.

Image description

Once completed, the terraform -help command can be run to confirm the installation.

Image description

Use terraform init to initialize the configuration within the terraform file, which in this case links to AWS on the backend.
Image description

Use terraform plan to check for changes to the configuration. If no suggestions are shown make sure that the file has been saved with 'ctrl s'.
Image description

Re-run the terraform plan command and a list will appear.
Image description

Finally, run terraform apply to execute the suggested actions.
Image description

Back in the AWS EC2 Console we can confirm the initialization of our newly provisioned Instance, which after a few moments will begin running.
Image description

Alternatively, the instance status can also be checked via the AWS CLI with the following command;
aws ec2 describe-instances

Image description

Top comments (0)