Skip to content

Instantly share code, notes, and snippets.

@markwk
Created November 17, 2018 06:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markwk/38d269dae2db20182b9d51e87cf91ed0 to your computer and use it in GitHub Desktop.
Save markwk/38d269dae2db20182b9d51e87cf91ed0 to your computer and use it in GitHub Desktop.
Reboot an ec2 instance using python and boto3
# -*- coding: utf-8 -*-
##############################################
# Reboot an ec2 instance using python and boto3
#
# Boto3 Documenation Reference:
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.reboot_instances
##############################################
import boto3
s3 = boto3.client(
'ec2',
# best stored keys in environmental variables
aws_access_key_id=S3_KEY,
aws_secret_access_key=S3_SECRET_ACCESS_KEY,
)
my_ec2_instance_id = '' # add ec2 instance id but also best stored in env
def reboot_ec2(ec2_instance_id):
response = client.reboot_instances(
InstanceIds=[
ec2_instance_id,
],
DryRun=True # change to fals to usd
)
return response
reboot_ec2(my_ec2_instance_id)
# RELATED: AWS - Using CloudWatch to trigger script on ec2 instance
# SEE: https://stackoverflow.com/a/41365623/609617
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment