Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate the Configuring the AWS Command Line Interface and setting up profile process in python?

I want to automate the Configuring the AWS Command Line Interface shown at this page in python. I want to do so so that i don't have to manually type in every step (like the aws access key id,secret access key,region and output format) as I have a large number of servers that needs this step.

Is there way that I can code this in a python script and when I run that python on individual servers it will do this profile setup? With this I will only have to run a script in each server and not run run every individual step.

like image 605
user2966197 Avatar asked Mar 31 '16 17:03

user2966197


People also ask

Which command is used to configure AWS CLI?

Quick configuration with aws configure For general use, the aws configure command is the fastest way to set up your AWS CLI installation. When you enter this command, the AWS CLI prompts you for four pieces of information: Access key ID. Secret access key.

How do I create an aws profile?

Choose Users from the navigation bar and then choose your AWS user name (not the check box). Choose the Security credentials tab, and then choose Create access key. If you already have an access key but you can't access your secret key, make the old key inactive and create a new one.

What is the command aws configure -- profile used for?

aws configure setSpecify the profile that you want to view or modify with the --profile setting. For example, the following command sets the region in the profile named integ . To remove a setting, use an empty string as the value, or manually delete the setting in your config and credentials files in a text editor.

What four pieces of information is required to use the aws configure command to set up your AWS CLI installation?

When you type this command, the AWS CLI prompts you for four pieces of information: Access key, secret access key, AWS Region, and output format.

How do I set a default AWS CLI profile?

In order to set the name for the default AWS CLI profile, set the AWS_PROFILE environment variable to the name of the profile stored in your credentials and config files, e.g. admin for a named profile, or default for the default profile. Copied!


2 Answers

I suggest using Ansible and copy the config files to servers. Easiest and error free. If Ansible is ruled out, call aws configure set

If you want to create the config file, execute the following in subprocess:

aws configure set aws_access_key_id xxxxxxxxx
aws configure set aws_secret_access_key xxxxxxxxx
....

If you want to set it in the Python script only, set the environment variable in the script:

import os
os.environ['aws_access_key_id'] = 'xxxxxxxxx'
like image 157
helloV Avatar answered Oct 17 '22 02:10

helloV


helloV is helpful, should be upvoted.

Here's my re-working of it ( you may or may not need the sudo in last command )

( note this is bash, not python - many ways to pythonise the general idea :-) )

aws configure set default.region us-east-1
aws configure set aws_access_key_id 'YOUR_ACCESS_KEY'
aws configure set aws_secret_access_key 'YOUR_SECRET_KEY'
aws ecr get-login | sudo sh

And here as a one liner .

aws configure set default.region us-east-1; aws configure set aws_access_key_id 'YOURSACCESSKEY' ; aws configure set aws_secret_access_key 'YOUR_SECRET_KEY'; aws ecr get-login | sudo sh
like image 26
Simon Clewer Avatar answered Oct 17 '22 00:10

Simon Clewer