AWS INFRASTRUCTURE

EC2 Web Server Deployment

Automated deployment of a fully configured Apache web server on Amazon EC2 using Python and Boto3.

EC2PythonBoto3ApacheSecurity GroupsUser Data

PROJECT OVERVIEW

OBJECTIVE

Automate the complete lifecycle of deploying a web server on AWS EC2, from security group creation to instance launch and configuration.

APPROACH

Used Python with Boto3 SDK to programmatically provision infrastructure, configure security, and deploy Apache web server with user data scripts.

OUTCOME

Production-ready web server accessible via HTTP/HTTPS with automated security configuration and custom landing page deployment.

ARCHITECTURE DIAGRAM

EC2 Architecture Diagram

SECURITY FEATURES

  • Security Group with restricted port access (80, 443, 22)
  • SSH key pair generation for secure access
  • Elastic IP for consistent public addressing
  • VPC isolation with Internet Gateway

KEY COMPONENTS

  • EC2 t2.micro instance (Free Tier eligible)
  • Apache HTTP Server with custom configuration
  • User Data script for automated setup
  • Python Boto3 for infrastructure automation

CODE SAMPLE

deploy_ec2_webserver.py
import boto3
from botocore.exceptions import ClientError

def create_security_group(ec2_client, group_name, description):
    """Create security group with HTTP, HTTPS, and SSH access"""
    try:
        response = ec2_client.create_security_group(
            GroupName=group_name,
            Description=description
        )
        security_group_id = response['GroupId']
        print(f'✓ Security Group Created: {security_group_id}')
        
        # Add inbound rules
        ec2_client.authorize_security_group_ingress(
            GroupId=security_group_id,
            IpPermissions=[
                {
                    'IpProtocol': 'tcp',
                    'FromPort': 80,
                    'ToPort': 80,
                    'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
                },
                {
                    'IpProtocol': 'tcp',
                    'FromPort': 443,
                    'ToPort': 443,
                    'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
                },
                {
                    'IpProtocol': 'tcp',
                    'FromPort': 22,
                    'ToPort': 22,
                    'IpRanges': [{'CidrIp': '0.0.0.0/0'}]
                }
            ]
        )
        return security_group_id
    except ClientError as e:
        print(f'✗ Error creating security group: {e}')
        return None

def launch_ec2_instance(ec2_client, security_group_id, key_name):
    """Launch EC2 instance with Apache web server"""
    user_data_script = '''#!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    echo "<h1>Cloud Engineer Portfolio - EC2 Web Server</h1>" > /var/www/html/index.html
    '''
    
    try:
        instances = ec2_client.run_instances(
            ImageId='ami-0c55b159cbfafe1f0',  # Amazon Linux 2
            InstanceType='t2.micro',
            KeyName=key_name,
            SecurityGroupIds=[security_group_id],
            UserData=user_data_script,
            MinCount=1,
            MaxCount=1,
            TagSpecifications=[
                {
                    'ResourceType': 'instance',
                    'Tags': [{'Key': 'Name', 'Value': 'WebServer'}]
                }
            ]
        )
        instance_id = instances['Instances'][0]['InstanceId']
        print(f'✓ EC2 Instance Launched: {instance_id}')
        return instance_id
    except ClientError as e:
        print(f'✗ Error launching instance: {e}')
        return None

def main():
    ec2_client = boto3.client('ec2', region_name='us-east-1')
    
    # Create security group
    sg_id = create_security_group(
        ec2_client, 
        'webserver-sg', 
        'Security group for web server'
    )
    
    # Launch instance
    if sg_id:
        instance_id = launch_ec2_instance(ec2_client, sg_id, 'my-key-pair')
        print(f'\n✓ Deployment completed successfully!')

if __name__ == '__main__':
    main()

DEPLOYMENT STEPS

01

Configure AWS Credentials

Set up AWS CLI with access keys and region

02

Install Dependencies

Install Python 3 and Boto3 SDK: pip install boto3

03

Create SSH Key Pair

Generate key pair for secure SSH access to instance

04

Run Deployment Script

Execute Python script to provision infrastructure

05

Verify Deployment

Access web server via public IP address

06

Monitor & Maintain

Use CloudWatch for monitoring and logging

KEY LEARNINGS

TECHNICAL SKILLS

  • • Infrastructure automation with Python and Boto3
  • • EC2 instance lifecycle management
  • • Security group configuration and best practices
  • • User data scripts for automated configuration
  • • AWS networking fundamentals (VPC, IGW)

BEST PRACTICES

  • • Use IAM roles instead of hardcoded credentials
  • • Implement least privilege security group rules
  • • Tag resources for better organization
  • • Enable CloudWatch monitoring for visibility
  • • Use Elastic IPs for production workloads