DEV Community


Posted on

Create lambda function with aws cli


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.


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://
Enter fullscreen mode Exit fullscreen mode

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.


Create Package

For simple functions

Simple functions without any python package dependencies can be done in two steps

aws lambda update-function-code --function-name my-function --zip-file fileb://
Enter fullscreen mode Exit fullscreen mode

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

# 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}/ .
Enter fullscreen mode Exit fullscreen mode

This part is well documented here.


I don't think, this tutorial is standalone one. But, with reference, you will get a basic idea.

Top comments (0)