Run fleet with on demand instance across AZ

0

Hello,
I wanted to start EC2 fleet with on-demand instances only, and I wanted them to be distributed across availability zones. Unfortunately, I couldn't find a way to do that, and all the instances are always started in a single AZ.
That is not a problem with spot instances, as they spawn in all the AZ.
I was trying to try different allocation strategies and priorities, but nothing helped.
I was trying to do so in AWS-CDK, using both CfnEC2Fleet link as well as CfnSpotFleet link. Bellow is my code.
Is there way how to achieve that, or do I need to use something else?
Thank you.

const spotFleet = new CfnSpotFleet(stack, 'EC2-Fleet', {
    spotFleetRequestConfigData: {
        allocationStrategy: 'lowestPrice',
        targetCapacity: 8,
        iamFleetRole: fleetRole.roleArn,
        spotMaintenanceStrategies: {
            capacityRebalance: {
                replacementStrategy: 'launch-before-terminate',
                terminationDelay: 120,
            }
        },
        onDemandTargetCapacity: 4,
        instancePoolsToUseCount: stack.availabilityZones.length,
        launchTemplateConfigs: [{
            launchTemplateSpecification: {
                launchTemplateId: launchTemplate.launchTemplateId,
                version: launchTemplate.latestVersionNumber,
            },
            overrides: privateSubnets.map(subnet => ({
                availabilityZone: subnet.subnetAvailabilityZone,
                subnetId: subnet.subnetId,
            })),
        }],
    }
});

const ec2Fleet = new CfnEC2Fleet(stack, 'EC2-EcFleet', {
    targetCapacitySpecification: {
        totalTargetCapacity: 6,
        onDemandTargetCapacity: 6,
        defaultTargetCapacityType: 'on-demand',
    },
    replaceUnhealthyInstances: true,
    onDemandOptions: {
        allocationStrategy: 'prioritized',
    },
    launchTemplateConfigs: [{
        launchTemplateSpecification: {
            launchTemplateId: launchTemplate.launchTemplateId,
            version: launchTemplate.latestVersionNumber,
        },
        overrides: privateSubnets.map(subnet => ({
            availabilityZone: subnet.subnetAvailabilityZone,
            subnetId: subnet.subnetId,
        })),
    }]
});

Where launchTemplate is instance of LaunchTemplate and privateSubnets is array of Subnet instances, one for each AZ.

질문됨 2년 전256회 조회
1개 답변
0

Hello!

On-Demand Fleets, unlike Spot Fleets, do not spread across availability zones by default. The best way you could achieve a similar effect with On-Demand instances would be to use On-Demand Capacity Reservations across multiple AZs.

Here's the CDK documentation on ODCRs:https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnCapacityReservation.html

You will want these capacity reservations to be referenced by the Launch Template you are using for your On-Demand Fleet. You can do that by adding them to the launchTemplateData portion of the template.

This guide goes through the steps for setting up such reservations and adding them to the template, though this uses the AWS CLI rather than the CDK: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-fleet-launch-on-demand-instances-using-targeted-capacity-reservations-walkthrough.html

One final note: Don't confuse these with the CfnCapacityReservationFleet object. These do create Capacity Reservation Fleets, but such fleets only exist in a single AZ. For multi-AZ setups, creating separate capacity reservations and referencing them in the launch template is best.

Hope this helps!

Mark_G
답변함 9달 전
AWS
지원 엔지니어
검토됨 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