Skip to content

aws-ecs-patterns: Allow ScheduledFargateTask and ScheduledEc2Task to run on a public subnet #6312

@floehopper

Description

@floehopper

Allow ScheduledFargateTask and ScheduledEc2Task to run in a public subnet via a configuration option.

Use Case

I'm currently using the ScheduledFargateTask class to run a number of simple tasks. The tasks need access to the internet, but there's no need for them to be accessible from the internet. I could run the tasks on a private subnet, but this would mean I would need either a NAT Gateway (expensive) or run a NAT Instance on EC2 (maintenance/complexity overhead).

Since the tasks only run for a few minutes every week I'm willing to sacrifice the extra security that a private subnet provides in favour of a simpler/cheaper system where the tasks run on a public subnet.

However, currently ScheduledFargateTask will only run a task if its VPC has a private subnet - if there is no private subnet available, an error is reported. I would like to be able to run my tasks on a VPC defined along the following lines:

    const vpc = new ec2.Vpc(stack, 'Vpc', {
      maxAzs: 1,
      natGateways: 0,
      subnetConfiguration: [
        { name: 'public', cidrMask: 24, subnetType: SubnetType.PUBLIC }
      ],
    });
    const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });

Proposed Solution

Currently the addTaskDefinitionToEventTarget method on ScheduledTaskBase instantiates the underlying EcsTask, but there is no way to specify the subnetSelection property of that EcsTask and so it defaults to { subnetType: ec2.SubnetType.PRIVATE }.

I propose that we add a subnetSelection property to the ScheduledTaskBaseProps interface and supply that property when instantiating the underlying EcsTask. This new property would default to { subnetType: ec2.SubnetType.PRIVATE } thus retaining the existing behaviour.

Other

  • In this comment @skinny85 mentioned that this is a gap in the ScheduledFargateTask construct.

  • In this comment @moofish32 suggested that using only a public subnet is a reasonable option in some circumstances.

  • Reading between the lines, it sounds as if others commenting on this issue might welcome this functionality.

  • 👋 I may be able to implement this feature request

  • ⚠️ This feature might incur a breaking change


This is a 🚀 Feature Request

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-ecs-patternsRelated to ecs-patterns libraryeffort/mediumMedium work item – several days of effortfeature-requestA feature should be added or improved.in-progressThis issue is being actively worked on.p2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions