Using Pythonista and Boto3

The Apple iPhone and iPad have this wonderful Python development environment called Pythonista. If you can import a module using the Pythonista StaSh extension, then you can get working with boto3 in Pythonista.

However, boto3 requires your AWS credentials to be configured either in a shared credentials file, in environment variables, or specified when the boto3 session, resource, or client is created.

Working with Pythonista requires some degree of creativity when dealing with issues like this one. However, there is no reason why you can’t get the same shared credentials file you use on other computing devices onto your iPad for use with Pythonista. Let’s look at how to do this.

StaSh is a bash-like implementation that can run within Pythonista. There have been many improvements since its first release. To get and install StaSh, launch Pythonista and in the interactive console, execute the command

import requests as r; exec(r.get('http://bit.ly/get-stash').text)

The command downloads StaSh allowing you to execute it like any other python tool. Once installed, find the launch_stash.py file and run it. This results in a console window where you can interact with the shell.

Once StaSh is running, you can expand the console view and display the StaSh window.

The StaSh Console (image by author)
The StaSh Console (image by author)

At this point, run pip install boto3, assuming Python3, which is then nicely installed into the correct location in the Pythonista installation. All we have to do now is set up the credentials.

Unfortunately, I haven’t been able to get the AWS CLI running within Pythonista, even though it is a Python application. That doesn’t mean all is lost, however. From within Pythonista, create a config file. Here is an example:

[default]
region = us-east-1
[preview]
sdb = true

These options set the default region AWS commands are executed against unless a different region is chosen. The [preview] section allows using the sdb service which is in preview mode in the CLI.

Next, we create the credentials file.

[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

If you want multiple profiles, you can also configure those following the AWS documentation.

These files will have some sort of extension, which was possibly added when you created them with your favorite editor. We will remove when we move the files to the location they need to go, which is ~/.aws.

Open the StaSh console, and execute the following commands:

[~/Documents]$ cd ..
[~]$ mkdir .aws
[~]$ cd .aws
[~/.aws]$ mv ../Documents/config.txt config
[~/.aws]$ mv ../Documents/credentials.py credentials
[~/.aws]$ ls -l
config (50.0B ) 2020-10-10 01:31:53
credentials (116.0B ) 2020-10-10 01:31:08
[~/.aws]$

We are set. Boto3 will now have access to the credentials we have just configured.

It is important to store the files in the ~/.aws directory in the StaSh environment. Otherwise, Pythonista won't be able to find it when your Python script is executed.

This view illustrates a piece of sample Python code and the Pythonista console output.

Output from an AWS Service (image by author)
Output from an AWS Service (image by author)

This code sample connects to the Amazon SimpleDB service and prints some information about the SimpleDB domain the code is evaluating.

Giving Pythonista the ability to execute the Python code you are working on to interface with AWS services makes it easier to write, test, and debug in the Pythonista interface. And since this isn’t something you are likely to do very often, you are likely to forget (which is why I wrote this article — I forgot and needed to update my secret key.)

Boto3 Credentials

Configuration and Credential file Settings

Shell Like an Expert in Pythonista

Chris is a highly-skilled Information Technology, AWS Cloud, Training and Security Professional bringing cloud, security, training, and process engineering leadership to simplify and deliver high-quality products. He is the co-author of seven books and author of more than 70 articles and book chapters in technical, management, and information security publications. His extensive technology, information security, and training experience make him a key resource who can help companies through technical challenges. Chris is a member of the AWS Community Builder Program.

This article is Copyright © 2020, Chris Hare.

Written by

Chris is the co-author of seven books and author of more than 70 articles and book chapters in technical, management, and information security publications.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store