diff --git a/moto/ec2/models/subnets.py b/moto/ec2/models/subnets.py index 0197eab4b2dc..272fab44a5d3 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: 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") 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")