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.
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.
Next install a Terraform extension for autocompletion and code highlighting. I went with the extension offered by the creators of Terraform, HashiCorp.
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;
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.
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.
And proceeded to copy and paste the AMI id into its respective line of code.
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
.
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.
Once completed, the terraform -help
command can be run to confirm the installation.
Use terraform init
to initialize the configuration within the terraform file, which in this case links to AWS on the backend.
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'.
Re-run the terraform plan
command and a list will appear.
Finally, run terraform apply
to execute the suggested actions.
Back in the AWS EC2 Console we can confirm the initialization of our newly provisioned Instance, which after a few moments will begin running.
Alternatively, the instance status can also be checked via the AWS CLI with the following command;
aws ec2 describe-instances
Top comments (0)