⛰️Terraform & AWS
Write-up for creation of Terraform configuration to automate Prestashop, Database, S3 Bucket & configuring VPC/subnets for it
Overview
Currently, I'm learning about AWS services and offerings. In order to put this into practice I decided to use my knowledge about E-Commerce sites (specifically PrestaShop) and spin up something that could be used in a small environment. I wanted a publicly accessible PrestaShop store exposed to the internet for me to mess around with. I started to plan out my set up in AWS. Originally, this was the idea:
EC2 instance that runs PrestaShop
RDS used for the store database
S3 to host files and images for the store
This eventually evolved into adding security groups to the instances, creating IAM roles to access the S3 bucket, and creating a VPC (virtual private cloud) with public and private subnets

Terraform
I wanted to do this in a way that can be repeatable and customizable. I chose to use Terraform, as it works well with AWS. All you have to do is install AWS-CLI and Terraform, sign in to AWS-CLI and you are ready to go! I slowly built out my Terraform configuration, adding things where it was needed.

The start of the script builds out the networking portion, as we need to assign the instances after this is created.
This creates the VPC, Gateway, Subnets, and the private subnet group needed for the RDS
Next, the Security groups are created for the instances:
These allow traffic to each of the instances properly
We then create the IAM role for the S3 bucket
Then we create each of the instances, assigning them to the proper VPC, subnet, and security group
The instances spin up, and then it outputs the needed info to connect to each of them:
Python Script
Like I said earlier, I want this to be used by others to quickly spin up these instances. What isn't quick is changing these variables manually and potentially hard-coding your credentials accidentally. I decided to make a small Python script that accepts user input for these variables, then runs the actions needed to spin this up.
In order to do this, I created a file for specifically variables called terraform.tfvars. It has default values for each of the variables (I also put them into the variables.tf file just in case as well).
Most of these values should change when running this script. Here is the Python script that takes the user input and runs the appropriate commands:
These are all available in my GitHub repo as well
Here we can see the script asking the user for input:

And then it should run the required actions right away


and output the required information:

in order to tear this down quickly, cd into the AWS-Prestashop-Image directory and run terraform destroy

Issues
I ran into issues when trying to connect the EC2 and RDS. I was assigning the correct security groups, but I was still unable to connect to my database. That's when I learned about VPC's. I initially assumed all instances would automatically be under the same VPC. In AWS, you must explicitly assign instances to a VPC to ensure proper connectivity. So I added in resources for a VPC, deliberately assigning the instances to it.
In order to have a secure RDS, there needs to be separation between it and the EC2 instance. You don't want your database exposed on a public subnet. I created something similar to this, so I could separate the 2 and have a secure connection to the database. Once I implemented this, it was able to connect easily.
Costs associated
I do not have enough money to spin this up on a big expensive server, but I do have a free-tier AWS account. I was able to implement this by using the free-tier options for the EC2, RDS and S3 just fine. I would suggest others who want to test this do the same!
Potential Use Case
I got this idea from running an E-Commerce server at CCDC a while ago. There was a section at Regionals devoted to AWS, and it had something similar running in Docker. I wanted to recreate that so our Cybersecurity Club would be able to practice Cloud environment problems for the competition.
Other than that, this can absolutely be used by someone who just wants to spin up a quick Web Server in AWS. It's pretty flexible and lightweight, so you can definitely edit it to get it where you want it. The bonus of added security through subnetting and security groups automatically can be appealing as well.
Future Considerations
I'd like to expand this project to something like using Kafka to track orders, implementing a Data Lake, and other AWS services. I'd also like to fully set up PrestaShop on both HTTP and HTTPS using Docker (right now you still have to go through the setup process for PrestaShop).
Last updated