amazon-web-servicesdockeramazon-ec2amazon-elastic-beanstalkelastic-load-balancer

How to Share AWS EC2 Instances with Elastic Beanstalk Multi-Container Environments


Lets say I have two Docker containers 1 and 2. I want to deploy these to AWS Elastic Beanstalk using Multi-Container Docker Images so that I have the following EC2 instances to reduce costs:

Test Environment

Production Environment

So my questions are:

  1. How many Elastic Beanstalk applications do I need to create? It is unclear from the documentation.
  2. How can I get Container's 1 & 2 to share EC2 instances.
  3. The load balancers get created for you but each docker container needs it's own port 80, so how many ELB's need to be created. How does this work?

Solution

  • How many Elastic Beanstalk applications do I need to create?

    You can get the setup you'd like with a single application with multiple environments. This is described in the Elastic Beanstalk Documentation on environments. For your use case you would have a "production" and "test" environment configured for the same application.

    Your environments can have different configurations, so you can use smaller (cheaper) EC2 instances for your test environment.

    You can also have different auto-scaling rules, so in the environments you've described you would specify a minimum of 1 EC2 instance in the test environment, but a minimum of 2 EC2 instances in production.

    How can I get Container's 1 & 2 to share EC2 instances?

    I think you are asking how to deploy two Docker containers onto a single EC2 instance acting as the Docker host?

    This is defined in the Dockerrun.aws.json file, which is documented in the Elastic Beanstalk guide. Note - you should refer to version 2 for multi-container Docker applications.

    The file declares how you would like your environment to be configured. The relevant bit for your question is the containerDefinitions which contains an array of Docker containers which you would to deploy to your host machine (the underlying EC2 instance).

    When Elastic Beanstalk scales your environment, it applies the configuration in the Dockerrun file to the new instance. So, if your example production environment scaled out to have three instances you'd end up a third instance running the same two containers.

    How many ELB's need to be created

    As per the documentation, you would have one Elastic Load Balancer per environment:

    Every environment has a CNAME (URL) that points to a load balancer.

    Within the containerDefinition section of the Dockerrun file mentioned above you define the port mappings for your containers. So if one of the containers in your example was running Nginx you would have configuration similar to this in your Dockerrun file:

    "name": "nginx-proxy",
          "image": "nginx",
          "essential": true,
          "memory": 128,
          "portMappings": [
            {
              "hostPort": 80,
              "containerPort": 80
            }
          ],
    

    This configuration is then applied to any instances running your container configuration - when the load balancer routes a request to that particular instance it is forwarded to the container listening on port 80.