Exemplo n.º 1
0
def terminate(elastic_IP):
    global allocation_id

    ec2 = boto3.client('ec2')
    conn = boto.ec2.connect_to_region("us-east-1")

    filters = {"ip-address": elastic_IP}
    instances = conn.get_only_instances(filters=filters)

    filters = [{'Name': 'public-ip', 'Values': [elastic_IP]}]

    addresses = ec2.describe_addresses(Filters=filters)

    inst_id = str(instances[0].id)
    alloc_id = addresses['Addresses'][0]['AllocationId']

    ec2.release_address(AllocationId=alloc_id)
    conn.terminate_instances(instance_ids=[
        inst_id,
    ])

    while instances[0].update(
    ) != "terminated":  #wait until the instance has been terminated
        time.sleep(5)

    ec2.delete_security_group(GroupName='csc326-group23')
    ec2.delete_key_pair(KeyName='key')
Exemplo n.º 2
0
def test_classic_eip():
    template_json = json.dumps(ec2_classic_eip.template)
    cf = boto3.client("cloudformation", region_name="us-west-1")
    stack_name = str(uuid4())[0:6]
    cf.create_stack(StackName=stack_name, TemplateBody=template_json)
    ec2 = boto3.client("ec2", region_name="us-west-1")
    all_ips = [
        eip["PublicIp"] for eip in ec2.describe_addresses()["Addresses"]
    ]

    resources = cf.list_stack_resources(
        StackName=stack_name)["StackResourceSummaries"]
    cfn_eip = [
        resource for resource in resources
        if resource["ResourceType"] == "AWS::EC2::EIP"
    ][0]
    all_ips.should.contain(cfn_eip["PhysicalResourceId"])
Exemplo n.º 3
0
def test_vpc_single_instance_in_subnet():
    template_json = json.dumps(vpc_single_instance_in_subnet.template)
    cf = boto3.client("cloudformation", region_name="us-west-1")
    stack_name = str(uuid4())[0:6]
    cf.create_stack(
        StackName=stack_name,
        TemplateBody=template_json,
        Parameters=[{
            "ParameterKey": "KeyName",
            "ParameterValue": "my_key"
        }],
    )

    ec2 = boto3.client("ec2", region_name="us-west-1")

    stack = cf.describe_stacks(StackName=stack_name)["Stacks"][0]

    resources = cf.list_stack_resources(
        StackName=stack_name)["StackResourceSummaries"]
    vpc_id = [
        resource for resource in resources
        if resource["ResourceType"] == "AWS::EC2::VPC"
    ][0]["PhysicalResourceId"]

    vpc = ec2.describe_vpcs(VpcIds=[vpc_id])["Vpcs"][0]
    vpc["CidrBlock"].should.equal("10.0.0.0/16")
    vpc["Tags"].should.contain({
        "Key": "Application",
        "Value": stack["StackId"]
    })

    security_group = ec2.describe_security_groups(
        Filters=[{
            "Name": "vpc-id",
            "Values": [vpc["VpcId"]]
        }])["SecurityGroups"][0]
    security_group["VpcId"].should.equal(vpc["VpcId"])

    subnet_id = [
        resource for resource in resources
        if resource["ResourceType"] == "AWS::EC2::Subnet"
    ][0]["PhysicalResourceId"]

    subnet = ec2.describe_subnets(SubnetIds=[subnet_id])["Subnets"][0]
    subnet["VpcId"].should.equal(vpc["VpcId"])

    instance_id = [
        resource for resource in resources
        if resource["ResourceType"] == "AWS::EC2::Instance"
    ][0]["PhysicalResourceId"]
    res = ec2.describe_instances(InstanceIds=[instance_id])["Reservations"][0]
    instance = res["Instances"][0]
    instance["Tags"].should.contain({"Key": "Foo", "Value": "Bar"})

    eip_id = [
        resource for resource in resources
        if resource["ResourceType"] == "AWS::EC2::EIP"
    ][0]["PhysicalResourceId"]
    eip = ec2.describe_addresses(PublicIps=[eip_id])["Addresses"][0]
    eip["Domain"].should.equal("vpc")
    eip["InstanceId"].should.equal(instance["InstanceId"])