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
AWS CodeCommitas the source provider on step 2 of 5
Select the repository we created in the steps above
masteras 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
Create projectto 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.
Skip deploy stageas we’ll configure this stage using CodeBuild in the next step.
Review summary and click on
Create pipelineto 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.
Editand add a new stage called
Add action groupand configure an AWS CodeBuild provider
Create projectto 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
envwith a value of
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.ymlin the root directory (this is what we configured for our build stage). For our deployments, we’ll use the
- Click on
Continue to pipelineand save the stage by clicking on
Saveto 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
Add action groupand 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
envvariable to have the value
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.
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://<bucket name>-<dev | prod>.s3-website-us-east-1.amazonaws.com/substituting the name of the bucket and provider region you specified in the
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.
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.
Clean up our development environment
serverless remove –stage dev serverless client remove –stage dev
Clean up our production environment
serverless remove –stage prod serverless client remove –stage prod
Optionally delete additional AWS resources through the console
- AWS CodePipeline pipeline and IAM role
- AWS CodeBuild project and IAM roles
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: