A simple POC to demonstrate how to write, test locally, and deploy Python Lambda functions to AWS using Chalice.
Make sure you are using Python 2.7, Python 3.6, or Python 3.7 on your local machine. Also, make sure you have pip
installed. With that, use one of the following commands (depending on the version of Python installed) to install Chalice on your local machine:
pip install chalice
or
pip3 install chalice
cd
into the project directory and run the following command to start the local server for your Lambda function:
chalice local
If that command doesn't work or if Chalice fails to resolve localhost, try the following command:
chalice local --host 0.0.0.0
This command should work.
You need create a .aws
folder in your home directory and save your credentials in a file named config
inside this directory. You use the following commands to do that:
mkdir ~/.aws
cd ~/.aws && vim config
Add the following content to the file, replace the placeholders with your actual credentials, and save the file:
[default]
aws_access_key_id=<your_access_key_here>
aws_secret_access_key=<your_secret_key_here>
region=<your_region_here>
Once you have developed and written the function, make sure you have the following configuration in the .chalice/config.json
file:
{
"version": "2.0",
"app_name": "chalice-poc",
"stages": {
"dev": {
"api_gateway_stage": "api",
"manage_iam_role":false,
"iam_role_arn":"arn:aws:iam::xxxyyyzzz:role/abc",
"environment_variables": {
}
}
},
"app_name": "chalice-poc-dev"
}
Replace the placeholders with your actual values, of course. Once this is done, run the following command from the project root directory to deploy the Lambda function to AWS:
chalice deploy
For more information about how to work with Chalice, refer to the blog post here.