Wednesday, 21 October 2020

Deploy a single server via terraform

 Terraform code is written in the Hashicorp configuration language in files with extension .tf file.

1.  AWS region is a separate geographic area, such as us-east-1, us-east2, there are multiple isolated datacenters know as availability zone.

2. There are different kinds of resources available, base on your requirement you have to choose. for example aws_instance  

3. Terraform allows defining input variables. because don't need to repeat the same value everywhere.

         variable "Name" {

          }

  In the user Data section, I refer to the interpolation expression "${....}"

 4.  Terraform allow to define output variables by the following syntax.

         output "name" {

         }

    Instead of find the ec2 console to get ip address of instance, output section provide the ip address as an output.

 5. You have to create the server ssh key before itself.


Details:

             

Step: 1

create an empty directory and put a file in it called main.rf.

Step: 2 

Copy my following contents

provider "aws" {
    region = "us-east-1"
    access_key = "xxxxxxxxxxxxxxxxxx"
    secret_key = "xxxxxxxxxxxxxxxxxx"
}

variable "server_port" {
  type        = number
  default     = "8080"
  description = "security-group"
}


resource "aws_instance" "myec2" {
   ami = "ami-0dba2cb6798deb6d8"
   instance_type = "t2.micro"
   vpc_security_group_ids = [aws_security_group.instance.id]
   key_name = "Kubectl-testing-myaccount"

   user_data = <<-EOF
              #!/bin/bash
              echo "Hello, World" > index.html
              echo "hello 2"
              nohup busybox httpd -f -p ${var.server_port} &
              EOF

   tags = {
    Name = "Myec2instance"
    ID = "backup"
  }

}


resource "aws_security_group" "instance" {
  name = "terraform-example-instance"

  ingress {
    from_port   = var.server_port
    to_port     = var.server_port
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}


output "public_ip" {
  value       = aws_instance.myec2.public_ip
  sensitive   = false
  description = "The public IP address of the webserver"
}


Step: 3 Terraform show dependency graph by running the graph command.

The output is in a graph called DOT, copy the content apply to Graphvizline tool.










No comments:

Post a Comment

Configure EKS Persistent Static Storage With Elastic File System Using Terraform

  checkout :https://youtu.be/QuUIgQWBBZM