Launching EC2 instances along with key-pair and security group- an intro to Terraform

Hello, readers. This is my first blog and we start off with Terraform. I'll talk about Terraform in brief, and then moving onto the launching of EC2 instances. Then we will be seeing the addition of key pairs and security groups to the instance we created, which will then be followed by the creation of an EBS volume of size 1GB, which will then be attached to the instance.

What is Terraform?

Terraform is Infrastructure as code for managing, Building infrastructure from code. Terraform can manage existing and popular cloud service providers(AWS, AZURE, GCP, Alibaba) as well as custom in-house solutions. It is one of the famous DevOps tools in the market.

Suppose you need EC2 Instance with that EC2 Instances, 1 Security Group, 1 Keypair etc, So you’ll manually create it in AWS Console/CLI/SDK. Now in case, you need the same thing, many times in your requirements, so this will be hectic work for you. So this is where Terraform comes, you write code once, use it or modify it accordingly, as many times.

Create/Launch Application using Terraform

Below is the Flow for the application launch:

1. Create a key pair

2. Create a security group

3. Launch an instance using the above-created key pair and security group.

4. Create an EBS volume of 1 GB.

5. The final step is to attach the above-created EBS volume to the instance you created in the previous steps.


Step 1: Creating Key Pair

First, add a provider clause that specifies your region.

provider "aws" {
  region     = "ap-south-1"
  profile    = "default"
}

Now add the clause for key pair

resource "aws_key_pair" "deploy" {
  key_name   = "mykeypair"
  public_key = "ssh-rsa <public_keypair>"

For public key pair generation, use the website

https://asecuritysite.com/encryption/ssh



Step 2: Create a security group

resource "aws_security_group" "examplesg" {
  name = "My  Security Group"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
 ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
}
egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

This will create a security group name My Security Group having ingress port 80,22 allowed to internet and allow egress of all ports.




3. Creating EC2 instance

resource "aws_instance" "myenv" {
  ami           = "ami-010aff33ed5991201"
  availability_zone = "ap-south-1a"
  instance_type = "t2.micro"
  key_name = aws_key_pair.deploy.key_name
  security_groups = [aws_security_group.examplesg.name]
  tags = {
    Name = "MyFirstos"
  }
}

This will create an ec2 instance with the name as MyFirstos having key and security groups that we created in earlier steps.




Step 4 & 5: 
Launching EBS volume and attaching same to that instance

resource "aws_ebs_volume" "myebsvol" {
  availability_zone = aws_instance.myenv.availability_zone
  size              = 1
  tags = {
    Name = "myebsvol"
  }
}


#For attaching that ebs volume


resource "aws_volume_attachment" "ebs_att" {
  device_name = "/dev/sdf"
  volume_id   = aws_ebs_volume.myebsvol.id
  instance_id = aws_instance.myenv.id
}

This will create an EBS volume with the name myebsvol and the same EBS volume is also attached to the instance which we created.


A simultaneous view of the terminal is also displayed below, for the execution of Terraform on the above code.






I hope the post was easy to follow!



Comments

Popular posts from this blog

How Unilever Benefitted by Transforming into Cloud-Based Digital-Market Empire

Creating custom Terraform module - creating submodule for s3 bucket resource from the AWS provider.