-
Notifications
You must be signed in to change notification settings - Fork 4
Docker Deployment Example: Elastic Beanstalk with CloudWatch
Prerequisites: Python; a local ssh key for AWS use
-
Visit AWS IAM via the AWS console. In IAM, create an AWS role for EC2 instances that both has access to Elastic Beanstalk and can write logs to CloudWatch. This role may include standard policies like
AmazonEC2ContainerRegistryReadOnly
,AWSElasticBeanstalkWebTier
,AWSElasticBeanstalkMulticontainerDocker
,AWSElasticBeanstalkWorkerTier
. It should also include a CloudWatch policy that has the actionslogs:CreateLogGroup
,logs:CreateLogStream
,logs:PutLogEvents
, andlogs:DescribeLogStreams
. -
Download the Elastic Beanstalk CLI with
pip install awsebcli --upgrade --user
. -
Create a local directory, e.g.
simplified_eb/
, to house project files and create supporting configuration files. All of the following steps take place inside this local directory.$ mkdir simplified_eb && cd simplified_eb $ touch Dockerrun.aws.json $ mkdir .ebextensions $ touch .ebextensions/01_cloudwatch_agent_config.config
-
Alter the two files you just created with the following content. Make sure to update
<YOUR-TARGET-TAG>
inDockerrun.aws.json
with the Docker image tag you would like to deploy.// Dockerrun.aws.json { "AWSEBDockerrunVersion": 2, "containerDefinitions": [ { "name": "circ-webapp", "image": "nypl/circ-webapp:<YOUR-TARGET-TAG>", "essential": true, "memory": 1536, "portMappings": [ { "hostPort": 80, "containerPort": 80 } ], "mountPoints": [ { "sourceVolume": "awseb-logs-circ-webapp", "containerPath": "/var/log/uwsgi" } ] } ] }
# .ebextensions/01_cloudwatch_agent_config.config files: '/etc/awslogs/config/application_log.conf' : mode: "000644" owner: root group: root content: | [/var/log/containers/circ-webapp/uwsgi.log] file=/var/log/containers/circ-webapp/uwsgi.log* log_group_name=`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/uwsgi/uwsgi.log"]]}` log_stream_name={instance_id} commands: restart_awslogs: command: service awslogs restart
-
Run
eb init
to configure the Elastic Beanstalk application. This will begin an interactive tool. Select your target region and create a new,Multi-container Docker
environment, using the latest version. For debugging purposes, we recommend enabling SSH into the EC2 instance. -
Use
eb create
to set the configuration for your target Elastic Beanstalk environment. This will kickstart another interactive tool, though you can also use CLI options. For example, if your application is calledsimplye-circulation
, you may run the following command with the appropriate environment name (e.g. dev, qa, prod)$ eb create simplye-circulation-[environmentname] \ --instance_type t2.small \ --instance_profile loggable-beanstalk \ --cname simplye-circulation-[environmentname] \ --vpc.id target-vpc-id \ --vpc.elbsubnets public-subnet-id-1,public-subnet-id-2 \ --vpc.ec2subnets private-subnet-id-1,private-subnet-id-2 \ --vpc.elbpublic \ --tags Project=SimplyE, Foo=Bar \ --keyname your-aws-ssh-keyname \ --scale 2 \ --envvars SIMPLIFIED_PRODUCTION_DATABASE="xxx",VAR_NAME_2="xxx"
See the
eb create
documentation for more information. Be sure to configure the environment with the requiredSIMPLIFIED_PRODUCTION_DATABASE
variable. -
Run
eb deploy --label "a-versioned-label" --message "Your human-readable version description"
to deploy your the container image described inDockerrun.aws.json
. Documentation foreb deploy
can be found here. -
Using the AWS console, check Elastic Beanstalk for application health and CloudWatch for uwsgi logs.
-
As you need to update the container version, make local changes to the image tag in
Dockerrun.aws.json
and redeploy witheb deploy
. Application environments can also be duplicated and scaled in Elastic Beanstalk, as needed.