Automated deployment of a fully configured Apache web server on Amazon EC2 using Python and Boto3.
Automate the complete lifecycle of deploying a web server on AWS EC2, from security group creation to instance launch and configuration.
Used Python with Boto3 SDK to programmatically provision infrastructure, configure security, and deploy Apache web server with user data scripts.
Production-ready web server accessible via HTTP/HTTPS with automated security configuration and custom landing page deployment.

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()Set up AWS CLI with access keys and region
Install Python 3 and Boto3 SDK: pip install boto3
Generate key pair for secure SSH access to instance
Execute Python script to provision infrastructure
Access web server via public IP address
Use CloudWatch for monitoring and logging