DEV Community

Cover image for Deploying Your First Server with Terraform: A Beginner's Guide
frankfolabi
frankfolabi

Posted on

Deploying Your First Server with Terraform: A Beginner's Guide

Now you have installed Terraform, configured your AWS account to use the CLI, and installed VS Code. What next? Let us get deploy our first webserver.

  • On VS Code, create a directory to store your files with mkdir tf-files in the terminal
  • Next create a main.tf file with touch main.tf. You will write your configuration files here in Harshicorp Configuration Language.

code to make create directory

  • Here is the block of code that you can add in your main.tf file

Provider Block

This shows that the provider is AWS and the region to be used is us-east-2

provider "aws" {
    region = "us-east-2"
}
Enter fullscreen mode Exit fullscreen mode

Resource block

The resource block is used to define the resource Terraform would create. One interesting part is that Terraform would try to figure out which order to create these resources. In this resource block, we define the following:

  1. Amazon Machine Image (AMI)
  2. Instance type
  3. Security group id
  4. User data script
  5. Tag
resource "aws_instance" "webserver" {
    ami = "ami-0fb653ca2d3203ac1"
    instance_type = "t2.micro"
    vpc_security_group_ids = [aws_security_group.instance.id]
    user_data = <<-EOF
    #!/bin/bash
    echo "Hello, World. Welcome to the use of Terraform in deploying infrastructure" > index.html
    nohup busybox httpd -f -p 8080 &
    EOF
    user_data_replace_on_change = true
    tags = {
        Name = "tf-webserver"
    }
}
Enter fullscreen mode Exit fullscreen mode

This other resource block declares the security group to allow traffic from port 8080 from any IPv4 address

resource "aws_security_group" "instance" {
    name = "tf-sg"

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

We can now save our main.tf file. Next, let us now deploy the webserver. First, we need to download the required provider plugins. Run terraform init

running terraform init

You may wonder, what are the provider plugin? These are executable binaries that the Terraform Core install on our laptop uses to communicate with the APIs of the cloud provider that has the resources we need. Notice that the screenshot uses the expression "Reusing previous version of . . ." This happens when you are running the terraform init command after the first time.

As a quick check to see if our file follows the right syntax, run terraform validate

Terraform validate

What would our infrastructure look like? You can now run terraform plan

terraform plan

This information contains what resources would be created on the AWS. Now, let us proceed to deploy these resources. Run terraform apply. A confirmation will pop-up to ensure you really want to deploy these resources. The only acceptable answer to proceed is yes

terraform apply

So quick! You now have a webserver online. Did you notice how long it took to get the EC2 instance ready? 14 seconds. That's amazing!
instance ready

How do I know that my webserver is actually working? Let us check some information about the webserver. Run terraform show

Information about your instance

Did you notice the last entry displayed? That is the public IP address of the instance. Now, let us check the web content. Run curl http://<public_ip>:8080

webcontent
Isn't that beautiful? Why not let us check out our EC2 instance on the AWS Console. Here you go!

EC2 page on console

What about our webpage? Type in your browser http://<public_ip>:8080

webpage on browser

To clean up the resources and delete everything, run terraform destroy and enter yes as a confirmation to destroy the infrastructure.

Here is the architecture of what we built

Architecture diagram

Congratulations! We have deployed our first single web server, tested it and destroyed it. Can we improve on this? A future post will show us how. Feel free to ask questions regarding this post.

Top comments (0)