2019-07-21 00:00:00
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.
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.
If Elastic Beanstalk is considered appropriate for a production deployment on a project, always ensure:
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.
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.
There is no additional charge for Elastic Beanstalk – you pay only for the AWS resources needed to store and run your applications.
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.
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.
Here is another use case of using Elastic Beanstalk for blue/green deployment of an application.
Steps for green/blue deployment: