From a4aa2b6a59928f90af789c9e5f76dadfdcf31651 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 1 Apr 2026 17:59:30 +0530 Subject: [PATCH 1/3] Add test --- tests/test_ec2/test_subnets.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_ec2/test_subnets.py b/tests/test_ec2/test_subnets.py index 8f971400c5fe..9151f875a39e 100644 --- a/tests/test_ec2/test_subnets.py +++ b/tests/test_ec2/test_subnets.py @@ -1017,6 +1017,43 @@ def test_create_ipv6native_subnet(account_id, ec2_client=None, vpc_id=None): ec2_client.delete_subnet(SubnetId=subnet["SubnetId"]) +@pytest.mark.aws_verified +@ec2_aws_verified(create_vpc=True) +def test_private_dns_name_options(ec2_client=None, vpc_id=None): + subnet = None + try: + subnet = ec2_client.create_subnet( + VpcId=vpc_id, + CidrBlock="10.0.0.0/24", + )["Subnet"] + + assert "PrivateDnsNameOptionsOnLaunch" in subnet + assert subnet["PrivateDnsNameOptionsOnLaunch"]["HostnameType"] == "ip-name" + assert ( + subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"] + is False + ) + assert ( + subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsAAAARecord"] + is False + ) + + ec2_client.modify_subnet_attribute( + SubnetId=subnet["SubnetId"], + EnableResourceNameDnsARecordOnLaunch={"Value": True}, + ) + subnet = ec2_client.describe_subnets(SubnetIds=[subnet["SubnetId"]])["Subnets"][ + 0 + ] + assert ( + subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"] + is True + ) + finally: + if subnet: + ec2_client.delete_subnet(SubnetId=subnet["SubnetId"]) + + @mock_aws def test_create_subnet_cidr_reservations() -> None: ec2 = boto3.client("ec2", region_name="us-west-1") From 1ada0cbe206a87995f62509efc3958499d3216d9 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 1 Apr 2026 17:59:52 +0530 Subject: [PATCH 2/3] Add implementation --- moto/ec2/models/subnets.py | 11 ++++++++++- moto/ec2/responses/subnets.py | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/moto/ec2/models/subnets.py b/moto/ec2/models/subnets.py index 0197eab4b2dc..0c4ffb7f3fdc 100644 --- a/moto/ec2/models/subnets.py +++ b/moto/ec2/models/subnets.py @@ -97,6 +97,11 @@ def __init__( self.map_public_ip_on_launch = map_public_ip_on_launch self.assign_ipv6_address_on_creation = ipv6_native self.ipv6_cidr_block_associations: dict[str, dict[str, Any]] = {} + self.private_dns_name_options_on_launch = { + "HostnameType": "ip-name", + "EnableResourceNameDnsARecord": False, + "EnableResourceNameDnsAAAARecord": False, + } if ipv6_cidr_block: self.attach_ipv6_cidr_block_associations(ipv6_cidr_block) @@ -631,11 +636,15 @@ def delete_subnet(self, subnet_id: str) -> Subnet: raise InvalidSubnetIdError(subnet_id) def modify_subnet_attribute( - self, subnet_id: str, attr_name: str, attr_value: str + self, subnet_id: str, attr_name: str, attr_value: str | bool ) -> None: subnet = self.get_subnet(subnet_id) if attr_name in ("map_public_ip_on_launch", "assign_ipv6_address_on_creation"): setattr(subnet, attr_name, attr_value) + elif attr_name == "enable_resource_name_dns_a_record_on_launch": + subnet.private_dns_name_options_on_launch[ + "EnableResourceNameDnsARecord" + ] = attr_value else: raise InvalidParameterValueError(attr_name) diff --git a/moto/ec2/responses/subnets.py b/moto/ec2/responses/subnets.py index 1b9fdd3782a8..27e96ac54487 100644 --- a/moto/ec2/responses/subnets.py +++ b/moto/ec2/responses/subnets.py @@ -67,7 +67,11 @@ def describe_subnets(self) -> ActionResult: def modify_subnet_attribute(self) -> ActionResult: subnet_id = self._get_param("SubnetId") - for attribute in ("MapPublicIpOnLaunch", "AssignIpv6AddressOnCreation"): + for attribute in ( + "MapPublicIpOnLaunch", + "AssignIpv6AddressOnCreation", + "EnableResourceNameDnsARecordOnLaunch", + ): if self._get_param(f"{attribute}.Value") is not None: attr_name = camelcase_to_underscores(attribute) attr_value = self._get_param(f"{attribute}.Value") From 7b60dc2e87b2258f2945565a40c37b16efed60dd Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 1 Apr 2026 18:38:00 +0530 Subject: [PATCH 3/3] Fix types --- moto/ec2/models/subnets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moto/ec2/models/subnets.py b/moto/ec2/models/subnets.py index 0c4ffb7f3fdc..272fab44a5d3 100644 --- a/moto/ec2/models/subnets.py +++ b/moto/ec2/models/subnets.py @@ -636,7 +636,7 @@ def delete_subnet(self, subnet_id: str) -> Subnet: raise InvalidSubnetIdError(subnet_id) def modify_subnet_attribute( - self, subnet_id: str, attr_name: str, attr_value: str | bool + self, subnet_id: str, attr_name: str, attr_value: bool ) -> None: subnet = self.get_subnet(subnet_id) if attr_name in ("map_public_ip_on_launch", "assign_ipv6_address_on_creation"):