Install Terraform and deploy an EC2 instance on AWS with code
Published: 2022-11-02 21:37:30

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujrhlybh7ve4wgh9fxzc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udhj87foiixxx3s8vl4k.png)
 

Next install a Terraform extension for autocompletion and code highlighting. I went with the extension offered by the creators of Terraform, HashiCorp.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z497bo0dtbgkk0qk2yzq.png)

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=
```

Or directly hardcoded into your script as such;
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyhq3k7mmujteugh5x9o.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep7j7z0yx6ts94npbk7e.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zi82p60veutj2h2wpjj.png)

And proceeded to copy and paste the AMI id into its respective line of code.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d71h8vnc8ol4pu7skx1j.png)

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
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k43zcd4ax3ivrzeb0c86.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r449tkzf280gt84wpav.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ovnxggnmdp87gchviny3.png)

Use `terraform init` to initialize the configuration within the terraform file, which in this case links to AWS on the backend.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2eo1sy6sbtx8eqjakl6u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f5l431stmmt1utk2mt0.png)

Re-run the `terraform plan` command and a list will appear.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn0r716dbemyl1s7lvkw.png)

Finally, run `terraform apply` to execute the suggested actions.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob625alfkt30wv29j1rc.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ceak3pxcp4q2kftp0l5a.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3curea9m4jkw21xlu05a.png)