AWS Elastic Beanstalk app can deploy simply by using CodePipeline.
Below is a pipeline I created on CodePipeline for deploying apps to Elastic Beanstalk, S3, and Cloud. I just introduce CodePipeline this time.

This project’s directory structure is like this:
my-api
+ .elasticbeanstalk
| + config.yaml
+ application.py
+ requirements.txt
application.py and requrements.txt are simple Flask app. You can also read a Flask tutorial.
Before creating the deploy pipeline, you need to create your pipeline and Beanstalk app. Please refer to a creating pipeline tutorial and Beanstalk tutorial.
After you create a pipeline and minimal Beanstalk app, you need to set up a source where you get your code from. Below is the example using GitHub:

Next, you need to add a build step to do unit testing or build your app into binary code or archive. And also I specify Output artifacts as “api-artifact”. This is an archived file built in this step to use after steps. In this pipeline, a next deploy step will use this file. Below is my setting:

You need to create buildspec.yaml to run the build step on CodeBuild.
version: 0.2 | |
phases: | |
pre_build: | |
commands: | |
- pip install -r my-api/requirements.txt | |
build: | |
commands: | |
- python -m unittest discover -s my-api | |
artifacts: | |
files: | |
- '**/*' | |
base-directory: my-api |
The app is written with Python and include unit tests in a build step, and so add python -m unittest discover -s my-api under the build . A deploy step uses app artifact in the my-api directory, and so you need to base-directory: my-api to create an artifact under the my-api directory.
Next, since we want to deploy the app to Elastic Beanstalk. Below is a deploy step configuration:

So you add a configuration file for Elastic Beans like below:
# You must set this file into .elasticbeanstalk direcotry like this; | |
# .elasticbeanstalk/config.yaml | |
branch-defaults: | |
default: | |
environment: api-env | |
environment-defaults: | |
api-env: | |
branch: null | |
repository: null | |
global: | |
application_name: my-api | |
default_ec2_keyname: null | |
default_platform: python-3.6 | |
default_region: ap-northeast-1 | |
include_git_submodules: true | |
instance_profile: null | |
platform_name: null | |
platform_version: null | |
profile: eb-cli | |
sc: null | |
workspace_type: Application |
Then you just push your code to GitHub’s master branch, CodePipeline automatically builds it and deploy to Beanstalk.
Top comments (0)