Installing Configuring and Using AWS CLI and API’s

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Working with Amazon EC2 instances by using the CLI/API

I had a need to administrate AWS EC2 instances with the API using the CLI tool set or Python.
To administrate AWS EC2 instances there are many options, some of them are listed below.

  • Using the AWS EC2 console is of course an option
  • Using the AWS CLI
  • Using the AWS API Python module(s)

Below I will be discussing option 2 – Using the AWS CLI, the steps needed to install configure and use the CLI.

Installing AWS-cli

The below commends will download and install the aws-cli.

Note: Make sure to add /usr/local/bin to your PATH to use aws command directly.

Configuring AWS – Adding AWS Access Key

Note: While there are many things you can do with the aws cli. I would advice you to first add/configure your aws access keys (it would make life easier in the long 🙂 ).

To configure AWS remote keys just run aws configure an example is below.

To get a Secret Access Key, login to your AWS EC2 console and go to Security > IAM.
Create a Policy (or use a per-defined)
Create a new User and use the new Access Key and Secret Access Key, optional download and save the key in a safe place.
Attach the policy to the new user, to be able to access remotely.
Note: Once the you close the window, you wont be able to retrieve the Secret Access Key you can of course re-generate the key which might not always be the best option.

Attaching Policy to the new user

To be able to use the new user, you need to apply(attach) rights (i.e policy) to new user.
An example Policy is below, just copy and paste in AWS, or use one of the existing policy’s.

Accessing instances with the aws cli

The simplest form might be to use the describe-instances, an example output is below

Anther example can be monitoring remote instances.
If AWS cloudwatch is disabled and not configured, you would get the below output.

Administrating AWS EC2 by using the Python API’s

First we need to install the AWS modules.
Note: The AWS module is installed using pip, make sure to have pip installed before installing.
Installing AWS Python modules.

Next, lets create an /etc/boto.cfg (or ~/.boto), with the aws access key

Now, we can list all running AWS instances. run the below.

Sample output is below.

The above example is just a small sample of what can be done with aws api. almost the full ec2 manamgment is possible with the api’s.

Next will explorer how to install and use azure’s API.

References

Installing AWS python module
AWS Python Developer Center

Boto Reference

Boto documentation
Boto on github
Boto cheat sheet

Leave a Reply

avatar
300
  Subscribe  
Notify of