As cloud infrastructure grows in complexity, the need for infrastructure automation tools has never been higher. Terraform, developed by HashiCorp, is a front-runner in this realm, enabling users to define and provision infrastructure using a declarative configuration language. In this blog post, we'll guide you through setting up Terraform on an AWS EC2 Ubuntu instance.
Prerequisites:
An active AWS account.
Basic understanding of AWS EC2.
Familiarity with SSH.
1. Launching an AWS EC2 Ubuntu Instance
Login to AWS Management Console and navigate to the EC2 dashboard.
Launch a new instance by selecting the "Launch Instance" button.
In the AMI selection step, choose Ubuntu Server.
Proceed with your preferred instance type t2.micro, configure instance details default, add storage 8GB, and configure security groups default. Ensure your security group allows SSH-22 access.
Review and launch your instance. Make sure to select an existing key pair or create a new one, which you'll use for SSH access.
2. Connecting to the EC2 Instance
Using your terminal or SSH client, connect to your instance:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip-address
3. Installing Terraform
Once connected to your EC2 instance, follow these steps to install Terraform:
Update and Upgrade your machine:
sudo apt-get update && sudo apt-get upgrade -y
Download Terraform: Fetch the latest version of Terraform for Linux from click-here or use
wget
:sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
Install the HashiCorp GPG key:
wget -O- https://apt.releases.hashicorp.com/gpg | \ gpg --dearmor | \ sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
Verify the key's fingerprint:
gpg --no-default-keyring \ --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \ --fingerprint
Add the official HashiCorp repository to your system. The
lsb_release -cs
command finds the distribution release codename for your current system, such asbuster
,groovy
, orsid
.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
Download the package information from HashiCorp.
sudo apt update -y
Install Terraform from the new repository.
sudo apt-get install terraform
Verify the installation
terraform --version
4. Configuring AWS CLI (Optional)
To make API requests to AWS services, Terraform requires authentication. One method is using the AWS CLI:
Install AWS CLI:
sudo apt install awscli -y
Configure AWS CLI with your credentials:
aws configure
This command will prompt you to enter your AWS Access Key ID, Secret Access Key, default region, and output format. These credentials will be used by Terraform when interacting with AWS.
5. Running Your First Terraform Script
Create a directory called terraform where we will be configuring and running terraform scripts
mkdir terraform
Start Writing your terraform script
Create a file named
main.tf
and add a simple AWS S3 bucket configuration:provider "aws" { region = "us-east-1" } resource "aws_s3_bucket" "my_bucket" { bucket = "my-bucket-from-terraform" acl = "private" }
Initialize Terraform:
The command
terraform init
initializes a working directory containing Terraform configuration files, setting up the necessary backend and downloading required provider plugins.terraform init
Plan the deployment:
terraform plan
is a Terraform command that generates and displays an execution plan, showing what actions Terraform will take to apply your configuration, without actually making any changes to the infrastructure.terraform plan
Apply the configuration:
terraform apply
reads the Terraform configuration files, determines the desired infrastructure state, compares it to the current state, and then makes the necessary changes to achieve that desired state.terraform apply
After confirmation, Terraform will create the specified S3 bucket.
6. Verify your s3 bucket creation!
Go to AWS Console and c=search for S3 service. Click on buckets and you will see the bucket that you have created from Terraform
7. terraform destroy
It is used to remove all resources that exist in the current Terraform state. When you run this command:
Basic Usage: Navigate to the directory containing your Terraform configuration files and run:
terraform destroy
Terraform will display a plan showing what will be destroyed and will prompt you for confirmation.
Auto-approve Flag: If you're sure about your destroy action and want to skip the manual confirmation step, you can use the -auto-approve
flag:
terraform destroy -auto-approve
Caution: Use this flag wisely, especially in production environments.
Targeted Destroy: If you only want to destroy specific resources rather than the entire infrastructure, you can use the -target
flag:
terraform destroy -target=aws_s3_bucket.my_bucket
This command targets only the specified resource (aws_s3_bucket.my_bucket
in this case) and resources dependent on it.
Conclusion:
Terraform offers a robust and versatile approach to infrastructure automation, and with it set up on an AWS EC2 instance, you can manage and scale your resources with ease. While this guide offers a basic introduction, the possibilities with Terraform are vast, so dive in and explore its powerful capabilities!