Skip to content

How to define Spot instances in ECS

0

I use the follwoing code for my ECS Ec2 Instances. How can I set here a spot instance?

		const ec2AutoScalingGroup = new AutoScalingGroup(this, 'todo-app-asg', {
			vpc: this.mainVpc,
			instanceType: new InstanceType('m6a.2xlarge'), 
			machineImage: EcsOptimizedImage.amazonLinux2(AmiHardwareType.STANDARD),
			desiredCapacity: 1,
			minCapacity: 1,
			maxCapacity: 2,
		  });		
		  ec2AutoScalingGroup.protectNewInstancesFromScaleIn();
		  ec2AutoScalingGroup.connections.addSecurityGroup(securityGroup);		  

		  const capacityProviderDef = new AsgCapacityProvider(this, 'todo-app-capacity-provider', {
			autoScalingGroup: ec2AutoScalingGroup,
			enableManagedTerminationProtection: true,
			enableManagedScaling: true,			
		  });
	  		  
		  cluster.addAsgCapacityProvider(capacityProviderDef);

asked 2 years ago508 views

1 Answer
0

To use Spot Instances in your ECS cluster with the given code, you need to modify the AutoScalingGroup configuration. Here's how you can adjust your code to use Spot Instances:

  1. In the AutoScalingGroup constructor, add a new property called 'spotPrice'. This sets the maximum price you're willing to pay for Spot Instances.

  2. Optionally, you can use a mix of On-Demand and Spot Instances for better reliability. To do this, add the 'mixedInstancesPolicy' property.

Here's how your modified code would look:

const ec2AutoScalingGroup = new AutoScalingGroup(this, 'todo-app-asg', {
    vpc: this.mainVpc,
    instanceType: new InstanceType('m6a.2xlarge'),
    machineImage: EcsOptimizedImage.amazonLinux2(AmiHardwareType.STANDARD),
    desiredCapacity: 1,
    minCapacity: 1,
    maxCapacity: 2,
    spotPrice: '0.5', // Set your maximum Spot price here
    mixedInstancesPolicy: {
        instancesDistribution: {
            onDemandPercentageAboveBaseCapacity: 50, // 50% On-Demand, 50% Spot
        },
    },
});

In this configuration:

  • The 'spotPrice' is set to '0.5', meaning you're willing to pay up to $0.50 per hour for each Spot Instance. Adjust this value based on your budget and the current Spot prices for the instance type you're using.

  • The 'mixedInstancesPolicy' is configured to use 50% On-Demand Instances and 50% Spot Instances above the base capacity. You can adjust this percentage as needed.

Remember that Spot Instances can be interrupted with a two-minute warning when EC2 needs the capacity back. Ensure your application can handle instance interruptions gracefully. For ECS workloads, this typically means designing your tasks to be stateless and using appropriate task placement strategies.
Sources
When to Use Spot Instances - Overview of Amazon EC2 Spot Instances
How Spot Instances work - Amazon Elastic Compute Cloud
How to Launch Spot Instances - Overview of Amazon EC2 Spot Instances

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.