Nisari Balakrishnan
Nisari Balakrishnan

Reputation: 106

Python Boto3: set session, create an EC2 instance and run command using csdshell

I need to write a python script using boto3 which does the following,

Upvotes: 1

Views: 3294

Answers (1)

Argais
Argais

Reputation: 243

Its not really difficult, what you are asking is mostly covered on boto3 docs.

For creating a new t2.micro on us-east-1a running ubuntu 14.04. You should be able to do it like this :

# latest ubuntu ami
ami_id = 'ami-5189a661'

# define userdata to be run at instance launch
userdata = """#cloud-config

runcmd:
 - touch /home/ubuntu/heythere.txt
"""

conn_args = {
    'aws_access_key_id': 'YOURKEY',
    'aws_secret_access_key': 'YOUSECACCESSKEY',
    'region_name': 'us-east-1'
}

ec2_res = boto3.resource('ec2', **conn_args)

new_instance = ec2_res.create_instances(
    ImageId=ami_id,
    MinCount=1,
    MaxCount=1,
    UserData=userdata,
    InstanceType='t2.micro'
    )

print new_instance.id

Upvotes: 5

Related Questions