Use AWS CodeBuild & CodePipeline to Automate Deployment to Elastic Beanstalk
AWS has its own build and deployment offerings, called CodeBuild and CodePipeline respectively, and naturally, they are integrated with the other AWS services. CodePipeline connects your GitHub repository with CodeBuild and Elastic Beanstalk so that any update to your repository is built and deployed automatically to Elastic Beanstalk.
In this post we will go through the process of integrating AWS CodeBuild and CodePipeline with our Elastic Beanstalk app.
Setting up CodeBuild
Most projects will have some sort of a build step before they are ready to be deployed to production, and it usually involves writing the output files that will be executed in the production environment. CodeBuild takes care of that part of the continuous deployment process.
The commands to execute in CodeBuild are described in a special file, called
buildspec.yml
that should be at the root of your codebase. I’ll go through this file shortly, but let’s first create the build project in the CodeBuild console.
Click on the “Create project” button, and you will be pointed to this page:
Pick a name of your choosing for the build project, and optionally a description. In the “Source provider” field, choose GitHub to link your GitHub repository. If the repository is private, you can authorize CodeBuild to access it on your behalf.
For the environment section, choose Ubuntu as the operating system, and Docker for the runtime. Please note that this is the runtime of the build environment, not your server’s runtime. The build environment depends on what tools and commands you are using to build your app. You can even choose a “Base” runtime and install the dependencies yourself. For Hollowverse, we use a custom Docker image to provide the build tools we need. I’ve covered this image in some detail in a previous post (See “Building the environment image”).
For the build specification, we are going to write a custom
buildspec.yml
file.
CodeBuild will collect your output files and store them in S3, Amazon’s file storage service. The “Artifacts” section allows you to configure in which bucket the files will be stored and what name to use for the folder or ZIP file. The artifacts should be specified in
buildspec.yml
.
To perform the build and upload the files to your S3 bucket, CodeBuild will need to have some permissions. While you can create a service role and give it the required permissions yourself, it’s just easier to let CodeBuild handle that for you.
Click “Continue” to finish the process.
Now let’s write our
buildspec.yml
file! Remember, this should be at your project’s root directory.
Every
buildspec.yml
should at least specify a version
and a build command. If the project has artifacts, it should also specify an artifacts
section:
Comments
Post a Comment