Elastic Beanstalk

Enabling DevOps

We are sometimes asked about AWS Elastic Beanstalk by clients. For those who aren’t familiar, 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.

Use cases – Non-Production

For some projects, perhaps an initial proof-of-concept or early stage project work, Elastic Beanstalk may be suggested to allow a fast start for Developers to get an application up and running quickly without spending time configuring infrastructure. This is an acceptable approach but always be mindful of what the eventual production deployment landscape may look like and whether Elastic Beanstalk would be acceptable from a security and compliance standpoint.

Even in development or proof-of-concept work, always consider running Elastic Beanstalk within a VPC and with the appropriate IAM controls, regardless of the size or scope of the project.

Use cases – Production

If Elastic Beanstalk is considered appropriate for a production deployment on a project, always ensure:

  • Appropriate security controls are in place
  • Standard infrastructure as code methodology is followed
  • Solution is architected to the usual levels of resilience
  • Solution is scaled appropriately
  • Solution utilises standard logging and monitoring mechanisms
  • 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

AWS Elastic Beanstalk has two types of “environment tier” to support different types of applications:

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 specialized 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.

See AWS Elastic Beanstalk Concepts for further information.

Cost

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

Terraform Modules

AWS Elastic Beanstalk Application: To create the Elastic Beanstalk application check out our example EB Application Module. This module contains the example for provisioning an application in AWS Elastic Beanstalk service. To Setup, call the below Terraform code:

module "beanstalk_application" {
    source = "git::https://github.com/AirWalk-Digital/terraform-aws-elastic-beanstalk.git//modules/elastic_beanstalk_application"
    application_name = "sample_application"
    application_description = "A short description for sample application"
    service_role = "iam_role_arn"
    max_app_verions_count = 10
    delete_source_from_s3 = true
}

AWS Elastic Beanstalk Webserver: The module for provisioning Elastic Beanstalk tier web service, check out our EB Webserver Module. It contains the example of using this module. The extra add-on in this module is the use of the Terraform Workspace  concept. You can now use the same module for multiple accounts/environments by specifying different tfvars (such as prod.tfvars, qa.tfvars, dev.tfvars, etc). It will create a separate state file for each environment in the S3 backend under the specified workspace. It can also be used to add a prefix to the service name, eg for qa the service name will be qa-service-name. To provision the resource, call the following commands:

terraform init
terraform workspace new qa #for production replace qa with prod
terraform workspace select qa
terraform plan -var-file=qa.tfvars
terraform apply

Sample Code for calling module:

module "webservice" {
    source = "git::https://github.com/AirWalk-Digital/terraform-aws-elastic-beanstalk.git//modules/elastic_beanstalk_webserver"
    env_name = "${var.env_name}"
    application_name = "${var.application_name}"
    version_label = "${var.version_label}"
    env = "${var.env}"
    asg_instance_type = "${var.asg_instance_type}"
    instance_profile = "${var.instance_profile}"
    security_group = ["${var.security_group}"]
    service_role = "${var.service_role}"
    batchtype = "${var.batchtype}"
    batchsize = "${var.batchsize}"
    deployment_policy = "${var.deployment_policy}"
    key_pair = "${var.key_pair}"
    asg_capacity = "${var.asg_capacity}"
    asg_zones = "${var.asg_zones}"
    vpc_id = "${var.vpc_id}"
    elb_scheme = "${var.elb_scheme}"
    elb_subnets = "${var.elb_subnets}"
    ec2_subnets = ["${var.ec2_subnets}"]
    load_balancer_type = "${var.load_balancer_type}"
    beanstalk_env_variable_keys = "${var.beanstalk_env_variable_keys}"
    beanstalk_env_variable_values = "${var.beanstalk_env_variable_values}"
    additional_environment_variables = ["${var.additional_environment_variables}"]
    ami_id = "${var.ami_id}"
    tags = {
        "Name" = "testing"
    }
}

