This project provides infrastructure as code (IaC) using Terraform to deploy a Python Flask e-commerce web application to AWS EC2. The application uses the local app-source/ directory which includes enhanced checkout functionality, and is deployed using a separate deployment script.
- EC2 Instance: Amazon Linux 2 instance running the Flask application
- Security Group: Configured for SSH (22), HTTP (80), HTTPS (443), and Flask app (5000) access
- Application Stack: Python 3, Flask, SQLAlchemy, Nginx (reverse proxy)
- Automation: User data script handles complete application setup
- Monitoring: Health check scripts and systemd service management
Before deploying, ensure you have:
-
Terraform installed (>= 1.0)
# macOS brew install terraform # Or download from https://terraform.io/downloads.html
-
AWS CLI installed and configured
# Install AWS CLI pip install awscli # Configure with your credentials aws configure
-
AWS Account with appropriate permissions:
- EC2 instance management
- Security group management
- IAM role/policy management
- VPC access
-
AWS Key Pair for EC2 access
# Create a new key pair (replace 'ecommerce-key' with your preferred name) aws ec2 create-key-pair --key-name ecommerce-key --query 'KeyMaterial' --output text > ecommerce-key.pem chmod 400 ecommerce-key.pem
-
Clone and setup the project (already done if you're reading this)
-
Configure variables:
cp terraform.tfvars.example terraform.tfvars # Edit terraform.tfvars with your specific values -
Deploy the infrastructure:
./deploy.sh apply
-
Deploy the application:
./deploy-app.sh
-
Access your application:
- The deployment will output the application URL
- Access via browser:
http://[instance-ip]:5000 - Or via Nginx proxy:
http://[instance-ip]
Edit terraform.tfvars to customize your deployment:
aws_region = "us-east-1" # AWS region
aws_account_id = "676206911983" # Your AWS account ID
project_name = "ecommerce-app" # Project name prefix
environment = "dev" # Environment tag
instance_type = "t3.micro" # EC2 instance type
key_pair_name = "ecommerce-key" # Your AWS key pair name
allowed_cidr_blocks = ["0.0.0.0/0"] # Restrict for better security
app_port = 5000 # Flask application port
# github_repo_url is no longer used - app deployed from local app-source/- Restrict CIDR blocks: Change
allowed_cidr_blocksfrom["0.0.0.0/0"]to your specific IP ranges - Key pair security: Ensure your
.pemkey file has proper permissions (400) - Instance size: Start with
t3.micro(free tier) and scale as needed
Step 1: Deploy Infrastructure
# Plan deployment (see what will be created)
./deploy.sh plan
# Deploy infrastructure
./deploy.sh apply
# Destroy infrastructure
./deploy.sh destroy
# Show help
./deploy.sh helpStep 2: Deploy Application from Local Source
# Deploy the Flask app from local app-source/ directory
./deploy-app.sh# Initialize Terraform
terraform init
# Plan deployment
terraform plan
# Apply configuration
terraform apply
# Destroy resources
terraform destroyUse the provided management script to monitor and control your deployed application:
# Check application status
./manage-app.sh status
# View application logs
./manage-app.sh logs
# Restart application services
./manage-app.sh restart
# Run health check
./manage-app.sh health
# Show help
./manage-app.sh helpAfter successful deployment, Terraform will output:
- instance_id: EC2 instance identifier
- instance_public_ip: Public IP address
- instance_public_dns: Public DNS name
- application_url: Direct URL to access the Flask app
- ssh_connection_command: SSH command to connect to the instance
- security_group_id: Security group identifier
ecommerce-terraform-deploy/
βββ main.tf # Main Terraform configuration
βββ variables.tf # Variable definitions
βββ outputs.tf # Output definitions
βββ terraform.tfvars.example # Example variable values
βββ user-data.sh # EC2 user data script (infrastructure only)
βββ deploy.sh # Infrastructure deployment script
βββ deploy-app.sh # Application deployment script
βββ manage-app.sh # Application management script
βββ README.md # This documentation
βββ .gitignore # Git ignore rules
βββ app-source/ # Local e-commerce application with checkout
βββ unwrap/ # Flask application package
βββ requirements.txt # Python dependencies
βββ run.py # Application entry point
βββ products.csv # Sample product data
- User registration and authentication
- Product catalog with sample products
- Shopping cart functionality
- Complete checkout and order confirmation flow
- SQLite database (SQLAlchemy)
- Bootstrap frontend
- Systemd Service: Application runs as a system service
- Nginx Reverse Proxy: Professional web server setup
- Health Monitoring: Automated health checks
- Log Management: Centralized logging with journalctl
- Auto-restart: Service automatically restarts on failure
-
SSH Key Not Found:
# Ensure your key pair exists and has correct permissions ls -la *.pem chmod 400 your-key-name.pem
-
Application Not Starting:
# Check logs using the management script ./manage-app.sh logs # Or SSH directly and check ssh -i your-key.pem ec2-user@[instance-ip] sudo journalctl -u ecommerce-app -f
-
Permission Errors:
# Make sure scripts are executable chmod +x deploy.sh manage-app.sh -
Terraform State Issues:
# If state is corrupted, you may need to import resources terraform import aws_instance.ecommerce_app [instance-id]
- Application logs:
sudo journalctl -u ecommerce-app - Nginx logs:
/var/log/nginx/error.log - Deployment logs:
/var/log/deployment.log - Cloud-init logs:
/var/log/cloud-init-output.log
- Modify files in the local
app-source/directory - Run the deployment script:
./deploy-app.sh - The script will copy updated files and restart the application
- Modify Terraform files as needed
- Run
./deploy.sh planto see changes - Run
./deploy.sh applyto apply changes
- Instance Type: Use
t3.microfor development (free tier eligible) - Storage: Default 20GB encrypted GP3 volume
- Region: Choose region closest to your users
- Scheduling: Consider using Lambda to start/stop instances on schedule
- Restrict Access: Update security group to allow access only from your IP
- Key Management: Store SSH keys securely and rotate regularly
- Updates: Regularly update the EC2 instance and application dependencies
- Monitoring: Consider adding CloudWatch monitoring and alerting
- SSL/TLS: Add HTTPS certificates for production use
- Terraform AWS Provider Documentation
- AWS EC2 User Guide
- Flask Application Documentation
- Original E-commerce App Repository
Feel free to submit issues and enhancement requests!
This project is licensed under the MIT License - see the original application repository for details.
Note: This deployment is configured for development/testing purposes. For production use, consider additional security measures, load balancing, database separation, and monitoring solutions.