Skip to content

Instantly share code, notes, and snippets.

@ljmocic
Last active April 11, 2020 11:05
Show Gist options
  • Save ljmocic/d6e84677488ca0e3b0750cd4a359a9b6 to your computer and use it in GitHub Desktop.
Save ljmocic/d6e84677488ca0e3b0750cd4a359a9b6 to your computer and use it in GitHub Desktop.
# Read more about setting it up
# https://medium.com/@ljmocic/running-a-script-from-s3-on-ec2-with-7-lines-of-code-59806c07d300
import boto3
REGION = 'us-east-1'
AMI = 'ami-00b94673edfccb7ca'
INSTANCE_TYPE = 'm3.medium'
BUCKET = 'script-bucket'
SCRIPT_PATH = 'script.sh'
EC2 = boto3.client('ec2', region_name=REGION)
S3 = boto3.client('s3', region_name=REGION)
S3.download_file(BUCKET, SCRIPT_PATH, SCRIPT_PATH)
with open(SCRIPT_PATH, 'r') as f:
script = '\n'.join(f)
EC2.run_instances(
ImageId=AMI,
InstanceType=INSTANCE_TYPE,
MinCount=1,
MaxCount=1,
InstanceInitiatedShutdownBehavior='terminate',
UserData=script
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment