GV.

Tuesday, September 28 2021

How To Deploy Strapi To AWS Beanstalk Using AWS CodePipeline

Jump to the solution

Strapi is an OpenSource JavaScript CMS which can be hosted in our own servers. Let’s see how we can deploy Strapi to AWS BeanStalk using AWS CodePipeline

What is AWS BeanStalk?

AWS Elastic Beanstalk is an easy-to-use service for deploying and scaling web applications and services developed with Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

You can simply upload your code and Elastic Beanstalk automatically handles the deployment, from capacity provisioning, load balancing, auto-scaling to application health monitoring. At the same time, you retain full control over the AWS resources powering your application and can access the underlying resources at any time.

What is AWS CodePipeline?

AWS CodePipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change, based on the release model you define. This enables you to rapidly and reliably deliver features and updates. You can easily integrate AWS CodePipeline with third-party services such as GitHub or with your own custom plugin. With AWS CodePipeline, you only pay for what you use. There are no upfront fees or long-term commitments.

Deployment Instructions

  1. Setup your AWS BeanStalk with a sample NodeJs App

    1.1. AWS will provide you a sample app for setting up

  2. Setup your AWS CodePipeline

  3. Changes you need to do you in your source code

    3.1. Set your preferred NodeJS version in package.json. You can set the required version by setting up the engines property

    {
      "name": "my strapi web app",
      "scripts" {},
      "engines": {
        "node": "12.21.0",
        "npm": "^6.0.0"
      }
    }
    

    3.2. Creating a buildspec file. The main issue with deploying to beanstalk using codepipeline is, we need to build the Strapi Admin panel and deploy it.

    GitHub gist

     version: 0.2
     phases:
         install:
         runtime-versions:
         nodejs: 12
    
         pre_build:
             commands: - echo Installing source NPM dependencies... - npm install
    
         build:
             commands: - echo Build started on `date` - echo Compiling the Node.js code - npm run build
    
         post_build:
             commands: - echo Build completed on `date` - rm -rf node_modules
    
     artifacts:
         files: - '**/*'
    
  4. Deploy it and see if everything works 😄

Why do we need to set this buildspec?

Strapi Admin panel needs to be built and deployed to our beanstalk instances but we can’t build strapi admin in the beanstalk since the build steps needs atleast 1GB of RAM and if you happen to be deploying to a micro instance the build will fail.

So its better to build the admin panel in the build stage of the pipeline and copy the files to beanstalk. One additional think we need to is to delete the node_modules folder in the codepipeline before copying so we will beanstalk to do a npm install again.