Getting Started with Terraform: Automating NGINX Container Deployment

ARYAN VERMAARYAN VERMA
3 min read

What is Terraform ?

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision infrastructure using a declarative configuration language. With Terraform, you can manage a wide range of resources, including virtual machines, containers, networks, and more, across various cloud providers and on-premises environments.

Setting Up Your Project

Let's get started with creating an instance and setting it up locally.

Let's start by creating a new directory into our instance for our Terraform project:

mkdir terraform-nginx
cd terraform-nginx

Install docker as it is most important to start container by the command

sudo apt install docker.io

After intalling docker.io, there will be issues in permissions for the current user due to which the commands executed will not be performed successfully. Due to which we will change our user permission through command

sudo usermod -aG docker $USER

The changes made to user permission will be not be instantly changes , for which we will be reboot the instance and reconnect it to local.

sudo reboot

Terraform Installation

kindly execute the commands which are listed in order to get download the terraform into your local machine.

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring \
--keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
--fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform

Congratulations ! You have successfully downloaded the terraform into your local host.

Creating of Terraform Automated File

kindly create a file named "terraform.tf" or any name you like but the extension for terraform file should be ".tf"

vim terraform.tf
terraform{
    required_providers{
        docker={
            source="kreuzwerker/docker"
            version="3.0.2"
            }
    }
}

resource "docker_image" "my_nginx_image"{
    name="nginx:latest"
    keep_locally=false
}

resource "docker_container" "nginx_container"{
    image=docker_image.my_nginx_image.name
    name="nginx-automated"
    ports{
        internal=80
        external=80
    }
}

Save the file and exit by pressing "esc + :wq"

Initializing Terraform

Before applying our Terraform configuration, we need to initialize the Terraform working directory. Run the following command in your terminal:

terraform init

See the Plan

Seeing the plan will make things clear in order of which things are going to be executed or created. Run the following command to see that:

terraform plan

Applying the Configuration

Now that we've initialized our Terraform project, let's apply the configuration to create the NGINX container:

terraform apply

Accessing NGINX

Once the Terraform apply process completes successfully, you can access the NGINX container from your web browser by visiting http://instanceIP:80. You should see the default NGINX welcome page indicating that the container is up and running.

Conclusion

Congratulations! You've successfully automated the deployment of an NGINX container using Terraform. This is just the beginning of your journey with Terraform and infrastructure as code. Experiment with different configurations, explore Terraform documentation, and continue learning to unlock the full potential of infrastructure automation.

"Stay away from those people who try to disparage your ambitions. Small minds will always do that, but great minds will give you a feeling that you can become great too.” —Mark Twain

0
Subscribe to my newsletter

Read articles from ARYAN VERMA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ARYAN VERMA
ARYAN VERMA