Introduction
In this project, we deployed a PrestaShop server on AWS using a modular Terraform approach. By separating the infrastructure into three distinct modules VPC, EC2, and RDS we achieve better scalability, reusability, and maintainability of our Terraform code.
The following sections provide a step-by-step guide to setting up this infrastructure and installing PrestaShop on an Ubuntu-based EC2 instance connected to an RDS MySQL database. Screenshots from the AWS console will demonstrate the results of our deployment.
Modules Overview
1. VPC Module
The VPC module handles the networking layer for our infrastructure. It creates the Virtual Private Cloud, subnets, route tables, internet gateway, and necessary security groups. Kindly check the source code for more info
Code Snippet for VPC Module
module "vpc" {
source = "./modules/vpc"
vpc_name = var.vpc_name
cidr_block = var.vpc_cidr
public_subnets = var.public_subnets
private_subnets = var.private_subnets
enable_nat_gateway = true
}
Outputs from the VPC Module
- VPC ID
- Public and private subnet IDs
- Route table IDs
2. EC2 Module
The EC2 module provisions an Ubuntu-based instance, installs Apache, PHP, and PrestaShop dependencies, and configures the server. It also includes a security group to allow HTTP and SSH traffic.
Code Snippet for EC2 Module
module "ec2" {
source = "./modules/ec2"
instance_type = var.instance_type
ami_id = var.ami_id
key_name = var.key_name
subnet_id = module.vpc.public_subnet_ids[0]
security_groups = [module.vpc.ec2_sg_id]
user_data = file("user_data.sh")
}
The user_data.sh
script automates the installation of PrestaShop and its dependencies during instance initialization.
3. RDS Module
The RDS module provisions a MySQL database instance for PrestaShop. It includes a security group that allows access only from the EC2 instance's security group.
Code Snippet for RDS Module
module "rds" {
source = "./modules/rds"
engine = "mysql"
engine_version = "8.0"
instance_class = "db.t3.micro"
allocated_storage = 20
username = var.db_username
password = var.db_password
subnet_ids = module.vpc.private_subnet_ids
security_group_ids = [module.vpc.rds_sg_id]
}
Outputs from the RDS Module
- Database endpoint
- Security group ID
Deploying the Infrastructure
Step 1: Initialize Terraform
Run the following command to initialize your Terraform project:
terraform init
Step 2: Plan the Deployment
Generate an execution plan to verify the resources to be created:
terraform plan
Step 3: Apply the Configuration
Apply the configuration to provision the resources:
terraform apply
Approve the changes when prompted.
Step 4: Verify Resource Creation
Once the resources are created, use the AWS console to verify the following:
- VPC and subnets
- EC2 instance status
- RDS instance status and endpoint
Post-Deployment Configuration
1. Accessing the PrestaShop Server
- Navigate to the EC2 instance’s public IP or DNS in your browser.
- Verify the PrestaShop installation page appears.
2. Connecting to the Database
Use the RDS endpoint and credentials specified in your Terraform variables to connect the EC2 instance to the database.
3. Replacing Default Apache Page
Ensure the Apache default page is replaced by PrestaShop:
sudo rm /var/www/html/index.html
sudo mv /var/www/html/prestashop/* /var/www/html/
Restart Apache:
sudo systemctl restart apache2
4. Adjusting PHP Version
PrestaShop requires PHP 7.4. Use the following commands to install PHP 7.4:
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php7.4 libapache2-mod-php7.4 php7.4-mysql
sudo update-alternatives --set php /usr/bin/php7.4
sudo systemctl restart apache2
Screenshots of Created Resources
1. VPC Resources
- VPC details (Route tables, subnets)
2. EC2 Instance
Show:
- EC2 instance details (public IP, security group)
3. RDS Instance
Include:
- RDS instance details (endpoint, security group)
- Connectivity test from EC2 instance
Testing EC2 DNS
Conclusion
By following this modular approach, we successfully deployed a scalable and maintainable PrestaShop server on AWS. The use of Terraform modules ensures the infrastructure is reusable and easy to manage. Screenshots from the AWS console validate the successful creation of resources.
Feel free to share your feedback or enhancements for this project in the comments section!
Top comments (0)