How to run Elastic Beanstalk commands on EC2 instance before connecting Resources

Viewed 641

We have a situation where we would like to run a Django server in the usual Elastic Beanstalk manner while hooking up a custom Docker container to be used by the Django website. So far, I basically have the following .ebextensions configuration file:

packages:
  yum:
    ecs-init: []

files:
  /etc/ecs/ecs.config:
    mode: "000644"
    owner/group: root
    content: ECS_CLUSTER=${Ref: MyCluster}

commands:
  01_start_docker: sudo service docker start
  02_start_ecs: sudo start ecs

Resources:
  MyCluster:
    Type: AWS::ECS::Cluster
  MyService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: ${Ref: MyCluster}
      DesiredCount: 1
      TaskDefinition: ${Ref: MyTask}
  MyTask:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - ...

The problem is that the ECS service is trying to start up before the Elastic Beanstalk-provided EC2 instance is registered with the cluster. As a result, deploying to Elastic Beanstalk hangs. If I manually SSH'ed into the EC2 instance and manually installed ecs-init, created ecs.config, and ran the commands, the service continues being created and the EB environment is created successfully.

Is there a way to tell the service to wait until the EC2 instance created by EB's autoscaling group is registered with the cluster?

More context:

  • We want the Django server to be able to access the Docker container with localhost, but I wouldn't be opposed to including an EC2 instance in Resources specifically to host the Docker container, if it's easy to refer to in the auto-scaled EC2 instances
  • We've tried the multi-docker container approach, but this way seems closer to EB's usage (having the web server files directly in the environment instead of making a docker image for the environment to run)
2 Answers
Related