Hashicorp's Terraform is an open-source tool for provisioning and managing cloud infrastructure. Terraform can provision resources on any cloud platform.
Terraform allows you to create infrastructure in configuration files(tf files) that describe the topology of cloud resources. These resources include virtual machines, storage accounts, and networking interfaces.We will see how you can use Terraform to provision EC2 instance. Please do the below steps for provisioning EC2 instances on AWS:
2. Create a new access key if you don't have one. Make sure you download the keys in your local machine. Login to AWS console, click on username and go to My security credentials.
Continue on security credentials, click on access keys
Perform below commands in MacOS/EC2 where you have installed Terraform:
First setup your access keys, secret keys and region code locally.
Create Terraform Files
Execute the below command
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Watch the steps in YouTube channel:
Pre-requistes:
2. Create a new access key if you don't have one. Make sure you download the keys in your local machine. Login to AWS console, click on username and go to My security credentials.
Continue on security credentials, click on access keys
Perform below commands in MacOS/EC2 where you have installed Terraform:
First setup your access keys, secret keys and region code locally.
aws configure
cd ~
mkdir project-terraform
cd project-terraform
Create Terraform Files
sudo vi variables.tf
variable "aws_region" {
description = "The AWS region to create things in."
default = "us-east-2"
}
variable "key_name" {
description = " SSH keys to connect to ec2 instance"
default = "myJune2021Key"
}
variable "instance_type" {
description = "instance type for ec2"
default = "t2.micro"
}
variable "security_group" {
description = "Name of security group"
default = "my-jenkins-security-group-2022"
}
variable "tag_name" {
description = "Tag Name of for Ec2 instance"
default = "my-ec2-instance"
}
variable "ami_id" {
description = "AMI for Ubuntu Ec2 instance"
default = "ami-0b9064170e32bde34"
}
Now create main.tf file
sudo vi main.tf
provider "aws" {
region = var.aws_region
}
resource "aws_vpc" "main" {
cidr_block = "172.16.0.0/16"
instance_tenancy = "default"
tags = {
Name = "main"
}
}
#Create security group with firewall rules
resource "aws_security_group" "jenkins-sg-2022" {
name = var.security_group
description = "security group for jenkins"
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound from Jenkins server
egress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags= {
Name = var.security_group
}
}
resource "aws_instance" "myFirstInstance" {
ami = var.ami_id
key_name = var.key_name
instance_type = var.instance_type
vpc_security_group_ids = [aws_security_group.jenkins-sg-2022.id]
tags= {
Name = var.tag_name
}
}
Now execute the below command:
terraform init
you should see like below screenshot.
Execute the below command
terraform plan
the above command will show how many resources will be added.
Plan: 3 to add, 0 to change, 0 to destroy.
Execute the below command
terraform apply
Plan: 3 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Now login to EC2 console, to see the new instances up and running
List Resources created by Terraform
List Resources created by Terraform
Execute the below command to view list of the resources created by Terraform.
terraform state list
The above command will list three resources created.
You should be able to see EC2 instance up and running in AWS console.
How to push Terraform files into GitHub
All Terraform files should be checked into version control systems such as GitHub, BitBucket or GitLab. Let us see how to push code changes into GitHub. Make sure you are in the directory where Terraform files are created.
All Terraform files should be checked into version control systems such as GitHub, BitBucket or GitLab. Let us see how to push code changes into GitHub. Make sure you are in the directory where Terraform files are created.
Create Remote repo in GitHub
Create a new repo with below name, make sure it is a private repo. Also do not click on initialize this repository with a README option.
Note down the remote url as highligted below:
Note:
If you have any issues in uploading tf files, you may not have created ssh-keys and uploaded into GitHub. Create ssh keys using ssh-keygen command:
Create a new repo with below name, make sure it is a private repo. Also do not click on initialize this repository with a README option.
Note down the remote url as highligted below:
Note:
If you have any issues in uploading tf files, you may not have created ssh-keys and uploaded into GitHub. Create ssh keys using ssh-keygen command:
ssh-keygen
This should generate both public and private keys.
Copy the public keys by executing the below command:
sudo cat ~/.ssh/id_rsa.pub
Initialize the directory first
git init
The above command will create local git repository.
Now add terraform files.
git add *.tf
git commit -m "Added terraform files"
Copy the below red highlighted url from above screenshots circled in red.
git remote add origin your remote repo url per above screenshot
Now push the code into GitHub
git push -u origin master
Now Login to GitHub to view the Terraform files
git init
The above command will create local git repository.
Now add terraform files.
git add *.tf
git commit -m "Added terraform files"
Copy the below red highlighted url from above screenshots circled in red.
git remote add origin your remote repo url per above screenshot
Now push the code into GitHub
git push -u origin master
Now Login to GitHub to view the Terraform files
I really admired with your post, do share more updates.
ReplyDeleteDevOps certification in Chennai
DevOps Training in Chennai
AWS Training in Chennai
AWS course in Chennai
Data Science Course in Chennai
Data Science Training in Chennai
DevOps Training in Velachery
DevOps Training in Tambaram
Thanks for sharing like this content. I really appreciate sharing your post. and also you can go through this site and read the interesting thing we provide best free internship training in chennai
ReplyDeleteThanks for sharing this is really great post ,.i like so much :
ReplyDelete