Revolutionize Your Infrastructure Management with Terraform
Terraform is an Infrastructure as Code Tool by Hashicorp and is open source.
It can help you deploy your whole infrastructure on the choice of your favorite cloud provider (like Amazon Web Services, Google Cloud Platform, Azure, etc.) in minutes. You need to write some configuration files and they will be deployed in minutes. Configuration files are also effortless to write as well.
How to Install Terraform?
To install Terraform, follow these steps:
- Download the Terraform binary for your operating system from the Terraform website.
2. Extract the downloaded file and place the terraform
binary in a directory that is included in your PATH
environment variable. This will allow you to run the terraform
command from any directory.
3. Verify that the installation was successful by opening a new terminal session and running the terraform
command. If the command is not recognized, check that the terraform
binary is in a directory that is included in your PATH
environment variable.
4. Optionally, run terraform -help
to see the available command-line options and subcommands.
Note: these instructions assume that you are using a Unix-like operating system (such as Linux or macOS) and that you are familiar with basic command-line operations. If you are using a different operating system or are not comfortable with the command line, you may need to consult additional resources to complete the installation.
Deploy Simple Ec2 Instance on Amazon Web Services (AWS) using Terraform?
First of all, to launch any resource on any cloud provider terraform needs to have some kind of authentication access to it otherwise, it will not be able to create any resource.
So we will create an I am user in AWS with administrator access and we will provide those credentials to terraform. So terraform will act like an administrator and will be able to handle the deployment of resources.
Steps To Create I am User:
- Search I AM inside the AWS management console.
2. Click on the Users Link:
3. Click on the Add Users Link:
4. Give username and access type and click next:
5. Select Permissions:
6. That’s it user is created keep the access key and secret somewhere safe we will need those in the next Section.
Steps for Creating EC2 instance on the AWS using terraform.
- we will create a simple terraform configuration file (ec2.tf):
provider "aws" {
region = "ap-south-1"
access_key = "your_access_key"
secret_key= "your_secret_key"
}
resource "aws_instance" "chirag_ec2" {
ami = "ami-074dc0a6f6c764218"
instance_type = "t2.micro"
}
let’s understand what the above code meant. So the first line declares the provider block which defines the provider configurations. we are using AWS in this case. after that, we define the region you can place the region of your choice from the list of available regions supported by AWS. Then we define the access_key and secret key we generated for the IAM user in the above section.
Then we have a resource block in which we define we need an AWS instance we are giving the name chirag_ec2. after that, we define AMI type which is the image type you want for your ec2 instance you can copy the image from there in this case we are using Amazon Linux.
after that, we define instace_type which is t2.micro in our case which is a free tier. you can use instance type according to your need.
2. after creating the file:
terraform init
it will download the respective cloud provider plugin AWS in our case.
3. after we write terraform plan:
terraform plan
it will give you all the information terraform will create such as info about the ec2 instance.
4. after that you will type terraform apply and enter yes in the prompt:
terraform apply
that’s it you created your first ec2 instance with the help of Terraform. Don’t forget to destroy the resource using terraform destroy. otherwise, you will have to pay.
terraform destroy
Give yourself a nice pat on the back for learning new stuff today. come again tomorrow to learn new things.
My Other Fantastic Blogs: