gpt4 book ai didi

aws-sdk - aws cdk ecs任务调度指定现有安全组

转载 作者:行者123 更新时间:2023-12-03 07:13:36 25 4
gpt4 key购买 nike

在定义 ECS 任务计划时,我似乎找不到指定现有安全组的方法。有关可以使用 aws cdk 进行配置的任何指示吗?

在下面的代码片段中,您将看到我能够创建一个 cron,指定要调度的 docker 镜像,并通过指定现有集群和 vpc 来创建调度本身。但是,没有选项可以指定现有安全组...是否可以指定现有安全组?

schedule_cron = scaling.Schedule.cron(minute=manifest['schedule']['minute'], 
hour=manifest['schedule']['hour'],
day=manifest['schedule']['day'],
month=manifest['schedule']['month'],
year=manifest['schedule']['year'])

image_option = ecs_patterns.ScheduledFargateTaskImageOptions(image=img,
cpu=manifest["resources"]["cpu"],
memory_limit_mib=manifest["resources"]["memory"],
log_driver=ecs.AwsLogDriver(log_group=log_group,
stream_prefix=manifest["app_name"]),
secrets=secrets,
environment= env)

schedule_pattern = ecs_patterns.ScheduledFargateTask(self, f"scheduledtask{app_name}",
schedule= schedule_cron, scheduled_fargate_task_image_options=image_option, cluster=cluster,
desired_task_count=manifest["replica_count"], vpc=vpc)

最佳答案

ECS 模式尚不支持此功能。然而,底层结构确实如此。因此,您必须自己指定任务定义、事件和事件目标。使用事件指定计划,使用事件目标设置安全组。

这里是使用 TypeScript 的示例实现。请使用 aws_cdk.aws_events 和 aws_cdk.aws_events_targets 模块将其调整为 Python。

import aas = require('@aws-cdk/aws-applicationautoscaling');
import cdk = require('@aws-cdk/core');
import events = require("@aws-cdk/aws-events")
import event_targets = require("@aws-cdk/aws-events-targets");
import ec2 = require('@aws-cdk/aws-ec2');

const securityGroup = new ec2.SecurityGroup(this, "SecurityGroup", {
vpc: vpc,
});

const task = new ecs.FargateTaskDefinition(this, "TaskDefinition", {
family: "ScheduledTask",
cpu: ..,
memoryLimitMiB: ..,
});
task.addContainer("app_name", ...);

const rule = new events.Rule(this, "Rule", {
description: "ScheduledTask app_name Trigger",
enabled: true,
schedule: aas.Schedule.rate(cdk.Duration.hours(1)),
targets: [
new event_targets.EcsTask({
cluster: cluster,
taskDefinition: task,
securityGroup: securityGroup,
}),
],
});

请注意,EcsTask 事件目标仅允许一个安全组。不久前在 GitHub 上提出了这个问题:https://github.com/aws/aws-cdk/issues/3312

关于aws-sdk - aws cdk ecs任务调度指定现有安全组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59067514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com