Skip to main content

Define and run container tasks on AWS Fargate immediately or with schedule

Project description

NPM version PyPI version build

cdk-fargate-run-task

Define and run container tasks on AWS Fargate at once or by schedule.

sample

const app = new cdk.App();

const env = {
  account: process.env.CDK_DEFAULT_ACCOUNT,
  region: process.env.CDK_DEFAULT_REGION,
};

const stack = new cdk.Stack(app, 'run-task-demo-stack', { env });

// define your task
const task = new ecs.FargateTaskDefinition(stack, 'Task', { cpu: 256, memoryLimitMiB: 512 });

// add contianer into the task
task.addContainer('Ping', {
  image: ecs.ContainerImage.fromRegistry('busybox'),
  command: [
    'sh', '-c',
    'ping -c 3 google.com',
  ],
  logging: new ecs.AwsLogDriver({
    streamPrefix: 'Ping',
    logGroup: new LogGroup(stack, 'LogGroup', {
      logGroupName: `${stack.stackName}LogGroup`,
      retention: RetentionDays.ONE_DAY,
    }),
  }),
});

// deploy and run this task once
const runTaskAtOnce = new RunTask(stack, 'RunDemoTaskOnce', { task });

// or run it with schedule(every hour 0min)
new RunTask(stack, 'RunDemoTaskEveryHour', {
  task,
  cluster: runTaskAtOnce.cluster,
  runOnce: false,
  schedule: Schedule.cron({ minute: '0' }),
});

Public Subnets only VPC

To run task in public subnets only VPC:

new RunTask(stack, 'RunTask', {
  task,
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },

ECS Anywhere

Amazon ECS Anywhere allows you to run ECS tasks on external instances. To run external task once or on schedule:

const externalTask = new ecs.TaskDefinition(stack, 'ExternalTask', {
  cpu: '256',
  memoryMiB: '512',
  compatibility: ecs.Compatibility.EXTERNAL,
});

externalTask.addContainer('ExternalPing', {
  image: ecs.ContainerImage.fromRegistry('busybox'),
  command: [
    'sh', '-c',
    'ping -c 3 google.com',
  ],
  logging: new ecs.AwsLogDriver({
    streamPrefix: 'Ping',
    logGroup: new LogGroup(stack, 'ExternalLogGroup', {
      retention: RetentionDays.ONE_DAY,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    }),
  }),
});

// run it once on external instance
new RunTask(stack, 'RunDemoTaskFromExternal', {
  task: externalTask,
  cluster: existingCluster,
  launchType: LaunchType.EXTERNAL,
});

// run it by schedule  on external instance
new RunTask(stack, 'RunDemoTaskFromExternalSchedule', {
  task: externalTask,
  cluster: existingCluster,
  launchType: LaunchType.EXTERNAL,
  runAtOnce: false,
  schedule: Schedule.cron({ minute: '0' }),
});

Please note when you run task in EXTERNAL launch type, no fargate tasks will be scheduled. You will be responsible to register the external instances to your ECS cluster. See Registering an external instance to a cluster for more details.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk-fargate-run-task-2.0.310.tar.gz (43.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_fargate_run_task-2.0.310-py3-none-any.whl (41.6 kB view details)

Uploaded Python 3

File details

Details for the file cdk-fargate-run-task-2.0.310.tar.gz.

File metadata

  • Download URL: cdk-fargate-run-task-2.0.310.tar.gz
  • Upload date:
  • Size: 43.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for cdk-fargate-run-task-2.0.310.tar.gz
Algorithm Hash digest
SHA256 1c7407464c0987e023a1a5f994b2d5993ccb40c706f7a810ece12025970954d6
MD5 d07139b5202a94eab783867deabefcae
BLAKE2b-256 0c5459d56892de3f7b2879744713c588ae74699b6785917a343fb3156c69e942

See more details on using hashes here.

File details

Details for the file cdk_fargate_run_task-2.0.310-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_fargate_run_task-2.0.310-py3-none-any.whl
Algorithm Hash digest
SHA256 f40ce7fae07d4c86bc5fdb9bf90d9d2e7e1c8df00e5ef0a9b755eae5ede9e5a6
MD5 7c6908b2a0e4c3116f08da39721e8e86
BLAKE2b-256 60bef8e8eac21154f0a22f42fce09f7d1da8fad496e1d8156f6b709d4592921a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page