Skip to content

Instantly share code, notes, and snippets.

@dimzak
Created July 18, 2018 07:31
Show Gist options
  • Save dimzak/74183ed7c3b1e84eb9ab219eae72bd0c to your computer and use it in GitHub Desktop.
Save dimzak/74183ed7c3b1e84eb9ab219eae72bd0c to your computer and use it in GitHub Desktop.
Boto3 helpers
#!/usr/bin/env python
import sys
import boto3
s3 = boto3.client("s3")
client = boto3.client(
's3',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN,
)
bucket_name = sys.argv[1]
file_path = sys.argv[2]
try:
response = s3.Object(file_path, file).put(Body=open(file_path, 'rb'))
print response
except Exception as e:
print 'Error'
print str(e)
#!/usr/bin/env python
import boto3
try:
ec2 = boto3.client('ec2')
instance = ec2.create_instances(
ImageId='ami-1e299d7e',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro'
)
# wait to start
instance.wait_until_running()
print instance[0].id
id = instance[0].id
# or use with ssh_ec2_using_name.py
instance = ec2.Instance(id)
response = instance.terminate()
print response
except Exception as e:
print 'Error'
print str(e)
#!/usr/bin/env python
import boto3
import paramiko
import sys
print 'Running command in ' + str(sys.argv[1])
try:
ec2 = boto3.client('ec2')
response = ec2.describe_instances(Filters=[
{
'Name': 'tag:Name',
'Values': [
sys.argv[1],
]
},
])
for reservation in response['Reservations']:
dns = reservation['Instances'][0]['PublicDnsName']
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(dns, username='ubuntu')
stdin, stdout, stderr = client.exec_command('uname -a')
print stdout.readlines()
client.close()
except Exception as e:
print 'Error'
print str(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment