How to run a container on an existing EC2 instance using CloudFormation?

The scenario is as follows: I have 3 instances of EC2 (A, B, and C), all of which work with optimized ECS AMI. I would like to write a CloudFormation template with a task definition that allows the task to run only on A. How to do this?

All the CloudFormation examples I've seen require a new EC2 instance, which I don't want.

+4
source share
1 answer

The only way to bind the task to the host is to do it through the AWS CLI with the initial task: http://docs.aws.amazon.com/cli/latest/reference/ecs/start-task.html

ECS Cloudformation, CFT . CFT:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Curator runner",
    "Parameters": {
        "CpuUnits": {
            "Type": "Number",
            "Default": 0,
            "Description": "The number of CPU Units to allocate."
        },
        "Memory": {
            "Type": "Number",
            "Default": 256,
            "Description": "The amount of Memory (MB) to allocate."
        },
        "ClusterName": {
            "Type": "String",
            "Description": "The cluster to run the ecs tasks on."
        },
        "DockerImageUrl": {
            "Type": "String",
            "Description": "The URL for the docker image. Example: 354500939573.dkr.ecr.us-east-1.amazonaws.com/something:latest"
        }
    },
    "Resources": {
        "SomeTask": {
            "Type": "AWS::ECS::TaskDefinition",
            "Properties": {
                "ContainerDefinitions": [{
                    "Memory": {
                        "Ref": "Memory"
                    },
                    "Name": "something",
                    "Image": {
                        "Ref": "DockerImageUrl"
                    },
                    "Cpu": {
                        "Ref": "CpuUnits"
                    }
                }],
                "Volumes": []
            }
        },
        "service": {
            "Type": "AWS::ECS::Service",
            "Properties": {
                "Cluster": {
                    "Ref": "ClusterName"
                },
                "DesiredCount": "1",
                "TaskDefinition": {
                    "Ref": "SomeTask"
                }
            }
        }
    }
}
+1

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


All Articles