AWS Elastic Beanstalk Webserver: The module for provisioning Elastic Beanstalk tier web service, check out our EB Webserver Module. It contains the example of using this module. The extra add-on in this module is the use of the Terraform Workspace  concept. You can now use the same module for multiple accounts/environments by specifying different tfvars (such as prod.tfvars, qa.tfvars, dev.tfvars, etc). It will create a separate state file for each environment in the S3 backend under the specified workspace. It can also be used to add a prefix to the service name, eg for qa the service name will be qa-service-name. To provision the resource, call the following commands:

terraform init
terraform workspace new qa #for production replace qa with prod
terraform workspace select qa
terraform plan -var-file=qa.tfvars
terraform apply

Sample Code for calling module:

module "webservice" {
    source = "git::https://github.com/AirWalk-Digital/terraform-aws-elastic-beanstalk.git//modules/elastic_beanstalk_webserver"
    env_name = "${var.env_name}"
    application_name = "${var.application_name}"
    version_label = "${var.version_label}"
    env = "${var.env}"
    asg_instance_type = "${var.asg_instance_type}"
    instance_profile = "${var.instance_profile}"
    security_group = ["${var.security_group}"]
    service_role = "${var.service_role}"
    batchtype = "${var.batchtype}"
    batchsize = "${var.batchsize}"
    deployment_policy = "${var.deployment_policy}"
    key_pair = "${var.key_pair}"
    asg_capacity = "${var.asg_capacity}"
    asg_zones = "${var.asg_zones}"
    vpc_id = "${var.vpc_id}"
    elb_scheme = "${var.elb_scheme}"
    elb_subnets = "${var.elb_subnets}"
    ec2_subnets = ["${var.ec2_subnets}"]
    load_balancer_type = "${var.load_balancer_type}"
    beanstalk_env_variable_keys = "${var.beanstalk_env_variable_keys}"
    beanstalk_env_variable_values = "${var.beanstalk_env_variable_values}"
    additional_environment_variables = ["${var.additional_environment_variables}"]
    ami_id = "${var.ami_id}"
    tags = {
        "Name" = "testing"
    }

AWS Elastic Beanstalk Worker: The module for Worker is almost the same as for web server, But worker required a couple of extra variables for provisioning including SQS Name. Workspace and state file management are the same as mentioned above for the web server. The worker module and examples can be found here: AWS Elastic Beanstalk Worker.

Advantages

  • Configuration with Automation: Saves time by handling dependencies, including the setup, configuration and provisioning of other AWS services like EC2, RDS, and Elastic Load Balancing to create a web service
  • Powerful Customization: It gives the opportunity to customise the configuration to suit your application’s needs including Software, Instance configuration, Capacity, Load Balancer, Deployment strategy, Security, Monitoring, Notification, Database and Networks all through a single configuration page.
  • Price and Flexibility: There’s no additional cost of Elastic Beanstalk except the resources needed for the application. It has a built-in auto-stalling functionality with easily configured metrics which used for saving cost.

Disadvantages

  • Deployment Speed: Sometimes deployment time took longer – from 5 minutes to 15 minutes for a site with just two front-ends.
  • Stack Upgrades: Elastic Beanstalk comes out with new stack versions all time and finding what was actually changed or upgraded is difficult.
  • Old Application Versions: With every deployment, EB archives the old application version in S3 and when the count reaches 500 your deployment will fail. You need to delete application version occasionally.

Sample Use Case

Find the source code here. You will find a Beanstalk configuration with a sample Docker application. All you need to deploy Terraform configuration using the commands mentioned above in Elastic Beanstalk Webserver.

Weighted Deployment Use Case Architecture Diagram

Here is another use case of using Elastic Beanstalk for blue/green deployment of an application.

Steps for green/blue deployment:

  • Setup green Elastic Beanstalk environment with new configuration or using the configuration template of old (blue) environment
  • Deploy new application code in green environment and test the application
  • When satisfied, route the traffic from old to new environment and monitor the metrics
  • Once new environment receives 100% of traffic, new environment will become blue
  • if something goes wrong, revert the traffic