简体   繁体   中英

Using codedeploy for Fargate Scheduled tasks

I'm trying to set up a CI/CD pipeline for one of our applications that only consists of scheduled tasks. Can't seem to figure out the suggested CI/CD workflow for these. As far as I can see, all the documentation and examples online work with services.

Currently, my tasks are configured to use the "latest" tag, but, I'd like to be able to run them on a specific version, and as a new version is released, use a named version.

I'm using CDK for all the provisioning if that makes a difference.

Any pointers much appreciated.

I'm thinking a CodePipeline stack that builds, tests, and deploys to ECR or Docker would take care of the CI/CD part. As far as the scheduled task part goes, perhaps this provides some inspiration:

public class MyWorkloadTaskStack extends Stack {

    public MyWorkloadTaskStack(@Nullable Construct scope, @Nullable String id, @Nullable StackProps props, @NotNull Cluster cluster) {
        super(scope, id, props);

        FargateTaskDefinition task = FargateTaskDefinition.Builder.create(this, "MyWorkloadTask")
                .memoryLimitMiB(1024)
                .cpu(256)
                .build();

        task.addContainer("Workload", ContainerDefinitionOptions.builder()
                .image(fromRegistry("zsquare/my-workload"))
                .logging(AwsLogDriver.Builder.create()
                        .logGroup(LogGroup.Builder.create(this, "LogGroup")
                                .logGroupName("MyWorkloadTaskLogGroup")
                                .removalPolicy(RETAIN)
                                .retention(ONE_MONTH)
                                .build())
                        .streamPrefix("MyWorkloadTask")
                        .build())
                .build());

        Rule rule = Rule.Builder.create(this, "MyWorkloadRule")
                .schedule(cron(CronOptions.builder()
                        .hour("11")
                        .minute("15")
                        .build()))
                .build();

        rule.addTarget(EcsTask.Builder.create()
                .cluster(cluster)
                .subnetSelection(SubnetSelection.builder()
                        .subnetType(PUBLIC)
                        .build())
                .taskDefinition(task)
                .taskCount(1)
                .containerOverrides(asList(ContainerOverride.builder()
                        .containerName("Workload")
                        .build()))
                .build());
    }

}

This creates an EventBridge Rule to run the Task Definition named "Workload" every day at 11:15 UTC. No running Service is created, just a Task that starts, does its work, then gracefully ends.

It logs to CloudWatch with the prefix "MyWorkloadTask" to make logs easy to find.

The Task Definition itself pulls from Docker in this example, but it's trivial to specify ECR instead. It assumes you've created a Cluster elsewhere and are passing it in here as an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM