Brandon Chinn
Brandon Chinn

Reputation: 141

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:

Upvotes: 5

Views: 731

Answers (2)

Brandon Chinn
Brandon Chinn

Reputation: 141

Solved it myself after thinking about it more clearly.

It doesn't make sense to register every EC2 instance that Elastic Beanstalk spins up (via AutoScaling) to the ECS cluster. There should only be one instance of the ECS task. So it's actually necessary to create a separate EC2 instance that connects to the ECS cluster (whose service can now depend on the EC2 instance).

Upvotes: 0

NHol
NHol

Reputation: 2125

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

Upvotes: 1

Related Questions