DEV Community

Cover image for DEPLOYING WEB SERVER WITH TERRAFORM
Oluwaseun Olaleye
Oluwaseun Olaleye

Posted on

2

DEPLOYING WEB SERVER WITH TERRAFORM

Introduction

This guide provides a step-by-step explanation of how to use Terraform to deploy an AWS EC2 instance with a security group that allows HTTP traffic. The configuration automates the setup of an Apache web server on the instance.

Prerequisites

Before running the Terraform script, ensure you have the following:

  1. An AWS account.

  2. Terraform installed on your system.

  3. AWS CLI configured with appropriate credentials.

Terraform Script Breakdown

Below is the Terraform script used to create an EC2 instance and security group:

  1. Provider Configuration

Image description

The provider block specifies AWS as the cloud provider and sets the region where the resources will be deployed.

  1. EC2 Instance Configuration

Image description
This resource creates an EC2 instance with the following configurations:

  • Uses an Amazon Linux 2 AMI (ami-08b5b3a93ed654d19).

  • Assigns a t2.micro instance type (free-tier eligible).

  • Attaches a security group.

  • Runs a startup script that installs Apache, starts the web server, and creates a simple web page.

  1. Security Group Configuration

Image description

This security group:

  • Allows inbound HTTP traffic on port 80 from any IP (0.0.0.0/0).

  • Allows all outbound traffic.

Deploying the Terraform Script

Follow these steps to deploy the resources:

  1. Initialize Terraform (downloads required provider plugins):

Image description

  1. Preview the execution plan:

Image description

  1. Apply the configuration (creates resources in AWS):

Image description

4. Verify the Instance:

Image description

  • Copy the public IP from the output or AWS Console.

Image description

Destroying the Infrastructure

To delete the created resources and avoid charges, run:

Image description

Conclusion

This Terraform script automates the deployment of an EC2 instance running an Apache web server. By using infrastructure as code, you can quickly set up and manage AWS resources efficiently.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay