In general, AWS services can be accessed using
- AWS web interface,
- API libraries in a programming language, such as
boto3
for Python 3, - AWS command-line interface, i.e.
awscli
.
I opted for the API library since it is
- formally defined,
- programmable by definition,
- can be version controlled.
Assuming that awscli
is installed and properly configured with access keys and secrets,
we can run a very simple lambda function in hello.py
using scripts in the main.py
file.
The main.py
script is extremely minimal, and the source code demos how to interact with AWS
Lambda services.
For instance, let's start up an ipython
in the shell, and perform the followings.
In [1]: import main
...: main.setup_roles()
...: main.create_function()
...: main.invoke_function('Jeff', 'Bezos')
Out[1]: ...
To see the logs generated from the lambda function, we can do
In [2]: main.get_logs()
Out[2]: ...
If we changed the contents of hello.py
, we should update it on AWS Lambda by using
In [3]: main.update_function()
Out[3]: ...
If we changed the contents of our script main.py
, we can reload it.
In [4]: from importlib import reload
...: reload(main)
Out[4]:
To setup AWS accounts using IAM in general, follow Set Up an AWS Account - AWS Lambda. Alternatively, in ipython
,
In [1]: import boto3
...: iam = boto3.client('iam')
...: iam.create_user(UserName='user')
Out[1]: ...
To give it full permissions (not recommended),
In [2]: iam.attach_user_policy(UserName='user', PolicyArn='arn:aws:iam::aws:policy/AdministratorAccess')
Out[2]: ...
To create an access key,
In [3]: iam.create_access_key(UserName='user')
Out[3]: ...
We can now use the access key and secret to configure AWS command-line interface. To install and configure AWS command-line interface, follow these links,
- Installing the AWS Command Line Interface - AWS Command Line Interface
- Configuring the AWS CLI - AWS Command Line Interface
TL;DR, in the shell, e.g. bash,
pip3 install --upgrade awscli
aws configure
After properly configuring AWS CLI, the boto3
library can properly use AWS services in Python 3.
Just to note: you can simplify your code by using
... and avoid temp files.