From a088f25db61e93864689a3e0aea2e2eb0d3c7a02 Mon Sep 17 00:00:00 2001 From: Jonathan Mettes Date: Wed, 24 Feb 2016 23:52:36 +1100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ ec2.py | 10 ++++++++++ ec2_extra.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 .gitignore create mode 100755 ec2.py create mode 100755 ec2_extra.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8a0d25e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pem +.idea/ diff --git a/ec2.py b/ec2.py new file mode 100755 index 0000000..7b38a66 --- /dev/null +++ b/ec2.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import boto3 + +ec2 = boto3.resource('ec2') + +# launch an instance +instances = ec2.create_instances(ImageId='ami-11032472', + InstanceType='t2.micro', + MinCount=1, + MaxCount=1) diff --git a/ec2_extra.py b/ec2_extra.py new file mode 100755 index 0000000..63cf745 --- /dev/null +++ b/ec2_extra.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import boto3 +import botocore.exceptions + +ec2 = boto3.resource('ec2') + +# allow SSH access +try: + security_group = ec2.create_security_group(GroupName='ssh_access', + Description='pug talk') + security_group.authorize_ingress(IpProtocol='tcp', + CidrIp='0.0.0.0/0', + FromPort=22, + ToPort=22) +except botocore.exceptions.ClientError: + pass + + +# create SSH key +try: + key = ec2.create_key_pair(KeyName='cpug') + with open('cpug.pem', 'w') as file: + file.write(key.key_material) +except botocore.exceptions.ClientError: + pass + + +# launch an instance +instances = ec2.create_instances(ImageId='ami-11032472', + InstanceType='t2.micro', + MinCount=1, + MaxCount=1, + SecurityGroupIds=['ssh_access'], # <-- security group + KeyName='cpug') # <-- ssh key + + +print('launching') + +instances[0].wait_until_running() + +print('running') +print('might need to run: chmod 400 cpug.pem') +print('type: ssh -i cpug.pem ec2-user@' + ec2.Instance(instances[0].id).public_ip_address)