Terraform Configuration for AWS VPC with EC2 Instance This repository contains a Terraform configuration to set up a basic AWS Virtual Private Cloud (VPC) infrastructure. This setup includes an Internet Gateway, Route Table, Subnet, Security Group, EC2 instance with an Elastic IP, and associated networking components. The EC2 instance is provisioned with Apache web server installed and serves a simple webpage.
Prerequisites
Before applying this configuration, ensure you have the following:
- Terraform installed on your machine. You can install Terraform from here.
- AWS CLI configured with the appropriate credentials. You can configure it using
aws configure
. - An SSH key pair in your AWS account. The key pair name should match the value of
key_name
in the configuration (terraformkeypair
in this case). You can create one from the AWS Console under EC2 > Key Pairs.
Resources
This Terraform configuration sets up the following resources:
- VPC: A Virtual Private Cloud (VPC) with a CIDR block of
10.0.0.0/16
. - Internet Gateway: Allows the VPC to communicate with the internet.
- Route Table: Configures routes to allow outbound internet traffic via the Internet Gateway.
- Subnet: A subnet with a CIDR block of
10.0.1.0/24
, located inus-east-1a
. - Security Group: A security group with the following inbound rules: Allow HTTP (port 80) from anywhere.
Allow HTTPS (port 443) from anywhere.
Allow SSH (port 22) from anywhere.
Allow IPv6 HTTPS (port 443) from anywhere.
Additionally, outbound traffic is allowed for both IPv4 and IPv6.
- Elastic IP (EIP): An elastic IP address that is associated with the EC2 instance.
- Network Interface: A network interface attached to the EC2 instance.
- EC2 Instance: A
t2.micro
EC2 instance using an Amazon Linux AMI, with Apache installed and configured to serve a simple web page.
Configuration
1. Virtual Private Cloud (VPC)**
The VPC resource creates a VPC with a CIDR block of 10.0.0.0/16
.
2. Internet Gateway
The Internet Gateway allows the VPC to connect to the internet.
3. Route Table
The Route Table includes routes for both IPv4 and IPv6 traffic, directing outbound traffic through the Internet Gateway.
4. Subnet
This configuration creates a subnet in the us-east-1a
availability zone.
5. Security Group
The security group allows inbound web traffic (HTTP, HTTPS) and SSH, while allowing all outbound traffic.
Set Inbound Rules
• Allow HTTP (port 80) from anywhere (IPv4). • Allow HTTPS (port 443) from anywhere (IPv4 and IPv6). • Allow SSH (port 22) from anywhere
Set Outbound Rules
Allow all outbound traffic for both IPv4 and IPv6.
6. Network Interface
This network interface is associated with the subnet and security group.
7. Elastic IP
An Elastic IP is created and associated with the network interface.
9. EC2 Instance An EC2 instance is created with the network interface attached. Apache is installed and configured to serve a simple web page.
Usage
- Clone this repository to your local machine.
- Initialize Terraform:
- Validate the configuration:
- Apply the configuration:
- Confirm the changes, and Terraform will begin provisioning your infrastructure.
Cleanup To destroy the infrastructure created by this configuration, run:
This Terraform configuration sets up a basic VPC infrastructure with an EC2 instance and associated networking components. When deploying this configuration, make sure to:
1. Replace placeholders: Customize the CIDR blocks, region, AMI ID, and key pair name based on your environment.
2. Harden security: Restrict access where possible, use private subnets for internal resources, and apply the principle of least privilege to security groups and IAM roles.