当前位置: 首页 > 软件库 > 云计算 > Serverless 系统 >

python-lambda

授权协议 ISC License
开发语言 JavaScript
所属分类 云计算、 Serverless 系统
软件类型 开源软件
地区 不详
投 递 者 宦源
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Python-lambda is a toolset for developing and deploying serverless Python code in AWS Lambda.

A call for contributors

With python-lambda and pytube both continuing to gain momentum, I'm calling forcontributors to help build out new features, review pull requests, fix bugs,and maintain overall code quality. If you're interested, please email me atnficano[at]gmail.com.

Description

AWS Lambda is a service that allows you to write Python, Java, or Node.js codethat gets executed in response to events like http requests or files uploadedto S3.

Working with Lambda is relatively easy, but the process of bundling anddeploying your code is not as simple as it could be.

The Python-Lambda library takes away the guess work of developing yourPython-Lambda services by providing you a toolset to streamline the annoyingparts.

Requirements

  • Python 2.7, >= 3.6 (At the time of writing this, these are the Python runtimes supported by AWS Lambda).
  • Pip (~8.1.1)
  • Virtualenv (~15.0.0)
  • Virtualenvwrapper (~4.7.1)

Getting Started

First, you must create an IAM Role on your AWS account calledlambda_basic_execution with the LambdaBasicExecution policy attached.

On your computer, create a new virtualenv and project folder.

$ mkvirtualenv pylambda
(pylambda) $ mkdir pylambda

Next, download Python-Lambda using pip via pypi.

(pylambda) $ pip install python-lambda

From your pylambda directory, run the following to bootstrap your project.

(pylambda) $ lambda init

This will create the following files: event.json, __init__.py,service.py, and config.yaml.

Let's begin by opening config.yaml in the text editor of your choice. Forthe purpose of this tutorial, the only required information isaws_access_key_id and aws_secret_access_key. You can find these bylogging into the AWS management console.

Next let's open service.py, in here you'll find the following function:

def handler(event, context):
    # Your code goes here!
    e = event.get('e')
    pi = event.get('pi')
    return e + pi

This is the handler function; this is the function AWS Lambda will invoke inresponse to an event. You will notice that in the sample code e and piare values in a dict. AWS Lambda uses the event parameter to pass inevent data to the handler.

So if, for example, your function is responding to an http request, eventwill be the POST JSON data and if your function returns something, thecontents will be in your http response payload.

Next let's open the event.json file:

{
  "pi": 3.14,
  "e": 2.718
}

Here you'll find the values of e and pi that are being referenced inthe sample code.

If you now try and run:

(pylambda) $ lambda invoke -v

You will get:

# 5.858
# execution time: 0.00000310s
# function execution timeout: 15s

As you probably put together, the lambda invoke command grabs the valuesstored in the event.json file and passes them to your function.

The event.json file should help you develop your Lambda service locally.You can specify an alternate event.json file by passing the--event-file=<filename>.json argument to lambda invoke.

When you're ready to deploy your code to Lambda simply run:

(pylambda) $ lambda deploy

The deploy script will evaluate your virtualenv and identify your projectdependencies. It will package these up along with your handler function to azip file that it then uploads to AWS Lambda.

You can now log into theAWS Lambda management console toverify the code deployed successfully.

Wiring to an API endpoint

If you're looking to develop a simple microservice you can easily wire yourfunction up to an http endpoint.

Begin by navigating to your AWS Lambda management console andclicking on your function. Click the API Endpoints tab and click "Add API endpoint".

Under API endpoint type select "API Gateway".

Next change Method to POST and Security to "Open" and click submit (NOTE:you should secure this for use in production, open security is used for demopurposes).

At last you need to change the return value of the function to comply with thestandard defined for the API Gateway endpoint, the function should now looklike this:

def handler(event, context):
    # Your code goes here!
    e = event.get('e')
    pi = event.get('pi')
    return {
        "statusCode": 200,
        "headers": { "Content-Type": "application/json"},
        "body": e + pi
    }

Now try and run:

$ curl --header "Content-Type:application/json" \
       --request POST \
       --data '{"pi": 3.14, "e": 2.718}' \
       https://<API endpoint URL>
# 5.8580000000000005

Environment Variables

Lambda functions support environment variables. In order to set environmentvariables for your deployed code to use, you can configure them inconfig.yaml. To load the value for the environment variable at the time ofdeployment (instead of hard coding them in your configuration file), you canuse local environment values (see 'env3' in example code below).

environment_variables:
  env1: foo
  env2: baz
  env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}

This would create environment variables in the lambda instance upon deploy. Ifyour functions don't need environment variables, simply leave this section outof your config.

Uploading to S3

You may find that you do not need the toolkit to fullydeploy your Lambda or that your code bundle is too large to upload via the API.You can use the upload command to send the bundle to an S3 bucket of yourchoosing. Before doing this, you will need to set the following variables inconfig.yaml:

role: basic_s3_upload
bucket_name: 'example-bucket'
s3_key_prefix: 'path/to/file/'

Your role must have s3:PutObject permission on the bucket/key that youspecify for the upload to work properly. Once you have that set, you canexecute lambda upload to initiate the transfer.

Deploying via S3

