|
11 | 11 | region = 'eu-west-1' # AWS region |
12 | 12 |
|
13 | 13 | def describe_instances(): |
14 | | - """ |
15 | | - Describes all EC2 instances associated with an AWS account |
16 | | - """ |
17 | | - # Create an EC2 Client |
18 | | - ec2client = boto3.client('ec2', region_name=region) |
19 | | - # Describe instances |
20 | | - response = ec2client.describe_instances() |
21 | | - print(response) |
22 | | - print('\nInstances:') |
23 | | - for i in response['Reservations']: |
24 | | - for j in i['Instances']: |
25 | | - print('state of the instance "' + j['InstanceId'] + '" is: "' + j['State']['Name'] + '"') |
26 | | - return |
| 14 | + """ |
| 15 | + Describes all EC2 instances associated with an AWS account |
| 16 | + """ |
| 17 | + # Create an EC2 Client |
| 18 | + ec2_client = boto3.client('ec2', region_name=region) |
| 19 | + # Describe instances |
| 20 | + response = ec2_client.describe_instances() |
| 21 | + print(response) |
| 22 | + print('\nInstances:') |
| 23 | + for i in response['Reservations']: |
| 24 | + for j in i['Instances']: |
| 25 | + print('state of the instance "' + j['InstanceId'] + '" is: "' + j['State']['Name'] + '"') |
| 26 | + return |
27 | 27 |
|
28 | 28 |
|
29 | 29 | def run_instance(): |
30 | | - """ |
31 | | - Run an EC2 instance |
32 | | - """ |
33 | | - ami_id = "ami-785db401" # AMI Id |
34 | | - instance_type = "t2.micro" # Instance Type |
35 | | - |
36 | | - # Create an EC2 Client |
37 | | - ec2client = boto3.client('ec2', region_name=region) |
38 | | - # Run an instance |
39 | | - response = ec2client.run_instances(ImageId=ami_id, InstanceType=instance_type, |
40 | | - MaxCount=1, MinCount=1) |
41 | | - print(response) |
42 | | - Instance_id = response['Instances'][0]['InstanceId'] |
43 | | - print('\nInstance Id: ' + Instance_id) |
44 | | - print('Image Id: ' + response['Instances'][0]['ImageId']) |
45 | | - print('Instance Type: ' + response['Instances'][0]['InstanceType']) |
46 | | - print('State: ' + response['Instances'][0]['State']['Name']) |
47 | | - |
48 | | - return Instance_id |
| 30 | + """ |
| 31 | + Run an EC2 instance |
| 32 | + """ |
| 33 | + ami_id = "ami-785db401" # AMI Id |
| 34 | + instance_type = "t2.micro" # Instance Type |
| 35 | + |
| 36 | + # Create an EC2 Client |
| 37 | + ec2_client = boto3.client('ec2', region_name=region) |
| 38 | + # Run an instance |
| 39 | + response = ec2_client.run_instances(ImageId=ami_id, InstanceType=instance_type, |
| 40 | + MaxCount=1, MinCount=1) |
| 41 | + print(response) |
| 42 | + Instance_id = response['Instances'][0]['InstanceId'] |
| 43 | + print('\nInstance Id: ' + Instance_id) |
| 44 | + print('Image Id: ' + response['Instances'][0]['ImageId']) |
| 45 | + print('Instance Type: ' + response['Instances'][0]['InstanceType']) |
| 46 | + print('State: ' + response['Instances'][0]['State']['Name']) |
| 47 | + |
| 48 | + return Instance_id |
49 | 49 |
|
50 | 50 |
|
51 | 51 | def describe_instance(instance_id): |
52 | | - """ |
53 | | - Describes an EC2 instance |
54 | | - """ |
55 | | - # Create an EC2 Client |
56 | | - ec2client = boto3.client('ec2', region_name=region) |
57 | | - try: |
58 | | - # Describe an instance |
59 | | - response = ec2client.describe_instances(InstanceIds=[instance_id]) |
60 | | - print(response) |
61 | | - print('\nInstance Id: ' + instance_id) |
62 | | - print('Instance Id: ' + response['Reservations'][0]['Instances'][0]['InstanceId']) |
63 | | - print('Image Id: ' + response['Reservations'][0]['Instances'][0]['ImageId']) |
64 | | - print('Instance Type: ' + response['Reservations'][0]['Instances'][0]['InstanceType']) |
65 | | - print('State: ' + response['Reservations'][0]['Instances'][0]['State']['Name']) |
66 | | - if response['Reservations'][0]['Instances'][0]['State']['Name'] == 'running': |
67 | | - print('Private DNS Name: ' + response['Reservations'][0]['Instances'][0]['PrivateDnsName']) |
68 | | - print('Private IP: ' + response['Reservations'][0]['Instances'][0]['PrivateIpAddress']) |
69 | | - print('Public DNS Name: ' + response['Reservations'][0]['Instances'][0]['PublicDnsName']) |
70 | | - print('Public IP: ' + response['Reservations'][0]['Instances'][0]['PublicIpAddress']) |
71 | | - except botocore.exceptions.ClientError as e: |
72 | | - if e.response['Error']['Code'] == "MissingParameter": |
73 | | - print("Error: Missing instance id!!") |
74 | | - else: |
75 | | - raise |
76 | | - return |
| 52 | + """ |
| 53 | + Describes an EC2 instance |
| 54 | + """ |
| 55 | + # Create an EC2 Client |
| 56 | + ec2_client = boto3.client('ec2', region_name=region) |
| 57 | + try: |
| 58 | + # Describe an instance |
| 59 | + response = ec2_client.describe_instances(InstanceIds=[instance_id]) |
| 60 | + print(response) |
| 61 | + print('\nInstance Id: ' + instance_id) |
| 62 | + print('Instance Id: ' + response['Reservations'][0]['Instances'][0]['InstanceId']) |
| 63 | + print('Image Id: ' + response['Reservations'][0]['Instances'][0]['ImageId']) |
| 64 | + print('Instance Type: ' + response['Reservations'][0]['Instances'][0]['InstanceType']) |
| 65 | + print('State: ' + response['Reservations'][0]['Instances'][0]['State']['Name']) |
| 66 | + if response['Reservations'][0]['Instances'][0]['State']['Name'] == 'running': |
| 67 | + print('Private DNS Name: ' + response['Reservations'][0]['Instances'][0]['PrivateDnsName']) |
| 68 | + print('Private IP: ' + response['Reservations'][0]['Instances'][0]['PrivateIpAddress']) |
| 69 | + print('Public DNS Name: ' + response['Reservations'][0]['Instances'][0]['PublicDnsName']) |
| 70 | + print('Public IP: ' + response['Reservations'][0]['Instances'][0]['PublicIpAddress']) |
| 71 | + except botocore.exceptions.ClientError as e: |
| 72 | + if e.response['Error']['Code'] == "MissingParameter": |
| 73 | + print("Error: Missing instance id!!") |
| 74 | + else: |
| 75 | + raise |
| 76 | + return |
77 | 77 |
|
78 | 78 |
|
79 | 79 | def start_instance(instance_id): |
80 | | - """ |
81 | | - Start an EC2 instance |
82 | | - """ |
83 | | - # Create an EC2 Client |
84 | | - ec2client = boto3.client('ec2', region_name=region) |
85 | | - try: |
86 | | - # Start an instance |
87 | | - response = ec2client.start_instances(InstanceIds=[instance_id], DryRun=False) |
88 | | - print(response) |
89 | | - print("\nSuccessfully starting instance: ", instance_id) |
90 | | - except botocore.exceptions.ClientError as e: |
91 | | - if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
92 | | - print("Error: Invalid instance id!!") |
93 | | - else: |
94 | | - raise |
95 | | - return |
| 80 | + """ |
| 81 | + Start an EC2 instance |
| 82 | + """ |
| 83 | + # Create an EC2 Client |
| 84 | + ec2_client = boto3.client('ec2', region_name=region) |
| 85 | + try: |
| 86 | + # Start an instance |
| 87 | + response = ec2_client.start_instances(InstanceIds=[instance_id], DryRun=False) |
| 88 | + print(response) |
| 89 | + print("\nSuccessfully starting instance: ", instance_id) |
| 90 | + except botocore.exceptions.ClientError as e: |
| 91 | + if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
| 92 | + print("Error: Invalid instance id!!") |
| 93 | + else: |
| 94 | + raise |
| 95 | + return |
96 | 96 |
|
97 | 97 |
|
98 | 98 | def stop_instance(instance_id): |
99 | | - """ |
100 | | - Stop an EC2 instance |
101 | | - """ |
102 | | - # Create an EC2 Client |
103 | | - ec2client = boto3.client('ec2', region_name=region) |
104 | | - try: |
105 | | - # Stop an instance |
106 | | - response = ec2client.stop_instances(InstanceIds=[instance_id], DryRun=False) |
107 | | - print(response) |
108 | | - print("\nSuccessfully stopping instance: ", instance_id) |
109 | | - except botocore.exceptions.ClientError as e: |
110 | | - if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
111 | | - print("Error: Invalid instance id!!") |
112 | | - else: |
113 | | - raise |
114 | | - return |
| 99 | + """ |
| 100 | + Stop an EC2 instance |
| 101 | + """ |
| 102 | + # Create an EC2 Client |
| 103 | + ec2_client = boto3.client('ec2', region_name=region) |
| 104 | + try: |
| 105 | + # Stop an instance |
| 106 | + response = ec2_client.stop_instances(InstanceIds=[instance_id], DryRun=False) |
| 107 | + print(response) |
| 108 | + print("\nSuccessfully stopping instance: ", instance_id) |
| 109 | + except botocore.exceptions.ClientError as e: |
| 110 | + if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
| 111 | + print("Error: Invalid instance id!!") |
| 112 | + else: |
| 113 | + raise |
| 114 | + return |
115 | 115 |
|
116 | 116 |
|
117 | 117 | def reboot_instance(instance_id): |
118 | | - """ |
119 | | - Reboot an EC2 instance |
120 | | - """ |
121 | | - # Create an EC2 Client |
122 | | - ec2client = boto3.client('ec2', region_name=region) |
123 | | - try: |
124 | | - # Reboot an instance |
125 | | - response = ec2client.reboot_instances(InstanceIds=[instance_id], DryRun=False) |
126 | | - print(response) |
127 | | - print("\nSuccessfully rebooting instance: ", instance_id) |
128 | | - except botocore.exceptions.ClientError as e: |
129 | | - if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
130 | | - print("Error: Invalid instance id!!") |
131 | | - else: |
132 | | - raise |
133 | | - return |
| 118 | + """ |
| 119 | + Reboot an EC2 instance |
| 120 | + """ |
| 121 | + # Create an EC2 Client |
| 122 | + ec2_client = boto3.client('ec2', region_name=region) |
| 123 | + try: |
| 124 | + # Reboot an instance |
| 125 | + response = ec2_client.reboot_instances(InstanceIds=[instance_id], DryRun=False) |
| 126 | + print(response) |
| 127 | + print("\nSuccessfully rebooting instance: ", instance_id) |
| 128 | + except botocore.exceptions.ClientError as e: |
| 129 | + if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
| 130 | + print("Error: Invalid instance id!!") |
| 131 | + else: |
| 132 | + raise |
| 133 | + return |
134 | 134 |
|
135 | 135 |
|
136 | 136 | def terminate_instance(instance_id): |
137 | | - """ |
138 | | - Terminate an EC2 instance |
139 | | - """ |
140 | | - # Create an EC2 Client |
141 | | - ec2client = boto3.client('ec2', region_name=region) |
142 | | - try: |
143 | | - # Terminate an instance |
144 | | - response = ec2client.terminate_instances(InstanceIds=[instance_id], DryRun=False) |
145 | | - print(response) |
146 | | - print("\nSuccessfully terminating instance: ", instance_id) |
147 | | - except botocore.exceptions.ClientError as e: |
148 | | - if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
149 | | - print("Error: Invalid instance id!!") |
150 | | - else: |
151 | | - raise |
152 | | - return |
| 137 | + """ |
| 138 | + Terminate an EC2 instance |
| 139 | + """ |
| 140 | + # Create an EC2 Client |
| 141 | + ec2_client = boto3.client('ec2', region_name=region) |
| 142 | + try: |
| 143 | + # Terminate an instance |
| 144 | + response = ec2_client.terminate_instances(InstanceIds=[instance_id], DryRun=False) |
| 145 | + print(response) |
| 146 | + print("\nSuccessfully terminating instance: ", instance_id) |
| 147 | + except botocore.exceptions.ClientError as e: |
| 148 | + if e.response['Error']['Code'] == "InvalidInstanceID.Malformed": |
| 149 | + print("Error: Invalid instance id!!") |
| 150 | + else: |
| 151 | + raise |
| 152 | + return |
0 commit comments