How to create EC2 instances using Terraform | Automate EC2 instance Creation using Terraform on AWS Cloud

Terraform can provision resources on any cloud platform. We will see how you can use Terraform to provision EC2 instance. Please do the below steps for provisioning EC2 instances on AWS:



Pre-requistes:
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:

cd ~
mkdir project-terraform
cd project-terraform

Create Terraform Files
sudo vi aws.tf

copy the below code and fill xx with above access key and secret keys you have created. Make sure you have the right AWS region code.

provider "aws" {
  access_key = "xx"
  secret_key = "xx" 
  region     = "us-east-2"
}


Now execute the below command:
terraform init
you should see like below screenshot.


sudo vi create_ec2.tf
copy the below code in yellow color:

resource "aws_instance" "myFirstInstance" {
  ami           = "ami-916f59f4"
  count=1
  key_name = "myKey"
  instance_type = "t2.micro"
  security_groups= [ "security_jenkins_port"]
  tags= {
    Name = "jenkins_instance"
  }
}

resource "aws_security_group" "security_jenkins_port" {
  name        = "security_jenkins_port"
  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 jenkis server
  egress {
    from_port   = 0
    to_port     = 65535
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags= {
    Name = "security_jenkins_port"
  }
}

Execute the below command
terraform plan
the above command will show how many resources will be added.
Plan: 2 to add, 0 to change, 0 to destroy.


Execute the below command
terraform apply
Plan: 2 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

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Now login to EC2 console, to see the new instances up and running

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:



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.


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


Post a Comment

0 Comments