Full Stack Serverless CI/CD On AWS With The Serverless Framework – Part 2

In Part 1 of the series, we used the Serverless Framework to describe a full stack serverless application consisting of a JS single-page-app, NodeJS serverless function and NoSQL backend datastore. In this post, we’ll walk through the creation of a continuous integration and delivery pipeline using AWS Developer tooling.

THIS POST CONTINUES FROM PART 1

STEP 4 – CREATE THE CI/CD PIPELINE

In the following steps, we’ll create a continuous integration and deployment pipeline to build (or rather package) our serverless application, deploy into our development environment, await approval to promote before deploying into production all using AWS CodePipeline and AWS CodeBuild.

Note: We won’t be using AWS CodeDeploy to deploy our serverless application. Instead we will use AWS CodeBuild so we can leverage the built-in deployment features of the Serverless Framework CLI.

  • Login into the AWS console and create a new AWS CodePipeline

    Give your new pipeline a name on step 1 of 5 and leave the rest as default

  • Tell our pipeline where our source code lives
    • Select AWS CodeCommit as the source provider on step 2 of 5
    • Select the repository we created in the steps above
    • Select master as the branch and leave the rest as default

  • Create a build stage to package our serverless application ready for deployment:
    • Select AWS CodeBuild as the build provider
    • Select the region you are using
    • Click on Create project to create a new AWS CodeBuild project that will package our serverless application
    • Provide a name and description for the new CodeBuild project
    • Configure the build environment to use the latest Ubuntu image (standard 2.0) leaving the rest of the options as default
    • Scroll to the bottom and click on Continue to CodePipeline. In the original window you should see the following.
    • Click on Skip deploy stage as we’ll configure this stage using CodeBuild in the next step.
    • Review summary and click on Create pipeline to complete the first part of the CI/CD pipeline setup.
    • After the pipeline is created it will automatically kick-off the first build and after a few minutes you should see the following

  • Add a new stage to the pipeline to deploy our serverless application into the development environment. We’ll use CodeBuild for the deployments to make use of the built-in deployment features of the Serverless Framework CLI.
    • Click on Edit and add a new stage called Deployment
    • Click on Add action group and configure an AWS CodeBuild provider
    • Click on Create project to create a new CodeBuild project
    • Provide a name and description and configure the same managed environment image as we used for our build stage.
    • Provide the environment variables to our CodeBuild instance so we can specify the serverless framework stage and configure the AWS CLI with API credentials to deploy resources into our AWS account.
    • Add an environment variable called env with a value of dev
    • Create SSM parameters for our AWS API credentials as we will want to avoid managing secrets in plaintext.
    • The final step in our deployment stage is to configure which buildspec file we want CodeBuild to invoke. By default, CodeBuild uses the file buildspec.yml in the root directory (this is what we configured for our build stage). For our deployments, we’ll use the buildspec-deploy.yml file instead.

    • Click on Continue to pipeline and save the stage by clicking on Done and the Save to save changes to the pipeline.
  • So we don’t automatically trigger a deployment into production we want to configure an approval stage in our pipeline. We will trigger an email notification to be sent requesting approval for the change to be promoted into production.
    • Edit the pipeline and add a new stage named Approval-Gate
    • Click on Add action group and configure an Manual approval action provider

    Follow the guide in AWS docs if you don’t have an SNS topic already configured

  • The final stage in our pipeline will be the deployment into production. This stage is identical to the development deployment stage with only the environment variables requiring change.
    • Edit the pipeline and add a new stage using the same steps as the previous deployment stage.
    • On the Environment variables step change the env variable to have the value prod

 

STEP 5 – TEST THE CI/CD PIPELINE

To test our pipeline works, trigger the pipeline using the Release change button from the console or push a change from your development machine to CodeCommit to invoke the continuous integration.

Once deployment into the development environment has completed successfully, the pipeline waits for approval before deploying our application into production.

Navigating to CloudFormation in the AWS console shows us the stack the Serverless Framework generated to provision, deploy and manage our development environment.

After testing the application functions correctly in dev, approve the request generated by the pipeline using either the linked emailed via SNS or by the waiting pipeline in the AWS console.

 

STEP 6 – TESTING THE SAMPLE APPLICATION

In this step we’ll test the sample application provided during this post. If you chose to develop your own, then just skip ahead to the next step.

  1. Open a browser and navigate to the URL of the web client the serverless-finch plugin deployed as part of our serverless application stack.

    Note: The URL of the single page app will be http://-.s3-website-us-east-1.amazonaws.com/ substituting the name of the bucket and provider region you specified in the serverless.yml file.

  2. Fill out the options and click on the Chaos button to randomly send a message to one of the numbers provided.

    Tip: Check your Twilio settings are configured correctly as described in the sample application walkthrough above.

  3. In the AWS console, navigate to the Lambda Management Console and view the configuration and invocations details of your serverless function.

 

STEP 7 – CLEANING UP OUR AWS RESOURCES

The Serverless Framework uses CloudFormation under the hood so cleaning up our AWS resources is as simple as issuing the remove command using the Serverless Framework CLI.

  1. Clean up our development environment
    serverless remove –stage dev
    serverless client remove –stage dev
    
  2. Clean up our production environment
    serverless remove –stage prod
    serverless client remove –stage prod
    
  3. Optionally delete additional AWS resources through the console
    • AWS CodePipeline pipeline and IAM role
    • AWS CodeBuild project and IAM roles

 

WRAPPING UP

In this post, we walked through the steps required to automate the delivery of a full stack serverless application on AWS using the Serverless Framework. Using serverless application frameworks provide an opinionated, repeatable and automated approach to build, deploy and run serverless applications and allow a consistent development toolchain no matter which cloud provider or language used.

For further reading check out the following links:

Enjoyed this blog?

Share it with your network!

Move faster with confidence