def get_running_ips(aws_region): ##### Get all regions ec2 = boto3.client('ec2', region_name='us-east-1') regionList = [] response = ec2.describe_regions() regions = response['Regions'] for r in regions: regionList.append(r['RegionName']) ##### Get running IPs for all regions running_ips = [] for region in regionList: ec2 = boto3.client('ec2', region_name=region) reservations = ec2.describe_instances()['Reservations'] for reservation in reservations: for instance in reservation['Instances']: try: if instance['State']['Name'] == 'running': if 'VpcId' in instance: running_ips.append(instance['PrivateIpAddress']) else: running_ips.append(instance['PublicIpAddress']) except: pass return running_ips
def test_volume_size_through_cloudformation(): ec2 = boto3.client("ec2", region_name="us-east-1") cf = boto3.client("cloudformation", region_name="us-east-1") tag_value = str(uuid4()) volume_template = { "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "testInstance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": EXAMPLE_AMI_ID, "KeyName": "dummy", "InstanceType": "t2.micro", "BlockDeviceMappings": [{ "DeviceName": "/dev/sda2", "Ebs": { "VolumeSize": "50" } }], "Tags": [ { "Key": "foo", "Value": "bar" }, { "Key": "blah", "Value": tag_value }, ], }, } }, } template_json = json.dumps(volume_template) stack_name = str(uuid4())[0:6] cf.create_stack(StackName=stack_name, TemplateBody=template_json) resource = cf.list_stack_resources( StackName=stack_name)["StackResourceSummaries"][0] resource.should.have.key("LogicalResourceId").being.equal("testInstance") resource.should.have.key("PhysicalResourceId").shouldnt.be.none resource.should.have.key("ResourceType").being.equal("AWS::EC2::Instance") instances = ec2.describe_instances( InstanceIds=[resource["PhysicalResourceId"]]) volume = instances["Reservations"][0]["Instances"][0][ "BlockDeviceMappings"][0]["Ebs"] volumes = ec2.describe_volumes(VolumeIds=[volume["VolumeId"]]) volumes["Volumes"][0]["Size"].should.equal(50)
def test_volume_size_through_cloudformation(): ec2 = boto3.client("ec2", region_name="us-east-1") cf = boto3.client("cloudformation", region_name="us-east-1") volume_template = { "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "testInstance": { "Type": "AWS::EC2::Instance", "Properties": { "ImageId": EXAMPLE_AMI_ID, "KeyName": "dummy", "InstanceType": "t2.micro", "BlockDeviceMappings": [{ "DeviceName": "/dev/sda2", "Ebs": { "VolumeSize": "50" } }], "Tags": [ { "Key": "foo", "Value": "bar" }, { "Key": "blah", "Value": "baz" }, ], }, } }, } template_json = json.dumps(volume_template) cf.create_stack(StackName="test_stack", TemplateBody=template_json) instances = ec2.describe_instances() volume = instances["Reservations"][0]["Instances"][0][ "BlockDeviceMappings"][0]["Ebs"] volumes = ec2.describe_volumes(VolumeIds=[volume["VolumeId"]]) volumes["Volumes"][0]["Size"].should.equal(50)
def get_running_instances(region): for ses in ['default', 'ci', 'dev', 'siva']: boto3.setup_default_session(profile_name=ses) ec2 = boto3.client('ec2', region) reservations = ec2.describe_instances() for reservation in reservations["Reservations"]: for instance in reservation["Instances"]: if instance["State"]["Name"] == "running": if not instance.has_key('SpotInstanceRequestId'): for tagname in instance["Tags"]: if tagname["Key"] == "Name": if instance["InstanceType"] in final: final[instance["InstanceType"]] = final[ instance["InstanceType"]] + 1 else: final[instance["InstanceType"]] = 1 for key, value in final.viewitems(): # print region, ":", key, value running[region][key] = value
def test_single_instance_with_ebs_volume(): template_json = json.dumps(single_instance_with_ebs_volume.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": "key_name" }], ) resources = cf.list_stack_resources( StackName=stack_name)["StackResourceSummaries"] instance_id = [ r["PhysicalResourceId"] for r in resources if r["ResourceType"] == "AWS::EC2::Instance" ][0] volume_id = [ r["PhysicalResourceId"] for r in resources if r["ResourceType"] == "AWS::EC2::Volume" ][0] ec2 = boto3.client("ec2", region_name="us-west-1") ec2_instance = ec2.describe_instances( InstanceIds=[instance_id])["Reservations"][0]["Instances"][0] volumes = ec2.describe_volumes(VolumeIds=[volume_id])["Volumes"] # Grab the mounted drive volume = [ volume for volume in volumes if volume["Attachments"][0]["Device"] == "/dev/sdh" ][0] volume["State"].should.equal("in-use") volume["Attachments"][0]["InstanceId"].should.equal( ec2_instance["InstanceId"])
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"])
# ## Utilities # def block_until_running(self, instances): ''' Blocks until all defined instances have reached running state and an ip has been assigned''' ## Run describe instances until everyone is running tmpinstances = instances instances = [] while len(tmpinstances) > 0: time.sleep(30) print "Waiting for", len(tmpinstances), "instances." sys.stdout.flush() all_running_instances = self.describe_instances("running") # print all_running_instances # print tmpinstances for i in range(0, len(all_running_instances)): for j in range(0, len(tmpinstances)): if (all_running_instances[i].id == tmpinstances[j].id ) and (not (all_running_instances[i].public_dns_name == "0.0.0.0")): tmpinstances.pop(j) instances.append(all_running_instances[i]) break self.describe_instances() return instances if __name__ == "__main__": ec2 = EC2Cluster() ec2.describe_instances("cluster")