Create Infrastructure from Terraform

Create Infrastructure from Terraform

Table of contents

No heading

No headings in the article.

  1. Launch a Terraform server on AWS using EC2

  2. Install Terraform on the Ec2 instance

  3. Install git on the EC2 instance

create a directory on the server like - "terragit"

cd "terragit"

Run the command "git init" in the directory

and create a tf file in that directory like: "vi file1.tf"

Write the code to connect to aws in the tf file :
(find code ata - https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

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

now save and get out of the file after writing the above code: esc ->:wq

  1. run command "terraform init"

  2. run command "ls -a", now we see more files(plugins for aws ) other than file1.tf in the directory terragit which gets downloaded when we ran terraform init

  3. Now we have to connect our terraform server(which can be personal laptop or personal server ) to the cloud provider. (here our terraform server and infrastructure server to be made is both on aws).

  4. Create a new IAM user like(terrauser)on aws infra which will be used to connect to the aws from the terraform server, Give programactic acces and and existing policy(administrator access) to the new user -> create user.
    Download the .CSV given by AWS which has the new iam user credentials

  5. Now open the file1.tf in the terragit directory and paste the below code next after the previous code .

  6.   provider "aws" {
        region     = "us-west-2"
        access_key = "my-access-key"
        secret_key = "my-secret-key"
      }
    
  7. Create an access key and secret access key for your aws user, Goto IAM-> users-> select your user(terrauser)->security credentials -> create access keys -> create -> download the new .csv file

  8. Open the new .csv file copy the acces key and secret access key of the user and replace in the above code in (6) also choose your desired region instead of the default us-west-2 like below.

  9.   provider "aws" {
        region     = "ap-south-1"
        access_key = "BAQWERTYUCCCDS"
        secret_key = "riftwjeg70+EUCnsuewiENCDkjjWIJdiunjdf"
      }
    
  10. Now run command "terraform plan"

  11. Now run command "terraform apply"

  12. Now to check if our credentials in the tf file can login to aws and create infra.

  13. To launch the infra we need to have infra details like OS image id - for this goto aws -> launch ec2 instance -> select a OS -> below we see a AMI for that OS(make sure you copy the ami of the same region in which you want to create the infra ) , copy the ami in a notepad and the instance name t2.micro

  14. now we have to get terraform code to launch instance for that visit - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance

    On this page we will get the terraform code as below:

    resource "aws_instance" "web" {
      ami           = data.aws_ami.ubuntu.id
      instance_type = "t3.micro"
    
      tags = {
        Name = "HelloWorld"
      }
    }
    

Now copy the above code and paste in the file1.tf file in the terraform server change the value of the default code and other arguments you want to add in the format as mentioned in the baove link. Like server name from "web" to "T1", tags from "HellowWorld" - "server1" , instance type from "t3.micro" - "t2.micro" and ami to which we copied earlier and the above code will look like

 resource "aws_instance" "T1" {
  ami           = "ami-01bb8868a249be5ca"
  instance_type = "t2.micro"

  tags = {
    Name = "server1"
  }
}

Now save the exit from the file1.tf

  1. Now check if the code is correct by a dry run which is done by running the code "terraform validate" you will get the message "Success! The configuration is valid."

  2. Now run the command "terraform plan " which will show what terraform is going to make in the infra and what you provided and what you choose by default.

  3. After "terraform plan " run the command "terraform apply" then YES and the infra will be created in the aws.

  4. check the ec2 console of that particular region ap-south-1 and we see a server running.

  5. if you want to destroy your infrastructure , run command "Terraform destroy " on the terraform server . The ec2 server created will be destroyed

  6. If we want to create more than one server , open file1.tf .
    Paste the above code for instance details the number of times we want to create the number of instances with different server and tag names as below

    Now check on the ec2 console of that particular region and we will see 3 instances with there name running

  7. Now login to one of the servers created by terraform and install httpd "yum install httpd" this will install an apache web server.

  8. now goto path cd /var/www/html/ and create a file vi index.html

  9. Write any text or HTML ui code in the index file.

  10. then run the command "server httpd start"

  11. and visit the public ip of the server in the browser we see a web page with the HTML ui .
    Points 7 8 9 10 11 can be done with terraform and ansible also instaead of manually.