How to Provision NGINX webserver with Terraform in AWS?

Kubernetes Advocate
AVM Consulting Blog
3 min readNov 6, 2020

Deployment and infrastructure management isn’t a simple process. On top of a lot of ever-changing, domain expertise we need to learn Yet Another Tool or a new workflow. If you have been putting this off, this article is a chance for you to get acquainted with one approach to infrastructure management.

Let’s start installation :

  1. Find the package for your Laptop/server and download it.
  2. Now unzip the package.

We should make sure and confirm it regarding the `terraform` binary is coupled to your PATH

Now you must check and confirm if your terminal print a colon-separated list of locations in your PATH.

echo $PATH

Let’s move the terraform binaries to a specific location

$ mv ~/Downloads/terraform /usr/local/bin/terraform

For more detail about adding binaries to your path

1. Check the Installation

Using the below command check it by opening a CLI/Terminal and listing the terraform subcommands

$ terraform -help
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
##...

Add any subcommand to terraform -help to see what all functions it performs

$ terraform -help plan

If in case you see issues with terraform is not found, It directs to the ENV Variables that may have not properly set as per PATH, Let’s confirm PATH variable has a directory where we installed Tf

You can use a terminal-like bash or Zsh for running terraform commands, currently run the below command and restart the shell

$ terraform -install-autocomplete

Let’s start after the terraform is installed

SO now as we already installed terraform, you can also provision Nginx/apache using containerization

After you installed Docker and Terraform on your server, let us start the docker desktop

$ open -a Docker

Let's start with the Demo

Create a directory and name it as terraform-docker-demo.

$ mkdir terraform-docker-demo && cd $_

Now paste the Terraform configuration into a file and name it main.tf.

resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}

Initialize the project, which downloads a plugin that allows Terraform to talk to Docker.

$ terraform init

Lets Provision the NGINX server with apply.

$ terraform apply

Confirm by typing “YES” and press ENTER

Click on Site that you just provisioned with NGINX container by clicking on localhost:8000 in your web browser

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78ghsdn46289 ghj74846ghdjs "nginx -g 'daemon of…" 19seconds ago Up 18seconds 0.0.0.0:8000->80/tcp

To stop the container, run terraform destroy.

$ terraform destroy

So in this article, we provisioned and decommissioned an NGINX webserver with Terraform

Thanks

For Reference

👋 Join us today !!

️Follow us on LinkedIn, Twitter, Facebook, and Instagram

If this post was helpful, please click the clap 👏 button below a few times to show your support! ⬇

--

--

Kubernetes Advocate
AVM Consulting Blog

Vineet Sharma-Founder and CEO of Kubernetes Advocate Tech author, cloud-native architect, and startup advisor.https://in.linkedin.com/in/vineet-sharma-0164