aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[ecs] Fargate Spot Instances

Open jamiepmullan opened this issue 6 years ago • 21 comments

We would like to be able to launch Fargate instances with the capacity provider FARGATE_SPOT. This is currently available via the AWS CLI.

Use Case

The cost benefit to this is pretty obvious!

Proposed Solution

Other

CLI: https://aws.amazon.com/blogs/aws/aws-fargate-spot-now-generally-available/

  • [ ] :wave: I may be able to implement this feature request
  • [ ] :warning: This feature might incur a breaking change

This is a :rocket: Feature Request

jamiepmullan avatar Jan 17 '20 09:01 jamiepmullan

Just found the dependency on: #5471

jamiepmullan avatar Jan 17 '20 09:01 jamiepmullan

Any updates?

XBeg9 avatar Apr 27 '20 22:04 XBeg9

@XBeg9 Fargate Spot instances are not yet supported in CloudFormation. Once they're supported in CFN, we'll look to determine the scope of work involved in the CDK.

piradeepk avatar Apr 27 '20 22:04 piradeepk

@pkandasamy91 any ideas how we can create ecs.Cluster with Networking Only, so those 2 FARGATE providers will get populated automatically? Thanks

XBeg9 avatar Apr 27 '20 23:04 XBeg9

Any updates here? Will be great to do a Fargate spot tasks cluster using CDK.

w601sxs avatar Aug 08 '20 18:08 w601sxs

Couldn't a custom resource, backed by lambda, be used to create this?

swe-ds avatar Aug 08 '20 18:08 swe-ds

Isn't this supported in CloudFormation now? Both at the cluster level for a default provider, and at the service level with CapacityProviderStrategyItem?

npetter avatar Oct 06 '20 07:10 npetter

Should be supported as of Oct 26

AWS Fargate Spot for Amazon ECS is now supported in AWS CloudFormation https://aws.amazon.com/about-aws/whats-new/2020/10/aws-fargate-spot-for-amazon-ecs-now-supported-in-aws-cloudformation

mikkotikkanen avatar Oct 29 '20 08:10 mikkotikkanen

Any updates as CF now supports it ?

pmaoui avatar Dec 31 '20 15:12 pmaoui

I was able to leverage the cfnCluster class do do this, although it would still be nice to just be able to specify this in the constructor properties.

    const cfnCluster = cluster.node.defaultChild as ecs.CfnCluster
    cfnCluster.capacityProviders = ['FARGATE', 'FARGATE_SPOT']
    cfnCluster.defaultCapacityProviderStrategy = [
      {
        capacityProvider: 'FARGATE',
        weight: 1,
      },
      {
        capacityProvider: 'FARGATE_SPOT',
        weight: 4,
      }
    ]

mattaschmann avatar Jan 27 '21 18:01 mattaschmann

I was able to leverage the cfnCluster class do do this, although it would still be nice to just be able to specify this in the constructor properties.

    const cfnCluster = cluster.node.defaultChild as ecs.CfnCluster
    cfnCluster.capacityProviders = ['FARGATE', 'FARGATE_SPOT']
    cfnCluster.defaultCapacityProviderStrategy = [
      {
        capacityProvider: 'FARGATE',
        weight: 1,
      },
      {
        capacityProvider: 'FARGATE_SPOT',
        weight: 4,
      }
    ]

Do you maybe know what the first line from your code will look like in python?

airmonitor avatar Jan 28 '21 08:01 airmonitor

@mattaschmann changing the default capacity provider strategy of the cluster should have no effect if LaunchType: FARGATE is specified at the service level, which is the case when instantiating the class FargateService.

pmaoui avatar Jan 28 '21 10:01 pmaoui

@poupougnac You are correct! I misunderstood at what level the problem was. However, the solution still applies, however you need to apply the escape hatch to the service instead:

const fargateService = new ecs.FargateService(this, 'test-fargate-service', {
  cluster: cluster, // Required
  desiredCount: 6, // Default is 1
  taskDefinition: fargateTask,
})
const cfnService = fargateService.node.tryFindChild('Service') as ecs.CfnService
cfnService.launchType = undefined
cfnService.capacityProviderStrategy = [
  {
    capacityProvider: 'FARGATE_SPOT',
    weight: 4,
  },
  {
    capacityProvider: 'FARGATE',
    weight: 1,
  },
]

