Introduction
Creating functions are easy with AWS Console. Still, it's worth knowing the cli(command line interface) way. So, it can be automated later on.
In this tutorial, I will show you how to create a lambda function with aws-cli.
Create
aws lambda create-function \
--function-name my-lambda-function-name \
--runtime python3.6 \
--role arn:aws:iam::123456789012:role/service-role/MyTestFunction-role-tges6bf \
--handler my_pythonfile_without_py.my_handler_func \
--zip-file fileb://my-function.zip
Here keyword names,
function-name
: is the name of lambda function, you can type what ever you want (with some exceptions).
runtime
: I have given python3.6
, many of them are available. You can check this from aws console as well.
role
: this can be copied from AWS IAM page.
handler
: is the method that runs on lambda function invocation. It's usually, named like file_name_without_py.handler_function_name.
zip-file
: This zip file contains code and any related packages. I will explain, how I created in next section.
Docs
https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
https://docs.aws.amazon.com/cli/latest/reference/lambda/create-function.html
Create Package
For simple functions
Simple functions without any python package dependencies can be done in two steps
zip function.zip lambda_function.py
aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip
For functions with dependency
It can be done in few commands
# Create virtualenv
python3 -m venv v-env
# Activate that venv
source v-env/bin/activate
# Install all required packages (If you are python developer, this will make sense, otherwise, you may need to watch a tutorial on pip).
pip install Pillow
pip install -r requirements.txt
#etc.
deactivate
# go to site-packages
cd v-env/lib/python3.8/site-packages
# zip content with high-compression ratio (this 9 indicates that)
# OLDPWD is directory before our last cd
zip -r9 ${OLDPWD}/function.zip .
This part is well documented here. https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
Conclusion
I don't think, this tutorial is standalone one. But, with reference, you will get a basic idea.
Top comments (0)