Build and Deploy with AWS CodePipeline
Table of Contents
- Introduction
- GitHub Repo with Node.js Code
- Register AWS Account
- Create Elastic Beanstalk Application Running Node.js
- Create AWS Pipeline
- Config Pipeline with GitHub Repo
- Config Pipeline to Deploy to Elastic Beanstalk
- Check-in Code to GitHub
- Verify AWS Pipeline Building and Deploy
- Conclusion
- What's Next
Introduction
This blog will show how to use AWS CodePipeline to automate building and deploying code from a GitHub repo. Following are the steps:
-
GitHub repo with Node.js code
-
Register AWS account
-
Create Elastic Beanstalk application running Node.js
-
Create AWS Pipeline
-
Config Pipeline with GitHub Repo
-
Config Pipeline to deploy to Elastic Beanstalk
-
Check-in code to GitHub
-
Verify AWS Pipeline building and deploy
GitHub Repo with Node.js Code
You can create your own Node.js app, but in this blog, we'll use the Node.js app I already created.
sample aws nodejs app
Register AWS Account
Make sure you have created an AWS account.
Create Elastic Beanstalk Application Running Node.js
AWS CodePipeline can deploy code to Elastic Beanstalk or other AWS cloud platforms, but in this blog, we'll set up deployment to Elastic Beanstalk. For CodePipeline to deploy to Beanstalk, you'll have to create an application. Since we are deploying a Node.js app, we'll create an Elastic Beanstalk Node.js app as follows.
Create AWS Pipeline
You can go to AWS services to find CodePipeline and create your first AWS pipeline based on your region. For testing purposes, be aware to only create what you need.
Config Pipeline with GitHub Repo
Click Connect to GitHub, type your username and password for GitHub. Once GitHub authentication has passed, your GitHub repo is connected with AWS Code Pipeline. You'll have to provide which GitHub repo and what branch you want to set up with AWS CodePipeline.
Config Pipeline to Deploy to Elastic Beanstalk
After you set up the GitHub repo and branch in AWS Pipeline, you'll set up where to deploy your code. In this blog, we'll set up deployment to AWS Elastic Beanstalk as follows. Once you configure deployment to Elastic Beanstalk, whenever you check in your code to the GitHub repo's target branch, the AWS CodePipeline build process will kick off. If the build succeeds, the code will deploy to Elastic Beanstalk.
Check-in Code to GitHub
Check in to GitHub just like you normally do. Once you push to the remote repo, the AWS Pipeline build process will start.
Verify AWS Pipeline Building and Deploy
If everything works well, once you push code to the GitHub remote repo, your code will build and deploy to Elastic Beanstalk. However, something might go wrong during build or deploy. Following are things I noticed when I set up AWS Pipeline with GitHub.
buildspec.yml
buildspec.yml is an important file for AWS Pipeline to build and deploy to Elastic Beanstalk. Following is an example. You can reference the Build Specification Reference for CodeBuild for the full reference.
version: 0.2
artifacts:
files:
- "**/*"
nodecommand.config
nodecommand.config is used by AWS Elastic Beanstalk to run your app. The following example shows that when deploying to AWS Elastic Beanstalk, it should run the "npm start" command to start the app. For more detailed syntax, see the AWS documentation for nodecommand.config.
option_settings:
aws:elasticbeanstalk:container:nodejs:
NodeCommand: "npm start"
Conclusion
AWS CodePipeline is very powerful for continuous integration and continuous deployment. If you set up the master branch for production, once you check in, AWS will start to build and, if the build succeeds, will deploy to Elastic Beanstalk. You might also create lower environments — development, QA, or staging — using AWS Pipeline, if you wish to move all your code base, build, and deploy to the cloud.
About cost: once you set up the AWS CodePipeline, the cost is one active pipeline at $1/month. AWS will also set up CodeBuild, which is pay-as-you-go.
What's Next
Now that we know how to use AWS CodePipeline with GitHub, next I'd like to try other code repos and see if they work with AWS as well. Also, I'd like to try not just a simple Node.js app, but something with Express.js or Nest.js, or perhaps deploy a .NET app, React, or ASP.NET application.