Provisioning your first nginx using terraform on AWS

Photo by Mehmet Ali Peker on Unsplash

We will be learning to use provisioners as part of resource creation in terraform to deploy an EC2 and run custom script to install nginx web server.

Prerequisites-

  1. Terraform v0.13+ installed
  2. AWS CLI v 2+ installed and configured

What are provisioners?

Provisioners are helper components that help us configure resources launched via terraform. For example, terraform can launch an EC2 in AWS but terraform alone can not help us run scripts on EC2. This is what provisioners help with.

There are 2 types of provisioners-

  1. local-exec provisioner- allow to invoke local executable
  2. remote-exec provisioner — that runs scripts directly in the remote machine

Let’s get started with configuring our nginx using remote-exec provisioners

Step 1:

Let’s launch an EC2 via terraform.

Create a new file named main.tf. Have 2 blocks for provider and resource as below

Now run

terraform init

terraform apply -auto-approve

Cross check that this has launched an EC2 instance in our account

Step 2:

Let’s modify the code to install nginx via provisioner.

We will be installing nginx via remote-exec, for which we will need a key pair to connect to the server. Goto console-> EC2-> Key Pairs

Create a new key pair , download it and place it in the same folder as main.tf

Modify the tf to include provisioner

Now run

terraform apply -auto-approve

After apply is successful, open the console , get the public IP

Verify that nginx is up and running

Congratulations you have configured your nginx via terraform remote-exec

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.