[ecs] Fargate Spot Instances
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
Just found the dependency on: #5471
Any updates?
@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.
@pkandasamy91 any ideas how we can create ecs.Cluster with Networking Only, so those 2 FARGATE providers will get populated automatically? Thanks
Any updates here? Will be great to do a Fargate spot tasks cluster using CDK.
Couldn't a custom resource, backed by lambda, be used to create this?
Isn't this supported in CloudFormation now? Both at the cluster level for a default provider, and at the service level with CapacityProviderStrategyItem?
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
Any updates as CF now supports it ?
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,
}
]
I was able to leverage the
cfnClusterclass 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?
@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.
@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 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
@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 you are correct :) ! I have checked the synth output and I didn't create the stack. Using capacityProviderStrategy is the way to go.
Result :

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
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
...
@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,
launchTypeis of typestring | undefined, so setting it to undefined works. However, in python, it looks like it's anOptional(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
Noneor 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 :)
⚠️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.