---
title: "Using Terraform Provisioner to Deploy a Web Server"
description: "In this hands-on tutorial, deploy a web server to the AWS cloud environment with the AWS EC2 Terraform Provisioner!"
canonical: "https://adamtheautomator.com/terraform-provisioner/"
---

# Using Terraform Provisioner to Deploy a Web Server

> In this hands-on tutorial, deploy a web server to the AWS cloud environment with the AWS EC2 Terraform Provisioner!

Source: https://adamtheautomator.com/terraform-provisioner/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Using Terraform Provisioner to Deploy a Web Server](https://adamtheautomator.com/wp-content/uploads/2022/03/Deploy-a-Web-Server-with-the-AWS-EC2-Terraform-Provisioner.jpg)

# Using Terraform Provisioner to Deploy a Web Server

[![](https://secure.gravatar.com/avatar/2d7cef07ef659b643023a33acb03a593ea4aa235dd7dbd9b03b8e3f47133212c?s=192&d=mm&r=g)Michael Thanh](https://adamtheautomator.com/author/michael-thanh/)5 April 20225 min. read

Categories: [DevOps](/category/devops/)

Tags:[AWS EC2](/tag/aws-ec2/)[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Cloning the Terraform Code](#cloning-the-terraform-code)
*   [Examining the Terraform Files](#examining-the-terraform-files)
*   [Putting Terraform Provisioner to Work](#putting-terraform-provisioner-to-work)
*   [Testing the Apache Server](#testing-the-apache-server)
*   [Conclusion](#conclusion)

Are you tired of creating infrastructure for your websites and projects manually? Have you been using scripting languages and feeling down because they lack the flexibility and ease of sharing?

You came to the right place! A [Terraform Provisioner](https://www.terraform.io/language/resources/provisioners/syntax), in combination with PowerShell, can help automate infrastructure creation. In this guide, you will learn how to use Terraform Provisioners to create infrastructure for you. Dive in to get started.

## **Prerequisites**

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   An AWS account with powers to create resources. If you don’t have an AWS account, a **[free tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all)** account is available.
*   An EC2 instance with Terraform [installed](https://www.ktexperts.com/how-to-install-terraform-in-amazon-ec2-instance/).
*   An [installed](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and configured **[AWS CLI](https://aws.amazon.com/cli/)**.

Related:[Building an AWS VPC with Terraform Step-by-Step](https://adamtheautomator.com/terraform-vpc/#Building_the_Terraform_Configuration_for_an_AWS_VPC)

## Cloning the Terraform Code

Terraform is an open-source tool that codifies APIs into declarative configuration files. Team members can share the configuration files amongst the team. You and your team can treat the Terraform configuration files as code: edit, review, and; version them.

In this article, you will learn about Terraform and its configuration files by getting your hands dirty by setting up an Apache server on your AWS EC2 instance.

You will kick off this guide by cloning some Terraform code you will need later. Follow the steps below:

1\. Open an [SSH](https://adamtheautomator.com/powershell-ssh/) session to an EC2 instance.

Related:[Getting Started using SSH with PowerShell](https://adamtheautomator.com/powershell-ssh/)

2\. Once logged in, install Git with the following command so you can clone the example repository: `sudo yum install -y git`

![Installing Git on your system](https://adamtheautomator.com/wp-content/uploads/2022/03/image-554.png)

Installing Git on your system

3\. With Git installed, it is time to clone the GitHub repository to your current local repository. The repository holds code for creating an Apache server that will come in handy later.

```bash
git clone https://github.com/Adam-the-Automator/ata-aws-iam.git
```

![Cloning the repository](https://adamtheautomator.com/wp-content/uploads/2022/03/image-555.png)

Cloning the repository

## Examining the Terraform Files

Now that you have cloned the repository to your local system, it is time to examine the files you cloned to understand their purposes.

Run the `cd` command to navigate into the working directory for the local repository and run the `ls` command.

```bash
cd ata-apache
ls
```

The `ls` command lists three files in it: _main.tf, README.md, and setup.tf._

![Previewing the ata-apache directory](https://adamtheautomator.com/wp-content/uploads/2022/03/image-556.png)

Previewing the _ata-apache_ directory

Start by examining the _main.tf_ file with the `cat` command that shows the contents of the _main.tf_ file.

```bash
cat main.tf
```

When you check the file’s contents, you can see that it is responsible for creating an EC2 VM (`aws_instance`) and giving it many properties. Such as:

*   The [image](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html) you will install on your VM: `data.aws_ssm_parameter.webserver-ami.value`.
*   A public IP address: `associate_public_ip_address = true`.
*   The VM instance type: `t3.micro`.
*   The key pair you want to use for this VM: `aws_key_pair.webserver-key.key_name`
*   A few security groups: `aws_security_group.sg.id`.

Below you can see the output of the _main.tf_ file.

![Previewing the main.tf file.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-557.png)

Previewing the _[main.tf](http://main.tf)_ file.

The second file in the directory is the _setup.tf_ file. Again, run the cat command below to check the contents of the _setup.tf_ file.

```bash
cat setup.tf
```

The file contains a few configurations worth examining. As you don’t want to miss anything, a few screenshots will come next, highlighting different relevant parts.

The primary focus of this guide is Terraform provisioners. So you will want to pay special attention to the `provisioner` block present in _setup.tf_.

When inspecting the code, you will see the `remote-exec` keyword, meaning this Terraform provisioner is a remote one. The `remote-exec` keyword allows you to execute commands on the remote host: your web server, an EC2 instance.

Next, you will see the `inline` section, which lists commands to run on your newly created VM. For this example, the commands will be for:

*   Installing Apache **(**`install httpd`**)** and starting the webserver **(**`systemctl start httpd`**);**
*   Creating an _index.html_ file that will have its contents displayed when testing the server. The file contains the text to be shown: `My Test Website With Help From Terraform Provisioner`;
*   Moving the _index.html_ file to _/var/www/html/ –_ Apache web root directory – after provisioning.

![Previewing the provisioner block](https://adamtheautomator.com/wp-content/uploads/2022/03/image-558.png)

Previewing the provisioner block

Next, comes the `connection` block: this block tells Terraform what kind of connection to make when it’s running these commands. In this case, the connection type is `ssh`**,** and the username is `ec2-user`.

The `private_key` is of the typical `.ssh/id_rsa` [format](https://www.thedigitalcatonline.com/blog/2018/04/25/rsa-keys/) from Linux. This line creates a file called `~/.ssh/id_rsa` and puts the AWS EC2 user’s PEM-encoded RSA key in it. Since you are SSHing into the EC2 with the PEM-encoded RSA key, the host has access to execute commands. Terraform will grab the contents from the variable declared earlier **(**`associate_public_ip_address = true`).

![previewing the connection block](https://adamtheautomator.com/wp-content/uploads/2022/03/image-559.png)

previewing the connection block

The last block is the `tags` block. And it gives the resource you just created the name `webserver`.

![viewing the tags block](https://adamtheautomator.com/wp-content/uploads/2022/03/image-560.png)

viewing the tags block

## Putting Terraform Provisioner to Work

Now that you have checked the work laid out in the code, you will run Terraform commands to see it in action.

1\. Run the `terraform init` command to download any [plugins](https://www.terraform.io/cli/plugins) you need and initialize the state. If everything goes well, you will see no errors.

```bash
terraform init
```

![Initializing the Terraform working directory](https://adamtheautomator.com/wp-content/uploads/2022/03/image-561.png)

Initializing the Terraform working directory

2\. The `terraform validate` command checks if you don’t have syntax errors in your configuration. The command will allow you to catch any mistakes, and it’s a good practice to run this command before applying any changes.

```bash
 terraform validate
```

![Validating the code](https://adamtheautomator.com/wp-content/uploads/2022/03/image-562.png)

Validating the code

3\. Run the `terraform plan` command to look over the changes you made in your configuration and see what Terraform is about to do. This command gives you a preview of what Terraform intends to do. Running the `plan` command before applying changes is helpful. It allows you to make sure everything will work as you expect.

```bash
terraform plan
```

Some of the output you will get:

*   Lines starting with **+:** indicate that Terraform will create one or more resources;
    
*   A line with **Plan: 7 to add, 0 to change, 0 to destroy**. Meaning:
    

**7 to add:** indicates that Terraform plans to create seven resources.

**0 to change:** Terraform does not plan to change any existing resources.

**0 to destroy**: means that Terraform does not intend to destroy any existing resources.

![Previewing terraform planning output.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-563.png)

Previewing terraform planning output.

4\. Run the `terraform apply` command to apply all the changes you made in your configuration. Terraform will create any resources marked with + in the plan. Enter **yes** when it asks to **Enter a value** to continue.

```bash
terraform apply
```

![Previewing terraform apply output.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-564.png)

Previewing terraform apply output.

## Testing the Apache Server

Now that Terraform has done its magic, it is time to test the resource you just instructed Terraform to create.

Once the `terraform apply` process completes, you will see a `Webserver-Public-IP` displayed in your AWS CLI. The `httpd` service will be listening on this IP address, which is bound to your EC2 instance on its creation.

> _The resulting IP address is ephemeral. The IP will change if the instance gets re-created for whatever reason, failure, for example._

Take note of this IP address. You will need to access the test page with this IP address.

![Previewing the Webserver-Public-IP](https://adamtheautomator.com/wp-content/uploads/2022/03/image-565.png)

Previewing the Webserver-Public-IP

You will need to test the server to ensure it is working correctly by running the command below to check your Apache server version.

```bash
httpd -v
```

You should get similar to the following output.

![Checking your Apache server version.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-566.png)

Checking your Apache server version.

Copy and paste the public IP address of the EC2 instance you noted earlier into your browser and press **Enter**. You should see the **My Test Website With Help From Terraform Provisioner** page displayed in your browser. These outputs confirm that your Apache server is working properly.

![Previewing the My Test Website With Help From Terraform Provisioner page.](https://adamtheautomator.com/wp-content/uploads/2022/03/image-567.png)

Previewing the My Test Website With Help From Terraform Provisioner page.

Related:[How to Set up an Apache Docker Container](https://adamtheautomator.com/apache-docker/)

## Conclusion

Throughout this tutorial, you’ve learned how the Terraform provisioner works. You also learned how to create a web server with help from Terraform provisioner.

You can now start using what you learned here in your own infrastructure. You can use it with AWS, Azure, or on-premise resources. The choice is yours.

What’s next for you? Perhaps you will want to learn [how to secure your Apache server](https://www.tecmint.com/apache-security-tips/).

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-provisioner%2F&text=Using%20Terraform%20Provisioner%20to%20Deploy%20a%20Web%20Server)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-provisioner%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-provisioner%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/07/aws_launch_template.jpg)

### [Getting Started with Terraform aws\_launch\_template](/aws-launch-template/)

Discover how to use the aws\_launch\_template in Terraform to quickly provision EC2 instances and cut down on manual configurations!

![](https://adamtheautomator.com/wp-content/uploads/2023/02/terraform-best-practices.jpg)

### [Ensuring Success by Learning Terraform Best Practices](/terraform-best-practices/)

Discover Terraform best practices and ensure that your DevOps environment is being utilized safely and securely to it’s fullest potential!

![](https://adamtheautomator.com/wp-content/uploads/2022/10/Getting-Started-with-AWS-EC2-Autoscaling.jpg)

### [Getting Started with AWS EC2 Autoscaling](/ec2-autoscaling/)

Learn how to get started with AWS EC2 autoscaling for virtual machines and make sure that your application has enough resources when it needs it!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
