-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_all_instances.py
More file actions
44 lines (32 loc) · 1.03 KB
/
get_all_instances.py
File metadata and controls
44 lines (32 loc) · 1.03 KB
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
import boto3
ec2 = boto3.resource('ec2')
#Create instances
ec2.create_instances(
DryRun=False,
ImageId='ami-0044b96f' ,
MinCount=1, MaxCount=2,
KeyName='gig3vpc',
InstanceType='t2.micro',
SubnetId='subnet-2eda2954',
SecurityGroupIds=['sg-4ae39a22',]
)
for i in ec2.instances.all(): print (i)
instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in instances:
print(instance.id, instance.instance_type)
for i in ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['stopped']}]):
print (i)
for instance in ec2.instances.all():
print (instance.id, instance.state)
instance.start(instance.id)
#add tags
mytags = [{
"Key" : "Name",
"Value" : "GIG 3"
}]
for i in ec2.security_groups.all(): id.append(i.id)
for id_sg in id:
ec2.create_tags(Resources = [id_sg],Tags= mytags)
#Terminate all instances
#for i in ec2.instances.all(): ec2.instances.terminate(i.instance_id)