EC2 - Bugfix where associate_public_ip was always True (#4404)

This commit is contained in:
Bert Blommers 2021-10-13 09:57:39 +00:00 committed by GitHub
parent 4d10b11ecb
commit b95bda11f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -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

View File

@ -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"]