3

我正在尝试在 ECS 中运行或启动现有的任务定义,但缺少文档,而且我似乎无法在线找到任何示例。我碰壁了,我想知道是否有其他人做过类似的事情。

我正在使用 AWSSDK.ECS 包。

var request = JsonConvert.DeserializeObject<Request>(record.Sns.Message);

var task = new RunTaskRequest
{
    Count = 1,
    NetworkConfiguration = new NetworkConfiguration
    {
        AwsvpcConfiguration = new AwsVpcConfiguration
        {
            Subnets = new List<string>() { request.SubnetId},
            SecurityGroups = new List<string>() { request.SecurityGroupId},
            AssignPublicIp = AssignPublicIp.DISABLED
        }
    },
    Cluster = request.Cluster,
    LaunchType = LaunchType.FARGATE,
    Overrides = new TaskOverride
    {
        ContainerOverrides = new List<ContainerOverride>
        {
            new ContainerOverride
            {
                Name = request.ContainerName,
                Environment = request.EnvironmentVariables
                    .Select(kvp => new Amazon.ECS.Model.KeyValuePair()
                {
                    Name = kvp.Key,
                    Value = kvp.Value
                }).ToList()
            }
        }
    },
    TaskDefinition = request.TaskDefinitionUri
};

await new AmazonEcsClient().RunTaskAsync(task);
4

1 回答 1

1

检查您的任务定义。如果NetworkModeawsvpc,那么您必须提供NetworkConfiguration参数。这对我有用:

var runTaskRequest = new ECSModel.RunTaskRequest
{
    Cluster = "cluster-name",
    Count = 1,
    LaunchType = LaunchType.FARGATE,
    TaskDefinition = "task-definition",
    NetworkConfiguration = new ECSModel.NetworkConfiguration
    {
        AwsvpcConfiguration = new ECSModel.AwsVpcConfiguration
        {
            SecurityGroups = new List<string> { "security-group" },
            Subnets = new List<string> { "subnet" }
        }
    },
    Overrides = new ECSModel.TaskOverride
    {
        ContainerOverrides = new List<ECSModel.ContainerOverride>
        {
            new ECSModel.ContainerOverride
            {
                Name = "container-name",
                Command = new List<string>
                {
                    // In case if you need to pass parameters to your instance:
                    "parameter-one", "parameter-two", "etc"
                }
            }
        }
    }
};

await new AmazonEcsClient().RunTaskAsync(runTaskRequest);
于 2021-09-20T22:11:54.587 回答