Search code examples
amazon-web-servicesamazon-ec2aws-sdkamazon-ecs

Creating EC2 instance and attaching it to a specific ECS cluster


I am using the AWS PHP SDK to create an ECS Cluster.

I am also using the AWS PHP SDK to create EC2 Instances.

I can't find API calls to create an instance, specify particular options (for instance on the spot) and adding it to the cluster I am creating. Any ideas?


Solution

  • There is no API call for that. Instead, you specify which cluster a given instance belongs to using user data, as explained in AWS docs. So your newly launch EC2 instances should have user data in the following form:

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

    As well as proper IAM instance role allowing the ECS agent to register with your ECS cluster.