Urban

Setting up the AWS CLI with pyenv

aws cli python3 macos pip3 pyenv

I wrote an earlier post on setting up the AWS CLI on macOS. After almost a year of learning AWS and Python, I discovered there is a much better way. The main driver here is avoiding impact to the Apple Python installation.

Use pyenv for Python Version Management

There are many versions of Python. Different providers, such as AWS, support specific versions or use specific versions in their tooling. Also, some Python packages only work on certain versions.

pyenv helps us manage those versions in isolation from the global installation. I suggest installing Homebrew and then following the pyevn Homebrew installation steps. Be sure to set up the suggested build environment.

From the AWS docs, Python 2 version 2.6.5+ or Python 3 version 3.3+ is required

Python Virtual Environments

For similar reasons mentioned above, we need to be able to create Python environments that mimic our deployment environments. We may also want to tinker and be able to blow away an environment. We use virtual environments for this.

I favor pyenv-virtualenv. Again, follow the Homebrew steps. Once installed, follow usage steps.

The activate virtualenv section has a useful tidbit. If you add eval "$(pyenv virtualenv-init -)" to your shell, you can cd into your project directory and run pyenv local <project name>. Now, when you cd into the project directory, you’ll be in your virtual environment!

Install the CLI

This part is simple. cd into your project directory, assuming you have auto activation setup, run pip install --upgrade awscli. Pip was installed for us by pyenv-virtualenv so we’re good to go.

You can now work through some of the configuration steps.

Clean-up

Part of the beauty here is we can easily blow away what we’ve done. Delete the .python-version file in your project directory then run pyenv uninstall <virtual environment name>. And if you don’t want the Python version anymore you can run pyenv uninstall <python version>. Then run pyenv versions to confirm.

Author

Next post

Identities and Keys and Tokens O MY!

One of the most important pieces of any app is authentication and authorization for end users. A post by Dan Bellinski details the complexity of digital security. If you’re new to the auth world like I am, you can get lost in just the terminology. In this post I’ll explain how you can utilize and integrate with AWS Cognito in your serverless app.

Read More →