DEV Community

Ajeeb.K.P
Ajeeb.K.P

Posted on

4 1

Create lambda function with aws cli

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
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.

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

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.

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more