I'll note that you have to set launchType back to undefined as it won't let you specify that and capacityProviderStrategy at the same time.

@airmonitor as far as python goes, I would checkout the python specific "Modifying the AWS CloudFormation resource behind AWS constructs" section of the escape hatch docs.

mattaschmann avatar Jan 28 '21 17:01 mattaschmann

@mattaschmann Amazing ! I was not aware of this "Escape hatches" possibility. I've done this

const cfnService = fargateService.node.tryFindChild('Service') as ecs.CfnService
cfnService.launchType = 'FARGATE_SPOT'

It works ! Thank you so much

pmaoui avatar Jan 28 '21 19:01 pmaoui

@poupougnac I wasn't able to get it to work with a launchType of FARGATE_SPOT, as it seemed to want either EC2 or FARGATE, I could only get it to work using the capacityProviderStrategy. However, if it's working for you that's great!

mattaschmann avatar Jan 28 '21 21:01 mattaschmann

@mattaschmann you are correct :) ! I have checked the synth output and I didn't create the stack. Using capacityProviderStrategy is the way to go.

Result : image

pmaoui avatar Jan 29 '21 08:01 pmaoui

For the python, I was able to achieve this in two consecutive deployments first run:

    fargate_ecs_service = fargate_service.node.try_find_child("Service")
    fargate_service_cluster = fargate_service.cluster.node.default_child

    fargate_service_cluster.add_override("Properties.CapacityProviders", ["FARGATE", "FARGATE_SPOT"])

second run

    fargate_ecs_service = fargate_service.node.try_find_child("Service")
    fargate_service_cluster = fargate_service.cluster.node.default_child

    fargate_service_cluster.add_override("Properties.CapacityProviders", ["FARGATE", "FARGATE_SPOT"])

    if not self.props["initial_deployment"]:
        fargate_ecs_service.__setattr__(
            "capacity_provider_strategy",
            [
                {
                    "capacityProvider": "FARGATE_SPOT",
                    "weight": 4,
                },
                {
                    "capacityProvider": "FARGATE",
                    "weight": 1,
                },
            ]
        )

I was not able to remove launchType, set it to null as in typescript example above launchType = undefined`, thus two deployments... :/

If anyone have idea how to do this in one run in python please feel free to correct me :)

airmonitor avatar Jan 29 '21 20:01 airmonitor

@airmonitor I don't do my CDK in python, so take what I say with a grain of salt. But is it possible that the difference here is that in TypeScript, launchType is of type string | undefined, so setting it to undefined works. However, in python, it looks like it's an Optional (see: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ecs/CfnService.html).

My guess is that instead of trying to set it to None or undefined, you need to delete it from the dict, i.e.:

fargate_ecs_service = fargate_service.node.try_find_child("Service")
del fargate_ecs_service.launch_type
fargate_ecs_service.capacity_provider_strategy = [
    {
        "capacityProvider": "FARGATE_SPOT",
        "weight": 4,
    },
    {
        "capacityProvider": "FARGATE",
        "weight": 1,
    },
]

Edit: Looks like optional would actually prefer a None, if the previous doesn't work try:

...
fargate_ecs_service.launch_type = None
...

mattaschmann avatar Jan 29 '21 21:01 mattaschmann

@airmonitor I don't do my CDK in python, so take what I say with a grain of salt. But is it possible that the difference here is that in TypeScript, launchType is of type string | undefined, so setting it to undefined works. However, in python, it looks like it's an Optional (see: https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ecs/CfnService.html).

My guess is that instead of trying to set it to None or undefined, you need to delete it from the dict, i.e.:

fargate_ecs_service = fargate_service.node.try_find_child("Service")
del fargate_ecs_service.launch_type
fargate_ecs_service.capacity_provider_strategy = [
    {
        "capacityProvider": "FARGATE_SPOT",
        "weight": 4,
    },
    {
        "capacityProvider": "FARGATE",
        "weight": 1,
    },
]

Edit: Looks like optional would actually prefer a None, if the previous doesn't work try:

...
fargate_ecs_service.launch_type = None
...

Yeah, tried that with deleting the launch_type attribute but it failed as the attribute can't be deleted :/ Somebody with stronger python kung-fu than me is needed :)

airmonitor avatar Jan 29 '21 22:01 airmonitor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Feb 16 '21 19:02 github-actions[bot]