Friday, 13 April 2018

Deploy a single Web server with security group and user_data using Terraform

To Deploy a single web server that can respond to HTTP request .

Architecture Diagram

In this example I am using bash script that write the text message "Hello World" into index.html


user_data = <<-EOF
            #!/bin/bash
            echo "hello, world" > index.html
            nohup busybox httpd -f -p 8080 &
            EOF

I wrapped the busybox command with nohup and & so that the webserver run permanently in the background.

Note: In this example uses port 8080, rather than HTTP port 80 because root user privileges requires
If you listening on any port less than 1024 requires root user privileges.

You need to create the security group.

resource "aws_security_group" "instance" {
    name = "Terraform-example-1"

    ingress {
        from_port = 8080
        to_port   = 8080
        protocol  = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

Note: Aws does not allow any incoming or outgoing traffic from an ec2 instance.

Also need to tell the ec2 instance actually use it.To do that,you need to pass the ID of the security group into vpc_security_group_ids parameter of the aws_instance resource.

vpc_security_group_ids = ["${aws_security_group.instance.id}"]

Note: Terraform knows it needs to create the security group before the ec2 instance.

Source code of main.tf

variable "aws_access_key" {}
variable "aws_secret_key" {}

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region = "us-east-1"
}

resource "aws_instance" "First" {
ami = "ami-40d28157"
instance_type = "t2.micro"
vpc_security_group_ids = ["${aws_security_group.instance.id}"]

user_data = <<-EOF
            #!/bin/bash
            echo "hello, world" > index.html
            nohup busybox httpd -f -p 8080 &
            EOF

tags {

   Name = "Terraform-example-1"

  }

}

resource "aws_security_group" "instance" {
    name = "Terraform-example-1"

    ingress {
        from_port = 8080
        to_port   = 8080
        protocol  = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
    }
}

source code of  terraform.tfvars variable

aws_access_key = "xxxxxxxxxxxxxxxxxxxxx"
aws_secret_key = "xxxxxxxxxxxxxxxxxxxxx"


Run the apply command  > terraform apply -var-file=..\terraform.tfvars

Note:  Terraform to add a security group and replace the original ec2 instance with a new one that has the new user date( the -/+ means "replace")

Example output:

-/+ aws_instance.First (new resource required)
      id:                                   "i-0b6d39ea71d5fb1ed" => <computed> (forces new resource)
      ami:                                  "ami-40d28157" => "ami-40d28157"

Now EC2 instance launching




Use web browser or curl command to make an HTTP request

> curl http://<ec2-ip-address>:8080
Hello, World




Now web server running in AWS!

Note:

Even you get the dependency graph by running the terraform graph command:

> terraform graph

Output:

digraph {
        compound = "true"
        newrank = "true"
        subgraph "root" {
                "[root] aws_instance.First" [label = "aws_instance.First", shape = "box"]
                "[root] aws_security_group.instance" [label = "aws_security_group.instance", shape = "box"]
                "[root] provider.aws" [label = "provider.aws", shape = "diamond"]
                "[root] aws_instance.First" -> "[root] aws_security_group.instance"
                "[root] aws_security_group.instance" -> "[root] provider.aws"
                "[root] meta.count-boundary (count boundary fixup)" -> "[root] aws_instance.First"
                "[root] provider.aws (close)" -> "[root] aws_instance.First"
                "[root] provider.aws" -> "[root] var.aws_access_key"
                "[root] provider.aws" -> "[root] var.aws_secret_key"
                "[root] root" -> "[root] meta.count-boundary (count boundary fixup)"
                "[root] root" -> "[root] provider.aws (close)"
        }
}

The output is in a graph description language called DOT.

Note: you can turn into an image using Graphviz online tool and paste above output into text area.It generate the graphic picture.






No comments:

Post a Comment

Configure EKS Persistent Static Storage With Elastic File System Using Terraform

  checkout :https://youtu.be/QuUIgQWBBZM