- Published on
Day 06: Installing Terraform and Working with Terraform Providers
- Authors
- Name
- Surya Harahap
- @suryaharahap18
Introduction
In this hands-on lab, we'll go through installing and configuring Terraform version 13 on a Linux OS. We will also explore how to select and use a Terraform provider from among the many providers available publicly.
Lab practice:
Log in to the lab server using the credentials provided:
ssh cloud_user@<PublicIP>
In a web browser, log in to the AWS Management Console using the credentials provided.
Download And Manually Install the Terraform Binary
Install the appropriate Terraform binary package for the provided lab server VM (Linux 64-bit) using the amazon linux
command:
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Check the Terraform version information:
terraform version
# OUTPUT:
# Terraform v1.7.5
# on darwin_amd64
Since the Terraform version is returned, you have validated that the Terraform binary is installed and working properly.
You can check this official instalation terraform
Clone Over Code for Terraform Providers
Create a providers directory:
mkdir providers
Move into the providers directory:
cd providers/
Create the file main.tf:
vim main.tf
Paste in the following code from the provided GitHub repo:
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
provider "aws" {
alias = "us-west-2"
region = "us-west-2"
}
resource "aws_sns_topic" "topic-us-east" {
provider = aws.us-east-1
name = "topic-us-east"
}
resource "aws_sns_topic" "topic-us-west" {
provider = aws.us-west-2
name = "topic-us-west"
}
To save and exit the file, press Escape and enter :wq
.
Deploy the Code with Terraform Apply
Enable verbose output logging for Terraform commands using TF_LOG=TRACE:
export TF_LOG=TRACE
Note: You can turn off verbose logging at any time using the export TF_LOG= command.
Initialize the working directory where the code is located:
terraform init
Review the actions performed when you deploy the Terraform code:
terraform plan
Note: Two resources will be created, consistent with the providers that were configured in the provided code snippet.
Deploy the code:
terraform apply
When prompted, type yes and press Enter.
Verify that two resources were created with their corresponding Amazon Resource Name (ARN) IDs in the region in which they were spun up.
Optionally, verify that the resources were created in their respective regions within the AWS Management Console:
- Navigate to the AWS Management Console in your browser.
- Verify that you are logged in to the us-east-1 region upon signing in.
- Click Services.
- Type SNS in the search bar and select Simple Notification Service from the contextual menu.
- In the menu on the left, click Topics.
- Verify that the topic-us-east resource appears in the list.
- At the top-right, click N. Virginia and select us-west-2.
- Verify that the topic-us-west resource appears in the list.
Tear down the infrastructure you just created before moving on:
terraform destroy --auto-approve