Guide: Deploying Container on AWS Elastic BeanStalk
I would say that successfully deploying a Docker container into Elastic Beanstalk is the first step to an amazing future with Amazon Web Services. Elastic Beanstalk abstracts the details of scaling, load balancing, and capacity provisioning away from the developer so that we can focus on writing clean code. The code base for this article will be the same code base for the article series Guid to Deploying K8S on AWS. Instead of deploying multiple containers, the client image will only be deployed.
For this tutorial to run smoothly, these prerequisites are needed.
- aws-cli installed with set up credientials
- eb command line installed — https://github.com/aws/aws-elastic-beanstalk-cli-setup
We will work inside the /bc_api/client/
directory. In general, all eb
commands should be in directory that is home to the Dockerfile.
Inside that directory, run the command
eb init -p docker — region <region> <app_name>
so that an Elastic Beanstalk instance is created with the platform being docker. This creates the ./elasticbeanstalk
folder that configures our instance. To switch platforms, run the command
eb platform select
this will be needed if an invalid platform error occurs. This has a higher percentage of happening if the command
eb init
is ran instead and set to default values. To test this locally, run the command
eb local run — port <client_port>
to build and immediately start the UI on client_port.
It is way faster to see if the container runs locally before waiting a long time deploying our container. If the eb
local instance starts, we can begin to deploy it on Elastic Beanstalk. The command
eb create <env_name>
to name our new environment (like branch) or run
eb create
to go through the init questions. This create command will spawn a variety of services needed to support our application.
This output shows us that our Elastic Beanstalk has created a security group, load balancer, and an auto scaling launch config services, and there will be more services made.
After the setup is complete, the command
eb open
will open our default browser to our custom AWS endpoint to display the application.
As we can see — Success!!
Key Commands
eb list
will show all our environment names, similar to git branch -a.
eb use <env_name>
will switch between environments, similar to git checkout <branch>.
eb status
will tell us the health and details of the Elastic Beanstalk instance.
Pitfalls
For eb
to run smoothly our directory must have at least a Dockerfile
or Dockerrun.aws.json
in the root. This does not include dockerfile.
Although these two filenames vary by a capital D,
our deployment will fail.
AWS Elastic Beanstalk does not support multi-stage Docker image builds. So dockerfiles like the one below will fail.
This is because eb
will try to pull the image as
and builder,
which are not images. Thus, eb
will run into cannot find image
errors. To fix this sad issue, we simply have to change our dockerfile to the one below.
In this new dockerfile, stage one is referenced simply by its index, 0. Stage one no longer has an alias and we use the command COPY --from=0.
It is nice to see that AWS does not make deploying single containers difficult; instead, it is a fun process.