Skip to main content

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

Project description

NPM version PyPI version Release

cdk-fargate-run-task

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

sample

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
app = cdk.App()

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

stack = cdk.Stack(app, "run-task-demo-stack", env=env)

# define your task
task = ecs.FargateTaskDefinition(stack, "Task", cpu=256, memory_limit_mi_b=512)

# add contianer into the task
task.add_container("Ping",
    image=ecs.ContainerImage.from_registry("busybox"),
    command=["sh", "-c", "ping -c 3 google.com"
    ],
    logging=ecs.AwsLogDriver(
        stream_prefix="Ping",
        log_group=LogGroup(stack, "LogGroup",
            log_group_name=f"{stack.stackName}LogGroup",
            retention=RetentionDays.ONE_DAY
        )
    )
)

# deploy and run this task once
run_task_at_once = RunTask(stack, "RunDemoTaskOnce", task=task)

# or run it with schedule(every hour 0min)
RunTask(stack, "RunDemoTaskEveryHour",
    task=task,
    cluster=run_task_at_once.cluster,
    run_once=False,
    schedule=Schedule.cron(minute="0")
)

ECS Anywhere

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

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
external_task = ecs.TaskDefinition(stack, "ExternalTask",
    cpu="256",
    memory_mi_b="512",
    compatibility=ecs.Compatibility.EXTERNAL
)

external_task.add_container("ExternalPing",
    image=ecs.ContainerImage.from_registry("busybox"),
    command=["sh", "-c", "ping -c 3 google.com"
    ],
    logging=ecs.AwsLogDriver(
        stream_prefix="Ping",
        log_group=LogGroup(stack, "ExternalLogGroup",
            retention=RetentionDays.ONE_DAY,
            removal_policy=cdk.RemovalPolicy.DESTROY
        )
    )
)

# run it once on external instance
RunTask(stack, "RunDemoTaskFromExternal",
    task=external_task,
    cluster=existing_cluster,
    launch_type=LaunchType.EXTERNAL
)

# run it by schedule  on external instance
RunTask(stack, "RunDemoTaskFromExternalSchedule",
    task=external_task,
    cluster=existing_cluster,
    launch_type=LaunchType.EXTERNAL,
    run_at_once=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-0.1.72.tar.gz (37.1 kB view hashes)

Uploaded Source

Built Distribution

cdk_fargate_run_task-0.1.72-py3-none-any.whl (35.9 kB view hashes)

Uploaded Python 3

Supported by

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