AWS Express Server
Article Description
Guide to deploying express server to AWS Elastic Beanstalk.
Elastic Beanstalk is an AWS EC2 deploying your code similar which supports many languages including Node, Python, Java, Go, Docker etc.
This is the what services like Railway use for their deployments under the hood with Railway just making it easier to deploy with premade tempaltes.Using Beanstalk is also a cheaper alternative
This guide assumes you already have a Express server.You can also use this as guideline for generally deploying any type of app to AWS.
Good reference when creating REST API's here.
1. Set up a role in AWS IAM that has the roles in the screenshot
AdministratorAccess
AWSElasticBeanstalkMulticontainerDocker
AWSElasticBeanstalkWebTier
AWSElasticBeanstalkWorkerTier
2. Navigate to AWS ElasticBeanstalk dashboard and create a new application
-On the first page set a name and such. Platform should be Node (which ever version). Make sure to use the Sample Application (dont upload your own code yet), this way you can be sure the deployment config is correct.
-On the second page use the Beanstalk role created earlier
-You should now be able to click 'Skip to Review' at the bottom of the page
-On the Review page double check the Platform Software section is using Nginx
, then start the deployment
Config Deployment
-Copy the folder structure from this screenshot, starting from root
.ebextensions
folder is used to store your Beanstalk deployment config, this runs before your deployment.
playwright.config
is the config itself (you can name it whatever you want).I've used it to install additional packages that dont come preinstalled on the linux server.
below is the format for commands. AWS EC2 instances use yum for installing packages
commands: 01_install_npm: command: sudo yum install 'your dependencies'
Config Nginx
The Nginx config folder structure looks like this
.platform\nginx\conf.d\nginx.config
Github Action for CD
This Github work flow automates deployment to AWS whenever main
branch gets updated.
.github\workflows\aws.yml
name: Deploy master
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v2
- name: Generate deployment package
run: zip -r deploy.zip . -x '*.git*'
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v21
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: {your app name}
environment_name: {your environment name}
version_label: ${{ github.sha }}
version_description: ${{github.event.head_commit.message}}
region: us-east-1
deployment_package: deploy.zip
use_existing_version_if_available: true
wait_for_environment_recovery: 60