@mock_ec2
def test_eip_boto3_vpc_association():
 session = boto3.session.Session(region_name=u'us-west-1')
 service = session.resource(u'ec2')
 client = session.client(u'ec2')
 vpc_res = client.create_vpc(CidrBlock=u'10.0.0.0/24')
 subnet_res = client.create_subnet(VpcId=vpc_res[u'Vpc'][u'VpcId'], CidrBlock=u'10.0.0.0/24')
 instance = service.create_instances(**{u'InstanceType': u't2.micro', u'ImageId': u'ami-test', u'MinCount': 1, u'MaxCount': 1, u'SubnetId': subnet_res[u'Subnet'][u'SubnetId']})[0]
 allocation_id = client.allocate_address(Domain=u'vpc')[u'AllocationId']
 association_id = client.associate_address(InstanceId=instance.id, AllocationId=allocation_id, AllowReassociation=False)
 instance.load()
 instance.public_ip_address.should_not.be.none
 instance.public_dns_name.should_not.be.none

@ mock_ec2 def test_eip_boto3_vpc_association ( ) : session = boto3 . session . Session ( region_name = u'us-west-1' ) service = session . resource ( u'ec2' ) client = session . client ( u'ec2' ) vpc_res = client . create_vpc ( CidrBlock = u'10.0.0.0/24' ) subnet_res = client . create_subnet ( VpcId = vpc_res [ u'Vpc' ] [ u'VpcId' ] , CidrBlock = u'10.0.0.0/24' ) instance = service . create_instances ( ** { u'InstanceType' : u't2.micro' , u'ImageId' : u'ami-test' , u'MinCount' : 1 , u'MaxCount' : 1 , u'SubnetId' : subnet_res [ u'Subnet' ] [ u'SubnetId' ] } ) [ 0 ] allocation_id = client . allocate_address ( Domain = u'vpc' ) [ u'AllocationId' ] association_id = client . associate_address ( InstanceId = instance . id , AllocationId = allocation_id , AllowReassociation = False ) instance . load ( ) instance . public_ip_address . should_not . be . none instance . public_dns_name . should_not . be . none
associate eip to vpc instance in a new subnet with boto3 .