Install Terraform on Ubuntu, Launching the ec2 instance and creating an s3 bucket

Terraform is an infrastructure as a code platform developed by HashiCorp. You can simply write code in human readable format following the HashiCorp Configuration Language (HCL) and deploy it to get the cloud infrastructure. Terraform is compatible with many cloud providers like Google, Amazon, Alibaba, etc.

Here in this article, we are going to install the latest version of terraform on Ubuntu on PC. We are performing the terraform installation on Ubuntu 20.04, however you can do the same procedure on all Linux platforms.
Also, learn how to use terraform with a simple example by launching an ec2 instance and creating an s3 bucket.
Install terraform on Ubuntu 20.04
Download the latest version of Terrafrom. At the time of writing this article, the latest version is 0.14.3.
$ wget https://releases.hashicorp.com/terraform/0.14.3/terraform_0.14.3_linux_amd64.zip
Now, unzip the download file.
$ sudo apt install zip -y 

$ sudo unzip terraform_0.14.3_linux_amd64.zip
This will give you a terraform file, just move it to /usr/local/bin/ to run the command.
$ sudo mv terraform /usr/local/bin/
look at this version
$ terraform version

How to use Terraform
Let's explain how to use terraform with basic examples.
Launching the ec2 instance and creating an s3 bucket
I would like to create a folder and do everything inside it.
$ mkdir aws && cd aws
Create a configuration file for terraform with extension 'tf'
vi configuration.tf
You must now provide the following information:
  • Provider: cloud provider, AWS in our case
  • Access, secret key: access to AWS resources
  • Region: The region where you are going to provision your infrastructure. I'm doing it in Oregon.
In the second block of code, define the AWS instance, i.e. ami (check amazon EC2 AMI Locator ), instance type, and tag.
The last part of the following code will create an s3 bucket called 'bucket-launched-using-terrafrom-20210106'. Remember that the bucket name must be unique within AWS.
Copy the following content pasted into the configuration.tf file. Provide access, secret key, region, own bucket name.
#Define keys and region 
provider "aws" {
access_key = "YOUR-ACCESS-KEY"
secret_key = "YOUR-SECRET-KEY"
region = "us-west-2"
} #Define ec2 instance
resource "aws_instance" "instance1" {
ami = "ami-089668cd321f3cf82"
instance_type = "t2.micro"
tags = {
Name = "ubuntu-20.04"
}
} #Define s3 bucket
resource "aws_s3_bucket" "bucket1" {
bucket = "bucket-launched-using-terrafrom-20210106 "
acl = "private" # or can be "public-read"
tags = {
Name = "Bucket"
Environment = "Production"
}
}
Now initialize, schedule, and run your code. Initializing terraform will do the necessary configuration, planning is like seeing what will happen today. You will also find your syntax error. And finally, apply means that you will deploy the code to the cloud. Let's run the following command one by one.
Terraform initialization will do the necessary configuration. So, run the following command,
$ terraform init
Planning is like seeing what will happen today. You will also find your syntax error.
$ terraform plan
Apply means that you will deploy the code to the cloud. Do it by simply running the command,
$ terraform apply
It will ask you for confirmation. Just type 'yes' and hit enter. In a few seconds your infrastructure will be ready.

You can now sign in to the AWS console and go to the ec2 service. You will see that ec2 starts.

Similarly, go to s3 and find your bucket.

Destroy the infrastructure
If you want to destroy the old infrastructure, you can simply type 'terraform destroy'.
$ terraform destroy
It asks you for confirmation, just say 'yes' and press enter. The ec2 instance and the s3 bucket that you created earlier must be removed from your AWS account. You can verify by signing in to the AWS console.
Remove terraform
If you want to remove terraform you can simply delete the 'terraform' file saved in /usr/local/bin/
$ sudo rm -f /usr/local/bin/terraform
Also, you can clean your directory where you have initialized terraform. In our case, we can delete the entire contents of the 'aws' folder.

No comments

Powered by Blogger.