-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathec2.boto.py
More file actions
35 lines (27 loc) · 912 Bytes
/
ec2.boto.py
File metadata and controls
35 lines (27 loc) · 912 Bytes
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
import sys, time
try:
import boto3
except ModuleNotFoundError:
print("Boto3 module is not installed. Please try boto3 using pip.")
sys.exit(1)
except Exception as e:
print(e)
#There are 2 methods to start boto3 code.
#1. With Session Object.
#2. Without Session Object.
console = boto3.session.Session(aws_access_key_id="xxxx", aws_secret_access_key="xxxx", region_name="ap-south-1")
print(console)
aws_resource_obj=console.resource(service_name="ec2")
aws_client_obj=console.client(service_name="ec2")
#####################1. With Session Object.
instance = aws_resource_obj.Instance('i-013a70797a0dc8465')
# print(dir(instance))
# print(instance.state)
instance.stop()
while True:
instance = aws_resource_obj.Instance('i-013a70797a0dc8465')
print(instance.state['Name'])
if instance.state['Name']=='stopped':
break
time.sleep(5)
print("ec2 instance is stopped")