import boto3
session = boto3.Session(profile_name='slscode')
ec2 = session.resource("ec2")
instances = ec2.instances.filter(Filters=[
{
'Name': 'tag-key',
'Values': ["Name"],
},
{
'Name': 'tag-value',
'Values': ["my-misbehaving-instance"],
},
{
'Name': 'instance-state-name',
'Values': ["running"],
},
])
if len([i for i in instances]) == 0:
print("not in `running` state")
else:
server = [i for i in instances][-1]
print("Found server %s" % server.instance_id)
server.reboot()
print("Rebooted server %s" % server.instance_id)
# subnet subnet-4b36143d
# sg sg-9c50c2e5 named network-WebServerSecurityGroup-16RHZWLDGGOGI
# Amazon Linux AMI ami-643b1972
instance = ec2.create_instances(
ImageId='ami-a07379d9',
InstanceType='t2.micro',
KeyName='hornet-slscode',
SecurityGroupIds=[
'sg-9c50c2e5'
],
SubnetId='subnet-4b36143d',
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Value': 'my-misbehaving-instance',
'Key': 'Name'
},
{
'Value': 'boto3+ipython',
'Key': 'tool'
}
]
}
],
MinCount=1,
MaxCount=1
)
instance