home
navigate_next
Blog
navigate_next

AWS Elastic Beanstalk - DevOps Approach

AWS Elastic Beanstalk - DevOps Approach
Madhumita Yadav
AWS Elastic Beanstalk - DevOps Approach

AWS Elastic Beanstalk is the fully managed service that lets us quickly deploy an application on to AWS and handles everything on its own including infrastructure provisioning. Think of it as a Platform as a Service (PasS) level offering. It allows Developers to take control of the deployment of their code but without spending time worrying about the underlying infrastructure (the “Ops”), thus speeding up their development cycles and providing faster feedback loops.

Production Ready Deployments

Elastic Beanstalk is quiet apt for an public facing production deployment application roll outs as it involves features those ensure -

  1. Appropriate security controls are in place
  2. Standard infrastructure as code methodology is followed
  3. Solution is architected to the usual levels of resilience
  4. Solution is scaled appropriately
  5. Solution utilises standard logging and monitoring mechanisms
  6. Resource controls are in place to restrict potential cost

Supported Platforms

Elastic Beanstalk supports Java, .NET, PHP, Node.js, Python, Ruby, Go, Docker and Multi-Container Docker on familiar servers such as Apache, Nginx, Passenger, and IIS.

Environment Tiers

  1. Web Server Environment: Used for a standard application that listens for and then processes HTTP requests, typically over port 80.
  2. Worker Environment: These are specialised applications that have a background processing task that listens for messages on an Amazon SQS queue. Worker applications post those messages to your application by using HTTP.

Cost

There is no additional charge for Elastic Beanstalk – you pay only for the AWS resources needed to store and run your applications.

Handling IaC approach

Elastic Beanstalk reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring. In order to leverage the full flexibility of the elastic beanstalk and utilising the plethora of settings one can use the AWS Command Line Interface (AWS CLI), or eb, a high-level CLI designed specifically for Elastic Beanstalk.

There are two parts to this -

Infrastructure Deployment for Beanstalk

Spinning up an new elastic beanstalk application manually involves -

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/GettingStarted.html

As, the above is way too manual , prone to error , inconsistency , involves system admins constant assistance , and not at all DevOps oriented; the need for IaC comes into picture.

Having familiar with the terraforms ability https://www.terraform.io/intro/vs/index.html for provisioning AWS infrastructure , it makes sense to choose it as an goto tool to get the task done.

One can create their own custom terraform modules using the terraform resources -

• aws_elastic_beanstalk_environment

• aws_elastic_beanstalk_application

, or can utilize few pre-build published modules from terraform repositories like -

https://registry.terraform.io/modules/cloudposse/elastic-beanstalk-environment/aws/latest

For an intensive list of all the elastic beanstalk namespaces , ref -

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html

Sample directory structure & code snippet -

Application code update, lifecycle management

After your environment is launched, you can then manage your environment and deploy new application versions. The following diagram illustrates the workflow of Elastic Beanstalk.

To achieve this, we can leverage any of the SCM tools capable to run deployment pipelines, with push-based deployment triggers. Here, I will demonstrate how to use Gitlab -

Firstly, Get the Gitlab Registration Token

Get the Gitlab Runner Registration token by clicking CI/CD in the settings of your Gitlab Repository.

Go to Runners, under specific runners, get the registration token and save it.

• Specify the Gitlab Variables

Click Settings → CICD → Variables, specify variables for the AWS Access/Secret keys for your AWS accounts -

AWS_ACCESS_KEY_ID=xxxxx

AWS_SECRET_ACCESS_KEY=xxxxxx

Setup Docker Gitlab Runner:

Prepare the Gitlab Runner

Create a config file for the Docker Gitlab Runner using the below command on the EC2 Server:

docker run --rm -t -i -v /opt/gitlab-runner/dev/config:/etc/gitlab-runner --name dev-gitlab-runner gitlab/gitlab-runner register

Provide the UserInput for:

  • Gitlab URL and Registration Token are saved in the previous step.
  • Executor: docker
  • Default Docker Image: python:2.7

Start the docker container with the config file

docker run -d --name dev-gitlab-runner --restart always \
-v /opt/gitlab-runner/dev/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:latest

Verify the docker container

docker ps

Gitlab CI/CD

Clone the Gitlab Repository which has the application source code and add the below files for configuring CI/CD:

vim .gitlab-deploy.sh

#!/usr/bin/env bash
# Install AWS CLI
pip install awsebcli --upgrade --ignore-installed
# Prepare deployment
mkdir ~/.aws/
touch ~/.aws/credentials
printf "[eb-cli]\naws_access_key_id = %s\naws_secret_access_key = %s\n" "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" >> ~/.aws/credentials
touch ~/.aws/config
printf "[profile eb-cli]\nregion=ap-southeast-1\noutput=json" >> ~/.aws/config

vim .gitlab-ci.yml

Sample pipeline code for gitlab-ci.yml -

deploy_application:
 only:
   refs:
     - master
   variables:    
     - $CI_PIPELINE_SOURCE == 'push'
 image: python:latest
 stage: deploy
 script:
   - echo "Deploy to staging server"
   - export AWS_ACCESS_KEY_ID=$XXX-sensitive-dataXXX
   - export AWS_SECRET_ACCESS_KEY=$XXX-sensitive-dataXXX
   - pip install awsebcli awscli==1.18.223 --use-deprecated=legacy-resolver
   - aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
   - aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
   - mkdir -p .elasticbeanstalk
   - mkdir -p .ebextensions
   - cp environment_settings/<config_files> .ebextensions/
   - eb list --verbose
   - eb deploy <ebs-env> --timeout 25 --verbose
 environment:
   name: <ebs-env>
 when: on_success

arrow_back
Back to blog