Using Terraform to provision your first nginx instance on AWS

 We'll use provisioners as part of terraform resource generation to deploy an EC2 and run a custom script to install the nginx web server.

Terraform v0.13+ must be installed as a prerequisite.

Installed and configured AWS CLI version 2+

What are provisioners and what do they do?

Provisioners are terraform-based auxiliary components that assist us in configuring resources. Terraform, for example, can launch an EC2 on AWS, but it can't enable us run scripts on it. Provisioners can assist with this.

There are two sorts of provisioners: 

1:local-exec provisioner and remote-exec provisioner.

2:remote-exec provisioner — allows you to run scripts on a remote machine.

Let's get started setting our nginx server with remote-exec provisioners.

1st step:

Let's use Terraform to start an EC2.

Make a new file with the name main.tf. As shown below, there are two blocks for the provider and resource.


Run terraform init terraform apply -auto-approve to complete the process.


Step 2: Now we'll change the code to install nginx using the provisioner.

We'll use remote-exec to install nginx, therefore we'll need a key pair to connect to the server. Key Pairs can be found by going to console->EC2->Key Pairs.

Make a new key pair, download it, and save it in the same location as the main one.


Make a change to the tf to include provisioner.


Run the terraform apply -auto-approve command now.

After the application has been approved, open the console and obtain the public IP address.



Make sure nginx is up and working.


Congratulations, you've successfully configured nginx using terraform remote-exec.

Comments