Multi Region Deployment with Laravel Vapor and Cloudflare
Hi, today I am going to show you how I've managed to have Laravel Vapor deployed in multiple regions, while having all the instances connecting to one RDS Database using Cloudflare Traffic Load Balancer.
This setup monthly cost is:
- Laravel Vapor: $39
- Cloudflare $20 for the Pro Plan (optional) + $20 for the Load Balancing (for 3 origin servers)
- AWS: this cost varies a lot depending on your traffic and application runtime as you get billed for each ms.
I don't go through the basics of the Laravel Vapor setup, but I will focus on sharing how to setup the multi region deployment. It can be automatized but I will keep it simple for educational purposes. If you need extra information about Laravel Vapor and how to set it up, I highly recommend Jack Ellis's course https://serverlesslaravelcourse.com.
Once you've finished setting up Laravel Vapor for your web application and got experience, you will need to create 3 more projects. The first one was to gain experience and it won't be longer used after this scenario is in place.
Each project needs to be based in the region of your choice, we use the following regions for this tutorial: us-west-1, eu-central-1 and ap-southeast-2 and name them like the following: website-us-west-1, etc..
. You will also need to create a Database from the Vapor dashboard and you will need to choose a public fixed database, so the other instances can connect to it as well. I would choose eu-central-1 to be the database region so you can have a head start with your GDPR compliance.
In your application, I would create a new folder named vapor. Inside, create three files matching your regions, us-west-1.vapor.yml, eu-central-1.vapor.yml and ap-southeast-2.yml. You can choose three different subdomains for each deployment or leave the domain blank, however you will need to set the base domain on the same region where the database is deployed like:
#eu-central-1.vapor.yml
id: -----
name: website-eu-central-1
environments:
production:
memory: 1024
cli-memory: 512
runtime: 'php-8.1:al2'
timeout: 10
domain:
- 'website.com' #to be added to only one region otherwise AWS will throw an error
- 'eu-central-1.website.com' #this is optional
warm: 2
build:
- 'COMPOSER_MIRROR_PATH_REPOS=1 composer install --no-dev'
- 'php artisan event:cache'
- 'npm ci && npm run prod && rm -rf node_modules'
It's now time to deploy all the regions. We need to run vapor deploy production
for as many regions you've setup. Copy the content of one region yml file into the vapor.yml
file and run vapor deploy production
. Once all the three regions are deployed, head over AWS Lamda and follow this:
- Select the first region from the top right corner, for example us-west-1
- Click Functions
- Click vapor-website-us-west-1-production
- Click Aliases
- Click production
- Copy the API endpoint hostname, for example:
randomcharacters.execute-api.us-west-1.amazonaws.com
and save it somewhere, a good place would be at the top of the yml file using a comment#randomcharacters.execute-api.us-west-1.amazonaws.com
You have to repeat the above for each region.
Now you will need to create a Cloudflare account, then head over to Traffic > Load Balancing and setup a new Load Balancer.
- In the hostname type
yourwebsite.com
without subdomain and click next.
Create a new Pool with
- Pool Name / Description: USA
- Origin Steering: Random
- Add one Origin giving the name of the AWS region, for example: us-west-1, for the Origin address you will need to go copy the Lambda function url that we've copied over before
randomcharacters.execute-api.us-west-1.amazonaws.com
- Set the weight to 1
- Open the Configure co-ordinates for Proximity Steering and drag the black dot to the region where the server is located, for example in this case it would be California. *please note: this dot is very tiny and at first you might find it difficult to find
- Add A Monitor, I called it
Website Frontend Check
and set a Health Check Region based on the server region location. - Click Save
You have to repeat the above for each region.
After creating all the pools, in Traffic Steering, select Proximity steering. You can now review the Load Balancer and click Save and Deploy
Conclusion
I believe I've mentioned everything here for you to have a multi region setup for your Laravel Vapor application. If you have any questions, I will do my best to respond them, however be mindful that I am a novice in this and I don't know much of AWS. This post is for educational purposes and might be reviewed for a production setup.