Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 427
Brandon Chinn Avatar asked Nov 08 '22 13:11

Brandon Chinn


1 Answers

Try something like the below. As EB is just CloudFormation stacks look at the stacks beginning with awseb- to find yours. Then look at the CF Resources while your EB app deploys to see the names used by the predefined EB resources that you aren't specifying in the .ebextensions. I see AWSEBInstanceLaunchWaitCondition in mine, which appears to relate to the initial instances launch.

Resources:
  MyService:
    Type: AWS::ECS::Service
    Properties:
      Cluster: ${Ref: MyCluster}
      DesiredCount: 1
      TaskDefinition: ${Ref: MyTask}
    DependsOn: AWSEBInstanceLaunchWaitCondition
like image 68
NHol Avatar answered Nov 15 '22 05:11

NHol