Accessing AWS Services

In this blog, I will cover common methods of accessing AWS services using AWS console, AWS CLI and AWS Python SDK. The basic requirement is to have the AWS free account already created.

AWS console:

With AWS console, all activities could be done using the web interface. This approach becomes cumbersome when dealing with hundreds of VMs. Also, we cannot have a programmatic model with this approach.


AWS CLI for Windows or Linux can be downloaded from here. Using AWS CLI, all AWS services can be invoked using CLI.

Basic config steps:

  • After installing cli application, try “aws help” – If it works fine, it means its installed fine.
  • To allow programmatic access to aws services, access key is needed, goto security credentials-create access key and download locally and keep it safe.
  • aws configure (specify accesskeyid, accesskey, region, output format). For me, region was Oregon, but I had to specify us-west-2 which was the availability zone. possible output formats are text, json, table. table format is useful for simple reading, json is useful for programmatic scenarios where parsing is needed with programs like python. configure option creates a config file in .aws directory with the above details.

Common EC2 commands:

Creating instance:
aws ec2 run-instances --image-id ami-ccf297fc --count 1 --instance-type t1.micro --key-name KP2 --security-groups "ping, ssh, http"
login to instance:
ssh -i KP2.pem ec2-user@<ip>
Displaying instances:
aws describe-instances
Terminate instance:
aws ec2 terminate-instances --instance-ids <id>

Common S3 commands:

aws s3 ls - lists all buckets
aws s3 mb s3://<bucketname> - make bucket
aws s3 cp <filename> s3://<bucketname>/
- copies <filename> to <bucketname> . 
Each object has a url that can be accessed from web. Permissions must be set to allow others to view the url.

AWS SDK for Python:

Boto is the AWS SDK for Python and can be downloaded from here. AWS SDK is also available in other languages like Java, Python, PHP and Ruby. AWS SDK gives a programmatic approach to invoking AWS services.

First step is creating Boto credentials for accessing AWS. Create a .boto file in home directory with below contents.

aws_access_key_id = YOURACCESSKEY
aws_secret_access_key = YOURSECRETKEY

Following sample code creates a EC2 instance, sleeps for some time and then terminates the instance.

import boto.ec2
import time

# Connect to EC2, use environment variables for access key
conn = boto.ec2.connect_to_region("us-west-2")

print 'conn', conn

# Get current instances
reservations = conn.get_all_reservations()
print 'reservations', reservations

# Create micro instance with ami amazon linux
reservation = conn.run_instances('ami-ccf297fc',
                    security_groups=['ping, ssh, http'])

inst_reserve_id = reservation.instances[0].id
print 'instance_id', inst_reserve_id

#sleep for some time
print 'Sleeping for 60 seconds'

# Terminate instances
print 'Terminating instance ', inst_reserve_id

Following are some notes regarding above example:

  • We get connection pointer using access key and secret key stored in .boto file.
  • Next step is creating instance using run_instances call with specific AMI, security group, key and instance type.
  • run_instance call returns reservation object and we get the instance id from reservation object.
  • The instance id is used later to terminate the instance.

As part of Boto download, there is a sample Boto S3 script provided that creates a S3 instance, uploads a object for public access and then deletes the object. Following is the output when the sample is run.

Creating new bucket with name: python-sdk-sample-72fa1b1d-1dd5-45bf-96e5-5a27b69eaa3a
Uploading some data to python-sdk-sample-72fa1b1d-1dd5-45bf-96e5-5a27b69eaa3a with key: python_sample_key.txt
Generating a public URL for the object we just uploaded. This URL will be active for 1800 seconds

Press enter to delete both the object and the bucket...
Deleting the object.
Deleting the bucket.


One thought on “Accessing AWS Services

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s