0

I am trying to execute this command programmatically with python 2.7:

aws configure --profile name_profile

The thing is that this command as soon as you executed it, it ask the user to input some parameters:

$ aws configure
AWS Access Key ID [****]: access_key_input
AWS Secret Access Key [****]: secret__key_input
Default region name [us-west-1]: region_optional_input

I know how to execute commands in python, usually with these lines you are done:

def execute_command(command):
    try:
        process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        output, error = process.communicate()
        return output, error
    except Exception as error:
        return None, error

But in this case, I don't know how to simulate this process. I want to write something like

def add_profile(access_key, access_secret, name_profile, region=None)

And that should execute the command of before and answer the questions with the parameters.

Thanks in advance to all.

  • So you want to ask the user for input, then use that to configure your aws connection? – Mason Caiby Sep 06 '19 at 21:43
  • @MasonCaiby no, this command is needed to automate some other things, the idea is that the user doesn't have to input anything, I will get all the data from the database and this process is gonna be executed in the backend without any frontend implication. – Roberto Fernandez Diaz Sep 06 '19 at 21:46
  • Are you trying to make a lot of aws configs? The issue is that you are trying to use a command line tool from a python script... Why do you want to make so many profiles? can you use `stdout` to send the `access_key` and a `\n` to the cli tool? – Mason Caiby Sep 06 '19 at 21:50

2 Answers2

1

This question has more to do with the AWS CLI than with Python. The easy, non-interactive way to configure you AWS credentials is to use the AWS shared credentials file.

Instead of having to interact with the command line, you could have your function create such a shared credentials file, making sure to also clean up or delete the file after your done with it.

If you're adamant on using Python to interact with the AWS CLI, there a couple of approaches. From a quick Google search, I've found this SO post. From my own experiences of interacting with the command line programatically with Python, I've used Pexpect. This library basically lets you expect that certain text will appear in the CLI, after which your program can output new data to the CLI.

natn2323
  • 1,983
  • 1
  • 13
  • 30
1

Even though the use of Pexpect (thanks @natn2323) solve the issue, I found out that if you don't need to check for dynamic questions and the amount and type of questions are always the same. This will work also:

try:
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    output, error = process.communicate(input=b"answer1\nanswer2\n\n")
    return output, error
except Exception as error:
    return None, error

Notice that the last 2 answers (the last part of the input "\n\n") are empty for my case (aws configure).

I have some errors with Pycharm and the use of Pexpect so at the end I will use this solution.