You can also choose to use S3 as your source for Lambda deployments. This canbe done by issuing lambda deploy-s3 with the same variables/AWS permissionsyou'd set for executing the upload command.

Development

Development of "python-lambda" is facilitated exclusively on GitHub.Contributions in the form of patches, tests and feature creation and/orrequests are very welcome and highly encouraged. Please open an issue if thistool does not function as you'd expect.

Environment Setup

  1. Install pipenv
  2. Install direnv
  3. Install Precommit (optional but preferred)
  4. cd into the project and enter "direnv allow" when prompted. This will begininstalling all the development dependancies.
  5. If you installed pre-commit, run pre-commit install inside the projectdirectory to setup the githooks.

Releasing to Pypi

Once you pushed your chances to master, run one of the following:

# If you're installing a major release:
make deploy-major

# If you're installing a minor release:
make deploy-minor

# If you're installing a patch release:
make deploy-patch
  • lambda是Python编程语言中使用频率较高的一个关键字。那么,什么是lambda?它有哪些用法?网上的文章汗牛充栋,可是把这个讲透的文章却不多。这里,我们通过阅读各方资料,总结了关于Python中的lambda的“一个语法,三个特性,四个用法,一个争论”。欢迎阅读和沟通(个人微信: slxiaozju)。 由于文章是从我的公众号上复制过来的,因此排版不整齐,但是内容绝对充实,欢迎关注公众号[

  • 一、lambda定义与用法 lambda表达式是一行的函数。它们在其他语言中也被称为匿名函数。即,函数没有具体的名称,而用def创建的方法是有名称的。如果你不想在程序中对一个函数使用两次,你也许会想用lambda表达式,它们和普通的函数完全一样。而且当使用函数作为参数的时候,lambda表达式非常有用,可以让代码简单,简洁。 lambda表达式返回的是function类型,说明是一个函数类型。 "

  • lambda函数也就是匿名函数,在python 编程中,习惯将其称为表达式;         快速实现某项功能,不用像def定义函数一样,需要再去想一个函数名字; def 与 lambda 实现相同功能:         def函数写法:(关于map的用法,参考博客:python -- map(), numpy -- flatten()_无脑敲代码,bug漫天飞的博客-CSDN博客) >>> de

  • 一、介绍 lambda表达式可以用来声明匿名函数,也可以定义有具体名字的函数。 lambda表达式中不允许包含复合语句,即while循环、if判断这些,但可以调用其他的函数。 表达式通常只有一行,符号:前是对应的参数,符号:后面是对应的函数体。 在函数体中不能进行赋值 使用def可以用来定义具名函数。 二、使用 如果需要传入两个参数,进行加法运算时 [1]. 匿名函数 在使用时,lambda x,

  • lambda的由来 python 定义函数时主要有两个关键字,一个是非常常用的def,另一个就是 今天的主人公 lambda lambda 是一种生成函数对象的表达式形式。 由于它与Lisp语言中的一个工具很相似所以称为lambda (lambda 源于Lisp语言的名称,得名于lambda演算,是一种符号化逻辑。但在python语法中将其引入这类表达式的一个关键字,而不是复杂的数学遗留问题,所以

 相关资料
  • 问题内容: 考虑以下代码片段: 我期望创建一个具有以下两个功能的字典: 但看起来生成的两个函数完全相同: 我真的不明白为什么。你有什么建议吗 ? 问题答案: 你需要为创建的每个函数绑定。一种方法是将其作为具有默认值的参数传递: 现在,函数内部的使用了参数,即使它具有相同的名称,并且在创建函数时会评估该参数的默认值。为了帮助你看到以下内容: 请记住默认值是如何工作的,例如可变对象(如列表和字典)的工

  • 本文向大家介绍Python lambda和Python def区别分析,包括了Python lambda和Python def区别分析的使用技巧和注意事项,需要的朋友参考一下 Python支持一种有趣的语法,它允许你快速定义单行的最小函数。这些叫做lambda的函数,是从Lisp借用来的,可以用在任何需要函数的地方。 lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambd

  • 问题内容: 在其他人的代码中,我阅读了以下两行: 由于defaultdict的参数是默认工厂,因此我认为第一行表示当我为不存在的键k调用x [k](例如类似v = x [k]的语句)时,键值对(k ,0)会自动添加到字典中,就像首先执行语句x [k] = 0一样。我对么? 那y呢?似乎默认工厂将创建一个默认值为0的defaultdict。但这具体意味着什么?我试图在Python shell中尝试使

  • python-lambda-layer-builder Creates an AWS Lambda Layers structure that is optimized for: Lambda Layer directory structure, compiled library compatibility, and minimal file size. This repo was created

  • 我有一个数据框我想选择列A的值在[2,3]中的行 为此,我编写了一个简单的for循环: 有没有任何内置函数可以代替使用for循环来实现这一点?

  • Python Python 诞生之初就被誉为最容易上手的编程语言。进入火热的 AI 人工智能时代后,它也逐渐取代 Java,成为编程界的头牌语言。 Python 是一门新手友好、功能强大、高效灵活的编程语言,学会之后无论是想进入数据分析、人工智能、网站开发这些领域,还是希望掌握第一门编程语言,都可以用 Python 来开启无限未来的无限可能! 语言排行榜 编程之旅 Python 适合谁来学习? 想