Search code examples
amazon-web-servicesamazon-ec2boto3amazon-ecs

How to start EC2 instance in cluster with boto3


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!


Solution

  • 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