How to run Elastic Beanstalk commands on an EC2 instance before connecting resources

We have a situation where we would like to start the Django server in normal Elastic Beanstalk mode when connecting the custom Docker container that will be used by the Django website. So far, I basically had 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 ECS is trying to start before the EC2 instance provided by Elastic Beanstalk is registered in the cluster. As a result, deployment to the elastic beanstalk occurs. If I manually SSH'ed into an EC2 instance and manually installed ecs-init , created ecs.config and executed the commands, the service continues to be created and the EB environment is created successfully.

Is there a way to tell the service to wait for the EC2 instance created by EB autoscale to be registered in the cluster?

More context:

  • We want the Django server to be able to access the Docker container with the local host, but I would not mind turning on the EC2 instance in resources specifically designed to host the Docker container, if it is easy to find in autoscaled EC2 instances
  • We tried the approach to containers with several dockers, but this method seems closer to using EB (with web server files directly in the environment, instead of portraying dockers to run the environment).
+5
source share
2 answers

I decided it myself, thinking about it more clearly.

It makes no sense to register every instance of EC2 that the Elastic Beanstalk spins (via AutoScaling) into the ECS cluster. There must be only one instance of the ECS task. Therefore, it is actually necessary to create a separate EC2 instance that connects to the ECS cluster (whose service can now depend on the EC2 instance).

0
source

Try something like below. Because EB is just CloudFormation, stacks look at stacks starting with awseb- to find yours. Then browse CF Resources until your EB application expands to see the names used by the predefined EB resources that you did not specify in .ebextensions. I see AWSEBInstanceLaunchWaitCondition in mine, which apparently refers to starting initial instances.

 Resources: MyService: Type: AWS::ECS::Service Properties: Cluster: ${Ref: MyCluster} DesiredCount: 1 TaskDefinition: ${Ref: MyTask} DependsOn: AWSEBInstanceLaunchWaitCondition 
+1
source

Source: https://habr.com/ru/post/1273560/


All Articles