Getting Started with Terraform,Creating a S3 Bucket and an EC2 Instance

Getting Started with Terraform,Creating a S3 Bucket and an EC2 Instance

Overview:

In this blog, we will set up Terraform, and create S3 Bucket and an EC2 Instance using HCL code.

Creating a server using AWS EC2:-

  • Sign in to your AWS account and click on EC2.

  • Here click on Launch Instance.

  • Give it a name I will name it Terraform and in the AMI Select Ubuntu.

  • In the Instance type select t2.micro and in the Key pair, login select your key pair. I have selected mine, Kube.

  • Check SSH, HTTP and HTTPS from Network settings.

  • Now from the right side click on Launch Instance.

Connecting Server using SSH and Setting up Terraform on the Server:-

  • We will connect the Server using SSH and set up Terraform.

  • To connect Server check your Instance and click on the button Connect from the top right.

  • Now here Copy the Example line in the SSH client.

  • Open the terminal in your system and navigate to the Place where your key pair is stored. In my case, it's in Downloads and after navigating paste the Example line with sudo in front of it. It should be this:

    Navigate by the command:

      cd Downloads/
    
  • This will connect our server using SSH.

  • Now we will Install Terraform here. At first, we will install some packages by the command:

      sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
    

  • Now we will Install the Hashcrop key by the Command:

      wget -O- https://apt.releases.hashicorp.com/gpg | \
      gpg --dearmor | \
      sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
    

  • We will verify it by the command:

      gpg --no-default-keyring \
      --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
      --fingerprint
    

  • Now at last run these Commands one by one:

      echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
      https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
      sudo tee /etc/apt/sources.list.d/hashicorp.list
    
      sudo apt update
      sudo apt-get install terraform
    

This will Install Terraform on our Server.

  • Verify it by running the Command:

      terraform --version
    

  • First, create a folder named terraform and navigate to it using the commands:

      mkdir terraform
      cd terraform
    

  • To create and Launch things we will set aws config because it has no idea on which account these will be created. So install awscli by the command:

      sudo apt install awscli
    

  • Press Enter here.

  • Now we will need Access Key ID and Secret Key So we have to generate it.

  • Search for IAM in your AWS Console and click on it.

  • In the IAM Dashboard click on Users from the left side.

  • Click on Create user.

  • Give it a name I will name it demo and click on Next.

  • Here select the option Attach policies directly, check the AdministratorAccess and click on Next.

  • Review it and click on Create User.

  • Our user is created now we will create the Access key and Secret key.

  • Open the User created and click on Security credentials.

  • Coming down click on Create access key.

  • Check the Command Line Interface(CLI) check the Confirmation and click on Next.

  • Write the description and click on the Create access key. This will create Keys for us which we will use in our Server to tell that the S3 bucket and EC2 Instance will be created in this AWS Account.

  • We will get our keys now in our terminal where the server was connected there we have to run the command:

      aws configure
    

Paste your Access Key ID and Secret key and Escape more things if asked by pressing enter.

Creating a S3 bucket and an EC2 Instance:-

  1. We are done now just we will create an S3 bucket and EC2 Instance.

  2. Create a file named terraform.tf by the command:

     vim terraform.tf
    

  • In this file paste these Code:

      terraform {
        required_providers {
          aws = {
            source  = "hashicorp/aws"
            version = "~> 5.0"
          }
        }
      }
    

    Save and exit this file.

  • Create one more file named main.tf by the command:

      vim main.tf
    
  • In this file Paste this code:

      provider "aws" {
        region = "us-east-1"
      }
    
      resource "aws_s3_bucket" "demo-s3" {
        bucket = "buckshivam"
      }
    
      resource "aws_instance" "demo-ec2" {
        ami = "ami-01c647eace872fc02"
        instance_type = "t2.micro"
        tags = {
          Name = "terra-auto-instance"
        }
      }
    

    Your AMI will be different so paste your AMI. You can find it easily.

  • We are all done now just run these commands one by one:

      terraform init
      terraform plan
      terraform apply
    

This will create a S3 bucket and an EC2 on your AWS Account. You can go and see it.

Thanks.