-1

I am attempting to write a python code that would few of my manual steps in logging into the AWS platform.

In Ubuntu terminal , I used to write the command

ssh -A ec2-user@<ip-address>

and then again log into another instance using

ssh ec2-user@<ip.address>

Now I am looking at python code that would be able to automate this logging in process. I have written the following code till now.

import boto3
ec2 = boto3.resource('ec2')
Kshitij Marwah
  • 1,091
  • 3
  • 14
  • 23
  • 1
    boto3 is an SDK used for interfacing with the AWS API, not for performing actions in the terminal or on instances. Do you perhaps mean that you want to use boto3 to retrieve the IP addresses of EC2 instances based on some criteria? You would then need to use another library, perhaps [paramiko](http://www.paramiko.org/), to perform any sort of actions over SSH. – mfisherca Oct 18 '16 at 09:44
  • @Fishizzle - I would not need the IP address of the instance. I already have that. I just need to log into the instance and then perform some actions. – Kshitij Marwah Oct 18 '16 at 09:46
  • @KshitijMarwah You don't want to login, you want to perform actions. What actions? – S. de Melo Oct 18 '16 at 09:47
  • @S.deMelo - I am a little new to AWS so my terminologies might be a bit here and there. I would like to run a python script as a action. – Kshitij Marwah Oct 18 '16 at 09:51
  • 2
    @KshitijMarwah Does http://stackoverflow.com/a/15503965/6919239 help? – S. de Melo Oct 18 '16 at 09:54
  • 2
    @S.deMelo Heads up that the `cmdshell` function was removed from boto3. The answer you linked to is specific to boto 2. `cmdshell` was effectively just a wrapper around Paramiko anyway. – mfisherca Oct 19 '16 at 19:11
  • @Fishizzle OK, good to know! – S. de Melo Oct 20 '16 at 07:09

2 Answers2

2

There are 2 ways mostly to configure the boto3 library.

  1. You need to configure it first on your system and use the same configuration everywhere. You can use AWS CLI for this by running aws configure on your terminal.

  2. Set the environment variables and call the boto3 configuration via process.env.ENV_KEY and then use it like :

client = boto3.client( 'ec2', aws_access_key_id=process.env.ACCESS_KEY, aws_secret_access_key=process.env.SECRET_KEY, aws_session_token=process.env.SESSION_TOKEN, )

Ani
  • 710
  • 7
  • 19
  • Thanks for the answer but I already have 's3' configured. I would like to work on the ec2 instance of AWS. – Kshitij Marwah Oct 18 '16 at 09:47
  • That was an example. You can edit the resource to anything from ec2, s3, sqs, etc. Answer Edited. – Ani Oct 18 '16 at 09:49
1

If you want to perform actions on a running instance, boto3 is not what you're looking for. What you're asking about is more in the realm of what's called configuration management.

While you could write something yourself using an SSH library like Paramiko, you may want to look at a more purpose-built software package like Fabric. It's built on-top of the aforementioned Paramiko, with added functionality tailored to running commands on remote servers. For a more full-featured, open source configuration management solution, I recommend looking into Ansible.

AWS also has a native service for configuring EC2 instances called EC2 Run Command.

mfisherca
  • 2,399
  • 22
  • 22