Giter Site home page Giter Site logo

aws-lambda's Introduction

aws-lambda

Working with python

Your python code may or may not need additional packages/libraries to run. In order to deploy your code, You need to create a deployment package (a zip file with source code and required packages/libraries) which are not included in lambda runtime environment. These are some of the ways for creating your deployment package.

Deployment package with no dependency

To create the deployment package

  1. Open a command prompt and create a my-lambda-function project directory. For example, on macOS:
mkdir my-lambda-function
  1. Navigate to the my-lambda-function project directory.
cd my-lambda-function
  1. Copy the contents of the Python code from here and save it in a new file named lambda_function.py. Your directory structure should look like this:
my-lambda-function$
| lambda_function.py
  1. Add the lambda_function.py file to the root of the .zip file.
zip my-deployment-package.zip lambda_function.py

This generates a my-deployment-package.zip file in your project directory. The command produces the following output:

adding: lambda_function.py (deflated 50%)

Deployment package with dependency

To create the deployment package

  1. Open a command prompt and create a my-sourcecode project directory. For example, on macOS:
mkdir my-sourcecode
  1. Navigate to the my-sourcecode project directory.
cd my-sourcecode
  1. Copy the contents of the following sample Python code from here and save it in a new file named lambda_function.py:

Your directory structure should look like this:

my-sourcecode$
| lambda_function.py
  1. Install the requests library to a new package directory.
pip install --target ./package requests
  1. Create a deployment package with the installed library at the root.
cd package
zip -r ../my-deployment-package.zip .

This generates a my-deployment-package.zip file in your project directory. The command produces the following output:

  1. Add the lambda_function.py file to the root of the zip file.
cd ..
zip my-deployment-package.zip lambda_function

Deployment package while using virtualenv

  1. Activate the virtual environment. For example:
~/my-function$ source myvenv/bin/activate
  1. Install libraries with pip.
(myvenv) ~/my-function$ pip install requests
  1. Deactivate the virtual environment.
(myvenv) ~/my-function$ deactivate
  1. Create a deployment package with the installed libraries at the root.
~/my-function$cd myvenv/lib/python3.8/site-packages
zip -r ../../../../my-deployment-package.zip .

The last command saves the deployment package to the root of the my-function directory.

Tip:

A library may appear in site-packages or dist-packages and the first folder lib or lib64. You can use the pip show command to locate a specific package.

  1. Add function code files to the root of your deployment package.
~/my-function/myvenv/lib/python3.8/site-packages$ cd ../../../../
~/my-function$ zip -g my-deployment-package.zip lambda_function.py

After you complete this step, you should have the following directory structure:

my-deployment-package.zip$
  │ lambda_function.py
  │ __pycache__
  │ certifi/
  │ certifi-2020.6.20.dist-info/
  │ chardet/
  │ chardet-3.0.4.dist-info/
  ...

aws-lambda's People

Contributors

csemanish12 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.