Skip to content

Commit b2929ff

Browse files
ec2: fix fleet launch template versioning
1 parent 7ac979b commit b2929ff

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

moto/ec2/models/fleets.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def __init__(
8686
# Always include the template ID in response (AWS does this even when name is used)
8787
resolved_launch_spec["LaunchTemplateId"] = launch_template.id
8888

89-
launch_template_data = launch_template.latest_version().data
89+
template_version = resolved_launch_spec.get(
90+
"Version", launch_template.default_version_number
91+
)
92+
launch_template_data = launch_template.get_version(template_version).data
9093
overrides_list = config.get("Overrides") or [{}]
9194
for override in overrides_list:
9295
# Merge launch template data with override

tests/test_ec2/test_fleets.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ def test_user_data():
914914
@pytest.mark.parametrize("version_specified", ["$Latest", "$Default"])
915915
def test_version_resolves_to_actual_version_number(version_specified, ec2_client=None):
916916
"""Test that $Latest or $Default in a fleet LaunchTemplateSpecification resolves to the actual version number"""
917-
with launch_template_context() as ctxt:
917+
with launch_template_context(region=ec2_client.meta.region_name) as ctxt:
918918
fleet_response = ctxt.ec2.create_fleet(
919919
LaunchTemplateConfigs=[
920920
{
@@ -946,6 +946,45 @@ def test_version_resolves_to_actual_version_number(version_specified, ec2_client
946946
ctxt.ec2.delete_fleets(FleetIds=[fleet_id], TerminateInstances=True)
947947

948948

949+
@pytest.mark.aws_verified
950+
@ec2_aws_verified()
951+
def test_fleet_uses_data_from_specified_template_version(ec2_client=None):
952+
with launch_template_context(region=ec2_client.meta.region_name) as ctxt:
953+
# Version 1 was created by the context manager with t2.micro;
954+
# add version 2 with a different instance type
955+
ctxt.ec2.create_launch_template_version(
956+
LaunchTemplateId=ctxt.lt_id,
957+
LaunchTemplateData={"InstanceType": "t3.medium"},
958+
)
959+
960+
fleet = ctxt.ec2.create_fleet(
961+
LaunchTemplateConfigs=[
962+
{
963+
"LaunchTemplateSpecification": {
964+
"LaunchTemplateId": ctxt.lt_id,
965+
"Version": "1",
966+
},
967+
}
968+
],
969+
TargetCapacitySpecification={
970+
"TotalTargetCapacity": 1,
971+
"OnDemandTargetCapacity": 1,
972+
"DefaultTargetCapacityType": "on-demand",
973+
},
974+
OnDemandOptions={"AllocationStrategy": "lowest-price"},
975+
Type="instant",
976+
)
977+
fleet_id = fleet["FleetId"]
978+
instance_id = fleet["Instances"][0]["InstanceIds"][0]
979+
try:
980+
instance = ctxt.ec2.describe_instances(InstanceIds=[instance_id])[
981+
"Reservations"
982+
][0]["Instances"][0]
983+
assert instance["InstanceType"] == "t2.micro"
984+
finally:
985+
ctxt.ec2.delete_fleets(FleetIds=[fleet_id], TerminateInstances=True)
986+
987+
949988
@pytest.mark.aws_verified
950989
@ec2_aws_verified()
951990
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)