In the previous post we have learned how to install and configure terraform with aws cli and connect it to our AWS account.

We had also learned how to create our first AWS instance using terraform.

In this post we will learn how to:

Creating webserver using terraform

Make sure you are done with the previous post and your aws cli and terraform are able to talk to your AWS account.

Create a file – webserver.tf . I am dividing this creating webserver using terraform post in three parts.

Part – 1 – Choosing AWS Region to host your Instance.

Configure your AWS region in which you want your resource.

Choosing AWS region to host our resources

Copy the code, if you are feeling lazy. But I strongly recommend to write terraform code. As that makes you remember the things.

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

Part – 2 – Create AWS Security Group which will allow http and ssh

Create a security group using terraform which allows port 80 (for http) and port 22 (for ssh). Enabling us to check the webpage and do ssh for management of our aws instance. I am creating a security group named “morning-ssh-http” which is allowing access from anywhere in the world (which is bad, but its ok for this small demo).

AWS security group to allow access to http and ssh services

Copy the code from here.

resource "aws_security_group" "morning-ssh-http" {
  name        = "morning-ssh-http"
  description = "allow ssh and http traffic"

  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"]
  }
}

Part – 3 – Create AWS Instance

Once the region and the security group is defined. Its time to create a webserver in terraform using aws_instance resource type. I am also attaching a ssh key “zoomkey” which is already present in my AWS Account. I will use the same key to do ssh, whenever required.

I will use terraform’s “user_data” attribute of “aws_instance” resource type to feed my script which will automatically.

  • Install the httpd package
  • Start and enable httpd service
  • Create a simple webpage

Our previously created security group – morning-ssh-http, will be attached with this instance. Else it will not be accessible.

Creating aws instance to use security group, ssh key and user data

Copy the code from there

resource "aws_instance" "good-morning" {
  ami               = "ami-5b673c34"
  instance_type     = "t2.micro"
  availability_zone = "ap-south-1a"
  security_groups   = ["${aws_security_group.morning-ssh-http.name}"]
  key_name = "zoomkey"
  user_data = <<-EOF
                #! /bin/bash
                sudo yum install httpd -y
                sudo systemctl start httpd
                sudo systemctl enable httpd
                echo "<h1>Sample Webserver Network Nuts" | sudo tee  /var/www/html/index.html
  EOF


  tags = {
        Name = "webserver"
  }

}

Finally, when its done. We will check our terraform code for any syntax errors using

terraform validate

And if everything comes good. Time to run the code and see if we are getting everything as expected in our AWS account. Run the creating webserver using terraform code using:

terraform apply

Applying terraform code using terraform apply command

Login AWS accounts and verify the created instance.

creating webserver using terraform
AWS Dashboard,verifying creating webserver using terraform

If I use the public IP of – 15.206.91.134

The website should come up.

So we have just done creating simple webserver using terraform.

Finally check if we are able to do ssh in this instance.

Getting ssh key from AWS dashboard
Doing ssh into the instance

Congrats, if you had been following the creating webserver using terraform post carefully. Your webserver is live and kicking.

Don’t forget to remove everything, once you are done using:

terraform destroy

Interested in our Terraform Training ? Click here.