A step-by-step Infrastructure as Code (IaC) project that deploys a PHP-MySQL application on AWS using Terraform. This project provisions the required AWS infrastructure, deploys the PHP application on an EC2 instance, stores application files in Amazon S3, and connects the application to an Amazon RDS MySQL database.
This project demonstrates how to provision and configure AWS resources using Terraform, including:
- π₯οΈ Amazon EC2 for hosting the PHP application
- ποΈ Amazon RDS (MySQL) for the database
- π¦ Amazon S3 for application uploads/files
- π IAM Role for secure EC2 access to S3
- π₯ Security Groups for controlled network access
- π Terraform Remote Backend using S3
- π DynamoDB for Terraform State Locking
GitHub Repository
β
β
Clone Application
β
+----------------------+
| Amazon EC2 |
| Ubuntu + Apache2 |
| PHP Application |
+----------+-----------+
β
+-------------+-------------+
β β
β β
IAM Role for S3 MySQL Connection
β β
βΌ βΌ
+--------------+ +----------------+
| Amazon S3 | | Amazon RDS |
| Upload Files | | MySQL |
+--------------+ +----------------+
Terraform Backend
-----------------
Amazon S3 (State File)
+
DynamoDB (State Lock)
aws-deployment/
β
βββ provider.tf
βββ versions.tf
βββ variables.tf
βββ terraform.tfvars
βββ outputs.tf
βββ data.tf
βββ networking.tf
βββ security_group.tf
βββ keypair.tf
βββ iam.tf
βββ s3.tf
βββ ec2.tf
βββ rds.tf
βββ userdata.sh
β
β
βββ screenshots/
β
βββ README.md
Create a remote backend for storing the Terraform state.
Resources:
- Amazon S3 Bucket
Purpose:
- Store Terraform state remotely
- Prevent state corruption
- Enable state locking
Configure:
- AWS Provider
- AWS Region
- Terraform Version
Use a Terraform Data Source to automatically retrieve the latest Ubuntu LTS AMI.
Benefits:
- No hardcoded AMI IDs
- Always deploy the latest supported Ubuntu image
Generate or import an SSH Key Pair for secure access to the EC2 instance.
Purpose:
- SSH into EC2
- Server administration
Allow:
- SSH (22)
- HTTP (80)
- HTTPS (443)
Allow:
- MySQL (3306)
Only from the EC2 Security Group.
Provision an S3 bucket for storing application files.
Features:
- Versioning
- Server-side Encryption
Used for:
- Uploaded files
- Images
- Static assets
Create an IAM Role for the EC2 instance.
Permissions:
- Read objects from S3
- Upload objects to S3
Attach the role using an Instance Profile.
No AWS Access Keys are stored on the server.
Provision an Ubuntu EC2 instance.
Using User Data, automatically install:
- Apache2
- PHP
- PHP MySQL Extension
- Git
- MySQL Client
- Composer
Automatically:
- Clone the GitHub repository
- Copy application files to Apache's web root
- Configure file permissions
- Restart Apache
Provision a managed MySQL database.
Configuration:
- MySQL Engine
- Private Access
- Security Group
- Automated Backups
The database is accessible only from the EC2 instance.
From the EC2 instance:
- Connect to the RDS database
- Create the application database
- Import the SQL dump
- Verify the tables
Example:
mysql -h <RDS-ENDPOINT> -u admin -pTest:
- EC2 is running
- Apache is serving the application
- PHP is working correctly
- Database connectivity
- File uploads to Amazon S3
| Service | Purpose |
|---|---|
| Amazon EC2 | Host the PHP application |
| Amazon RDS | MySQL Database |
| Amazon S3 | Store uploaded files |
| IAM | Secure access to S3 |
| Security Groups | Network security |
| Amazon VPC | Networking |
| Terraform | Infrastructure as Code |
terraform initterraform fmtterraform validateterraform planterraform apply --auto-approveterraform destroy --auto-approveterraform graph | dot -Tpdf > graph.pdf- Store Terraform State in Amazon S3
- Enable State Locking using DynamoDB
- Use IAM Roles instead of AWS Access Keys
- Restrict MySQL access to the EC2 Security Group only
- Enable Server-side Encryption for S3
- Follow the Principle of Least Privilege
- Store sensitive values using Terraform Variables
Add screenshots during each deployment stage.
- Terraform Apply
- AWS Console
- EC2 Instance
- Apache Homepage
- PHP Application
- Amazon S3 Bucket
- Amazon RDS
- Successful Database Connection
By completing this project, you will learn how to:
- Provision AWS infrastructure using Terraform
- Configure a remote Terraform backend
- Deploy a PHP application on Amazon EC2
- Manage infrastructure using Infrastructure as Code
- Configure IAM Roles for secure AWS access
- Store application files in Amazon S3
- Connect an EC2 instance to an Amazon RDS MySQL database
- Secure AWS resources using Security Groups
- Automate server configuration using User Data
- Terraform Modules
- Multi-Environment Support (Development, Staging, Production)
- CloudWatch Monitoring
- AWS Secrets Manager for Database Credentials
- AWS Systems Manager (SSM) for EC2 Management
- AWS Backup for Amazon RDS
Sazad Ahemad
Tech Stack
- Terraform
- AWS
- Linux
- Apache2
- PHP
- MySQL
- Git
β If you found this project helpful, consider giving it a star!