Deploy an EC2 Web Server with Python HTTP Server

By Pulumi Team
Published
Updated

The Challenge

You need a simple web server running on EC2 for development, testing, or hosting a basic website. This is the most straightforward way to get a web server running on AWS without external dependencies or container tooling.

What You'll Build

  • EC2 instance running Amazon Linux 2023
  • Security group allowing inbound HTTP traffic
  • Python HTTP server serving on port 80
  • Public IP and DNS hostname exported for access

Neo Try This Prompt in Pulumi Neo

Run this prompt in Neo to deploy your infrastructure, or edit it to customize.

Best For

Use this prompt when you need a simple web server for testing, development, or hosting basic HTML content. Perfect for learning how EC2, security groups, and user data scripts work together, or for quick prototypes that do not need complex infrastructure.

Architecture Overview

This deployment creates a minimal web server on AWS using a single EC2 instance. The architecture has three components: an AMI lookup to find the latest Amazon Linux 2023 image, a security group that controls network access, and the EC2 instance itself with a user data script that starts a web server at boot.

The AMI lookup ensures you always deploy on the latest version of Amazon Linux 2023 without hardcoding an image ID that would become outdated or fail in different regions. The security group acts as a virtual firewall, allowing inbound HTTP traffic on port 80 while blocking everything else. This is the minimum network configuration needed for a public-facing web server.

The user data script runs at instance launch and handles all server setup. It creates an HTML file and starts Python’s built-in HTTP server to serve it on port 80. Python comes pre-installed on Amazon Linux, so there are no additional packages to install. This approach is intentionally simple and works well for demos and testing, though production workloads would typically use nginx or a similar server.

AMI Lookup

Rather than hardcoding an AMI ID (which varies by region and becomes outdated), the deployment uses the EC2 AMI lookup feature to find the latest Amazon Linux 2023 image for x86_64 architecture. This makes the deployment portable across AWS regions and ensures you always start with a patched, current image.

Security Group

The security group defines inbound rules that allow TCP traffic on port 80 from any source. This is the equivalent of a firewall rule that permits HTTP access from the internet. Outbound traffic is allowed by default, which the instance needs to download packages or reach other AWS services. You can tighten these rules for production use.

EC2 Instance and User Data

The t2.micro instance is eligible for the AWS Free Tier, making this deployment nearly free for experimentation. The user data script is a shell script that runs once at first boot, creating the HTML content and starting the Python HTTP server. The instance receives a public IP address and DNS hostname, both of which are exported as stack outputs so you know where to access the server.

Common Customizations

  • Switch to nginx: Replace the Python HTTP server with nginx for better performance, static file serving, and reverse proxy capabilities needed in production.
  • Add SSH access: Extend the security group to allow TCP port 22 and provide an SSH key pair so you can log into the instance for debugging or manual configuration.
  • Use an Elastic IP: Assign an Elastic IP address so the public IP persists across instance stop/start cycles, which is important if other services reference the IP.
  • Increase instance size: Move to a larger instance type (t3.small, t3.medium) for workloads that need more CPU or memory than t2.micro provides.