The purpose of this blog post is provide the reader an understanding of how to build an ElasticBeanstalk application utilizing a CloudFormation Stack. In particular, the post describes:
- the resources required in a CloudFormation stack, including the
- AWS::ElasticBeanstalk::Application Resource
- AWS::ElasticBeanstalk::Environment Resource
- the relationship between these resources
After reading this blog post you should be able to build an ElasticBeanstalk application with multiple environments using a CloudFormation file. Subsequent posts will describe methods of deploying code to the ElasticBeanstalk application.
Resources required to support an ElasticBeanstalk Application
An ElasticBeanstalk application will require some underlying infrastructure such as a VPC, Subnet and, presumably, Internet Gateway, NAT Gateway, Route Tables and Route Table Associations.
The actual ElasticBeanstalk application. An Application serves as a container for Environments and Application Versions.
ElasticBeanstalkApplication: Type: AWS::ElasticBeanstalk::Application Properties: ApplicationName: !Ref AWS::StackName
The screenshot below shows the ElasticBeanstalk Application created by a AWS::ElasticBeanstalk::Application resource.
An “Environment” is a subset of the ElasticBeanstalk application. The “Environments” are shown in the AWS Console as parts of an application. For each “Environment” AWS will launch a CloudFormation stack containing components (typically an Auto Scaling Group, ) required to run your application.
ElasticBeanstalkEnvironment: Type: AWS::ElasticBeanstalk::Environment Properties: ApplicationName: !Ref ElasticBeanstalkApplication TemplateName: !Ref ElasticBeanstalkConfigurationTemplate
For a given application you will likely have a “Prod” environment or a “QA” environment. In the image below, a CloudFormation file containing two “AWS::ElasticBeanstalk::Environment” resources is used to construct “QA” and “Prod” environments – each of these Environments can have unique configurations and utilize different versions of a codebase.
A “Configuration Template” is used to specify the resources required to build an Environment as well as the configuration of these resources. These configuration options include things such as:
- the “Solution Stack” (where Solution Stack determines what type of AMI will be used to run a given application – for instance, an AMI that contains php, ruby, python or docker)
- if the Application will utilize an ElasticLoad Balancer
- the Min Size and Max Size of the Auto Scaling Group supporting the Elastic Beanstalk application if the environment utilizes Auto Scaling
- the VPC in which an ElasticBeanstalk application should reside if the application resides in a VPC
The full list of “OptionSettings” are available are available here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/command-options-general.html. An example Configuration Template is below:
ElasticBeanstalkProdConfigurationTemplate: Type: AWS::ElasticBeanstalk::ConfigurationTemplate Properties: ApplicationName: !Ref ElasticBeanstalkApplication OptionSettings: - Namespace: aws:autoscaling:asg OptionName: MinSize Value: 2 - Namespace: aws:autoscaling:asg OptionName: MaxSize Value: 2 - Namespace: aws:autoscaling:launchconfiguration OptionName: InstanceType Value: t2.micro - Namespace: aws:elasticbeanstalk:environment OptionName: EnvironmentType Value: LoadBalanced - Namespace: aws:ec2:vpc OptionName: VPCId Value: !Ref VPC - Namespace: aws:ec2:vpc OptionName: Subnets # Value: !Join turns the individual subnets into a string Value: !Join [ ",", [ !Ref PublicSubnet01, !Ref PublicSubnet02 ] ] - Namespace: aws:ec2:vpc OptionName: AssociatePublicIpAddress Value: true SolutionStackName: 64bit Amazon Linux 2018.03 v2.8.4 running PHP 5.6
Link to CloudFormation File
The stack that I used to aid understanding the use of CloudFormation to create AWS ElasticBeanstalk applications is available here: https://github.com/cloudavail/snippets/tree/master/aws/elasticbeanstalk/elasticbeanstalk_with_multiple_envs
If you have any questions about this particular blog post please feel free to post a question below or email firstname.lastname@example.org.