我有一个ECS集群,它将由我的cdk堆栈创建。在我的ECS服务栈部署之前,我必须运行一个fargate任务来为我的应用程序生成构建文件和配置。我想在现有的Ecs集群中运行一个独立的任务。
有两个问题。我会试着回答这两个问题:
- 首先你需要通过CDK 运行Fargate任务
您需要创建一个规则,该规则按时间表(或其他事件)运行ECS任务
import { Rule, Schedule } from '@aws-cdk/aws-events'; import { EcsTask } from '@aws-cdk/aws-events-targets'; new Rule(this, 'ScheduleRule', { schedule: schedule, targets: [ new EcsTask({ cluster, taskDefinition: task, }), ], });
- 第二个-我如何使用现有的集群
你可以通过属性
找到你的集群import { Cluster } from '@aws-cdk/aws-ecs'; let cluster = Cluster.fromClusterAttributes(this, 'cluster_id', { clusterName: "CLUSTER_NAME", securityGroups: [], vpc: iVpc });
:你可以通过一些自定义事件触发你的任务:
new Rule(this, 'EventPatternRule', { eventPattern: { "version": "0", "id": "CWE-event-id", "detail-type": "CodePipeline Pipeline Execution State Change", "source": "aws.codepipeline", "account": "123456789012", "time": "2017-04-22T03:31:47Z", "region": "us-east-1", "resources": [ "arn:aws:codepipeline:us-east-1:123456789012:pipeline:myPipeline" ], "detail": { "pipeline": "myPipeline", "version": "1", "state": "STARTED", "execution-id": "01234567-0123-0123-0123-012345678901" } } targets: [ new EcsTask({ cluster, taskDefinition: task, }), ], });
请参阅本文档了解事件模式