diff --git a/moto/ec2/models.py b/moto/ec2/models.py index b6c9bf178..0b6e252c4 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -410,7 +410,10 @@ class NetworkInterface(TaggedEC2Resource, CloudFormationModel): self.check_auto_public_ip() def check_auto_public_ip(self): - if self.public_ip_auto_assign: + if ( + self.public_ip_auto_assign + and str(self.public_ip_auto_assign).lower() == "true" + ): self.public_ip = random_public_ip() @property diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 260c74a21..713bf4cd9 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -3044,6 +3044,58 @@ def test_create_instance_with_launch_template_id_produces_no_warning( assert len(captured_warnings) == 0 +@mock_ec2 +def test_run_instance_and_associate_public_ip(): + ec2 = boto3.resource("ec2", "us-west-1") + vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") + subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/18") + + # Do not pass AssociatePublicIpAddress-argument + instance = ec2.create_instances( + ImageId=EXAMPLE_AMI_ID, + MinCount=1, + MaxCount=1, + NetworkInterfaces=[{"DeviceIndex": 0}], + SubnetId=subnet.id, + )[0] + interfaces = instance.network_interfaces_attribute + addresses = interfaces[0]["PrivateIpAddresses"][0] + addresses.should.have.key("Primary").equal(True) + addresses.should.have.key("PrivateIpAddress") + addresses.shouldnt.have.key("Association") + + # Pass AssociatePublicIpAddress=False + instance = ec2.create_instances( + ImageId=EXAMPLE_AMI_ID, + MinCount=1, + MaxCount=1, + NetworkInterfaces=[{"DeviceIndex": 0, "AssociatePublicIpAddress": False}], + SubnetId=subnet.id, + )[0] + interfaces = instance.network_interfaces_attribute + addresses = interfaces[0]["PrivateIpAddresses"][0] + addresses.should.have.key("Primary").equal(True) + addresses.should.have.key("PrivateIpAddress") + addresses.shouldnt.have.key("Association") + + # Pass AssociatePublicIpAddress=True + instance = ec2.create_instances( + ImageId=EXAMPLE_AMI_ID, + MinCount=1, + MaxCount=1, + NetworkInterfaces=[{"DeviceIndex": 0, "AssociatePublicIpAddress": True}], + SubnetId=subnet.id, + )[0] + interfaces = instance.network_interfaces_attribute + addresses = interfaces[0]["PrivateIpAddresses"][0] + addresses.should.have.key("Primary").equal(True) + addresses.should.have.key("PrivateIpAddress") + addresses.should.have.key("Association") + # Only now should we have a PublicIp + addresses["Association"].should.have.key("IpOwnerId").equal(ACCOUNT_ID) + addresses["Association"].should.have.key("PublicIp") + + def retrieve_all_reservations(client, filters=[]): resp = client.describe_instances(Filters=filters) all_reservations = resp["Reservations"]