1

I would like to start a task definition on an instance within my cluster (not in the default one). So something like:

  1. create a cluster
  2. create a task definition with a docker image (I have a docker image already pushed to ecs)
  3. run the task definition in the cluster
  4. I would like to add a keypair to the ec2 instance for ssh access

I have tried to use these functions form boto3 (ec2, ecs)

  • create_cluster
  • run_task
  • register_container_instance
  • register_task_definition
  • run_instances

I managed to run an instance with run_instances, it works perfectly well but I want to run an instance in my cluster. Here is my code:

    def run_instances():
        response = ec2.run_instances(
            BlockDeviceMappings=[
            {
                'DeviceName': '/dev/xvda',
                'Ebs': {
                     'DeleteOnTermination': True,
                     'VolumeSize': 8,
                     'VolumeType': 'gp2'
                },
            },
        ],
        ImageId='ami-06df494fbd695b854',
        InstanceType='m3.medium',
        MaxCount=1,
        MinCount=1,
        Monitoring={
            'Enabled': False
        })
        return response

There is a running instance on ec2 console but it doesn't appear in any of the clusters in the ecs console (I tried it with an ecs-optimized ami and with a regular one).

I also tried to follow these steps for getting my system up and running in a cluster without success: https://github.com/spulec/moto/blob/master/tests/test_ecs/test_ecs_boto3.py

Could you please help me find out what do I miss? Is there ant other setup have to make beside calling these SDK functions?

Thank you!

Farkas István
  • 504
  • 2
  • 5
  • 15
  • What are you looking for? Ec2 or ECS? - to start ec2 instance use [run_instances](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.run_instances) – titogeo Sep 21 '18 at 14:39
  • I managed to run instance with [run_instances](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.run_instances), it works perfectly well but I want to run an instance in my cluster. Please see my updated question with code. – Farkas István Sep 21 '18 at 18:04

1 Answers1

1

You will need to run an instance that uses ECS Optimized AMI since those AMIs have ECS agent preinstalled on them otherwise you would need to install ECS agent yourself and bake a custom AMI.

By default, your ECS optimized instance launches into your default cluster, but you can specify alternative cluster name in UserData property of run_instances function

#!/bin/bash
echo ECS_CLUSTER=your_cluster_name >> /etc/ecs/ecs.config

The list of available ECS AMIs is available here

b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57
  • Awsome. Works like a charm. One note: I had to attach a role with AmazonEC2ContainerServiceforEC2Role policy to the running instance as described [here](https://stackoverflow.com/questions/36523282/aws-ecs-error-when-running-task-no-container-instances-were-found-in-your-clust/38035652). Here is how to do it: [CLI](https://aws.amazon.com/blogs/security/new-attach-an-aws-iam-role-to-an-existing-amazon-ec2-instance-by-using-the-aws-cli/) [aws console](https://aws.amazon.com/blogs/security/easily-replace-or-attach-an-iam-role-to-an-existing-ec2-instance-by-using-the-ec2-console/) – Farkas István Sep 22 '18 at 22:25