-
Notifications
You must be signed in to change notification settings - Fork 56
/
awshelper.py
146 lines (130 loc) · 4.78 KB
/
awshelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
# -*- coding: utf-8 -*-
# awshelper.py
# It has methods for managing AWS EC2 instances.
# It uses Resource API (high-level) of Boto3.
import sys
import boto3
import botocore
region = 'eu-west-1' # AWS region
def describe_instances():
"""
Describes all EC2 instances associated with an AWS account
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
# Describe instances
instances = ec2_resource.instances.all()
for instance in instances:
print('State of the instance "' + instance.id + '" is: "' + instance.state['Name'] + '"')
return
def run_instance():
"""
Run an EC2 instance
"""
ami_id = "ami-785db401" # AMI Id
instance_type = "t2.micro" # Instance Type
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
# Run an instance
instances = ec2_resource.create_instances(ImageId=ami_id, InstanceType=instance_type,
MaxCount=1, MinCount=1)
Instance_id = instances[0].id
print('\nInstance Id: ' + Instance_id)
print('Image Id: ' + instances[0].image_id)
print('Instance Type: ' + instances[0].instance_type)
print('State: ' + instances[0].state['Name'])
return Instance_id
def describe_instance(instance_id):
"""
Describes an EC2 instance
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
try:
# Describe an instance
instance = ec2_resource.Instance(instance_id)
print('\nInstance Id: ' + instance_id)
print('Instance Id: ' + instance.id)
print('Image Id: ' + instance.image_id)
print('Instance Type: ' + instance.instance_type)
print('State: ' + instance.state['Name'])
if instance.state['Name'] == 'running':
print('Private DNS Name: ' + instance.private_dns_name)
print('Private IP: ' + instance.private_ip_address)
print('Public DNS Name: ' + instance.public_dns_name)
print('Public IP: ' + instance.public_ip_address)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "MissingParameter":
print("Error: Missing instance id!!")
else:
raise
return
def start_instance(instance_id):
"""
Start an EC2 instance
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
try:
# Start an instance
response = ec2_resource.Instance(instance_id).start(DryRun=False)
print(response)
print("\nSuccessfully starting instance: ", instance_id)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
print("Error: Invalid instance id!!")
else:
raise
return
def stop_instance(instance_id):
"""
Stop an EC2 instance
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
try:
# Stop an instance
response = ec2_resource.Instance(instance_id).stop(DryRun=False)
print(response)
print("\nSuccessfully stopping instance: ", instance_id)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
print("Error: Invalid instance id!!")
else:
raise
return
def reboot_instance(instance_id):
"""
Reboot an EC2 instance
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
try:
# Reboot an instance
response = ec2_resource.Instance(instance_id).reboot(DryRun=False)
print(response)
print("\nSuccessfully rebooting instance: ", instance_id)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
print("Error: Invalid instance id!!")
else:
raise
return
def terminate_instance(instance_id):
"""
Terminate an EC2 instance
"""
# Instantiate the service resource object
ec2_resource = boto3.resource('ec2', region_name=region)
try:
# Terminate an instance
response = ec2_resource.Instance(instance_id).terminate(DryRun=False)
print(response)
print("\nSuccessfully terminating instance: ", instance_id)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "InvalidInstanceID.Malformed":
print("Error: Invalid instance id!!")
else:
raise
return