aws lambda_Python和AWS Lambda –天生一对

aws lambda

In recent months, I’ve begun moving some of my analytics functions to the cloud. Specifically, I’ve been moving them many of my python scripts and API’s to AWS’ Lambda platform using the Zappa framework.  In this post, I’ll share some basic information about Python and AWS Lambda…hopefully it will get everyone out there thinking about new ways to use platforms like Lambda.

最近几个月,我开始将一些分析功能移至云中。 具体来说,我已经使用Zappa框架将它们的许多python脚本和API移至AWS的Lambda平台 在本文中,我将分享一些有关Python和AWS Lambda的基本信息……希望它能使所有人都开始思考使用Lambda等平台的新方法。

Before we dive into an example of what I’m moving to Lambda, let’s spend some time talking about Lambda. When I first heard about, I was a confused…but once I ‘got’ it, I saw the value. Here’s the description of Lambda from AWS’ website:

在深入探讨我要迁移到Lambda的示例之前,让我们花一些时间讨论Lambda。 当我第一次听说时,我很困惑……但是一旦我“明白了”,我就看到了价值。 这是AWS网站上对Lambda的描述:

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume – there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

AWS Lambda允许您运行代码而无需置备或管理服务器。 您只需为您消耗的计算时间付费-代码未运行时不收费。 借助Lambda,您几乎可以为任何类型的应用程序或后端服务运行代码-只需进行零管理即可。 只需上传您的代码,Lambda便可以处理以高可用性运行和扩展代码所需的一切。 您可以将代码设置为自动从其他AWS服务触发,或直接从任何Web或移动应用程序调用它。

Once I realized how easy it is to move code to lambda to use whenever/wherever I needed it, I jumped at the opportunity.  But…it took a while to get a good workflow in place to simplify deploying to lambda. I stumbled across Zappa and couldn’t be happier…it makes deploying to lambda simple (very simple).

一旦我意识到将代码移动到lambda以便随时随地使用它是多么容易,我便抓住了机会。 但是……花了一些时间才能建立好的工作流程来简化向lambda的部署。 我偶然发现了Zappa,再也高兴不了……这使得部署到lambda很简单(非常简单)。

OK.  So. Why would you want to move your code to Lambda?

好。 所以。 您为什么要将代码移至Lambda?

Lots of reasons. Here’s a few:

原因很多。 这里是一些:

  • Rather than host your own server to handle some API endpoints — move to Lambda
  • Rather than build out a complex development environment to support your complex system, move some of that complexity to Lambda and make a call to an API endpoint.
  • If you travel and want to downsize your travel laptop but still need to access your python data analytics stack move the stack to Lambda.
  • If you have a script that you run very irregularly and don’t want to pay $5 a month at Digital Ocean — move it to Lambda.
  • 而不是托管自己的服务器来处理一些API终结点-移至Lambda
  • 与其构建一个复杂的开发环境来支持您的复杂系统,不如将某些复杂性移至Lambda并调用API端点。
  • 如果您旅行时想要缩小旅行笔记本电脑的尺寸,但仍需要访问python数据分析堆栈,请将其移至Lambda。
  • 如果您的脚本运行非常不规律,并且不想在Digital Ocean每月支付5美元,请将其移至Lambda。

There are many other more sophisticated reasons of course, but these’ll do for now.

当然,还有许多其他更复杂的原因,但是现在就可以了。

Let’s get started looking at python and AWS Lambda.  You’ll need an AWS account for this.

让我们开始研究python和AWS Lambda。 您将需要一个AWS账户。

First – I’m going to talk a bit about building an API endpoint using Flask. You don’t have to use flask, but its an easy framework to use and you can quickly build an API endpoint with it with very little fuss.  With this example, I’m going to use Lambda to host an API endpoint that uses the Newspaper library to scrape a website, pull down the text and return that text to my local script.

首先–我将讨论有关使用Flask构建API端点的内容。 您不必使用flask,但它使用起来很简单,您可以花很少的时间快速使用它构建API端点。 在此示例中,我将使用Lambda承载一个API终结点,该终结点使用Newspaper库来抓取网站,下拉文本并将其返回到本地脚本。

编写您的第一个Flask + Lambda API (Writing your first Flask + Lambda API)

To get started, install Flask,Flask-Restful and Zappa.  You’ll want to do this in a fresh environment using virtualenv (see my previous posts about virtualenv and vagrant) because we’ll be moving this up to Lambda using Zappa.

首先,安装Flask,Flask-Restful和Zappa。 您将需要在新的环境中使用virtualenv进行此操作( 请参阅我以前有关virtualenv和vagrant的帖子 ),因为我们将使用Zappa将其移至Lambda。

pip install flask flask_restful zappa

Our flask driven API is going to be extremely simple and exist in less than 20 lines of code:

我们的烧瓶驱动的API将非常简单,并以少于20行代码存在:

from flask import Flask
from newspaper import Article
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)


class hello(Resource):
    def get(self):
       return "Hello World"

api.add_resource(hello, '/hello')

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5001)

Note: The ‘host = 0.0.0.0’ and ‘port=50001’ are extranous and are how I use Flask with vagrant. If you keep this in and run it locally, you’d need to visit http://0.0.0.0:5001 to view your app.

注意:'host = 0.0.0.0'和'port = 50001'是无关紧要的,这也是我如何在流浪汉中使用Flask的方法。 如果保留此代码并在本地运行,则需要访问 http://0.0.0.0:5001 查看您的应用。

The last thing you need to do is build your requirements.txt file for Zappa to use when building your application files to send to Lambda. For a quick/dirty requirements file, I used the following:

您需要做的最后一件事是建立您的 requirements.txt Zappa文件,供您在构建要发送到Lambda的应用程序文件时使用。 对于快速/肮脏的需求文件,我使用了以下内容:

zappa
newspaper
flask
flask_restful

Now…let’s get this up to lambda.  With zappa, its as easy as a couple of command line instructions.

现在...让我们开始讨论lambda。 使用zappa,就像几个命令行指令一样简单。

First, run the init command from the command line in your virtualenv:

首先,从virtualenv中的命令行运行init命令:

zappa init

You should see something similar to this:

您应该看到类似以下内容:

zappa init screenshot

You’ll be asked a few questions, you can hit ‘enter’ to take the defaults or enter your own. For this eample, I used ‘dev’ for the environment name (you can set up multiple environments for dev, staging, production, etc) and made a S3 bucket for use with this application.

系统会提示您一些问题,您可以单击“输入”以使用默认值或输入自己的默认值。 对于此示例,我使用“ dev”作为环境名称(您可以为dev,staging,production等设置多个环境),并制作了一个S3存储桶以用于此应用程序。

Zappa should realize you are working with Flask app and automatically set things up for you. It will ask you what the name of your Flask app’s main function is (in this case it is api.app). Lastly, Zappa will ask if you want to deploy to all AWS regions…I chose not to for this example. Once complete, you’ll have a zappa_settings.json file in your directory that will look something like the following:

Zappa应该意识到您正在使用Flask应用并自动为您进行设置。 它将询问您Flask应用程序主要功能的名称是什么(在本例中为api.app)。 最后,Zappa会询问您是否要部署到所有AWS区域……在此示例中,我选择不部署。 完成后,您的目录中将具有一个zappa_settings.json文件,该文件将类似于以下内容:

{
    "dev": {
        "app_function": "api.app", 
        "profile_name": "default", 
        "s3_bucket": "DEV_BUCKET_NAME" #I removed the S3 bucket name for security purposes
    }
}

I’ve found that I need to add more information to this json file before I can successfully deploy. For some reason, Zappa doesn’t add the “region” to the settings file. I also like to add the “runtime” as well. Edit your json file to read (feel free to use whatever region you want):

我发现在成功部署之前,需要向此json文件添加更多信息。 由于某些原因,Zappa不会将“区域”添加到设置文件中。 我也喜欢添加“运行时”。 编辑您的json文件以进行读取(可以随意使用所需的任何区域):

{
    "dev": {
        "app_function": "api.app", 
        "profile_name": "default", 
        "s3_bucket": "DEV_BUCKET_NAME",
        "runtime": "python2.7",
        "aws_region": "us-east-1"
    }
}

Now…you are ready to deploy. You can do that with the following command:

现在…您已经准备好进行部署。 您可以使用以下命令执行此操作:

zappa deploy dev

Zappa will set up all the necessary configurations and systems on AWS AND zip up your libraries and code and push it to Lambda.   I’ve not found another framework as easy to use as Zappa when it comes to deploying…if you know of one feel free to leave a comment.

Zappa将在AWS上设置所有必要的配置和系统,并压缩您的库和代码,并将其推送到Lambda。 在部署方面,我还没有找到像Zappa一样易于使用的其他框架……如果您知道有什么话可以发表评论。

After a minute or two, you should see a “Deployment Complete: …” message that includes the endpoint for your new API. In this case, Zappa built the following endpoint for me:

一两分钟后,您应该看到“ Deployment Complete:…”消息,其中包括新API的端点。 在这种情况下,Zappa为我构建了以下端点:

https://4wq2muonbb.execute-api.us-east-1.amazonaws.com/dev

If you make some changes to your code and need to update Lambda, Zappa makes it easy to do that with the following command:

如果您对代码进行了某些更改并需要更新Lambda,则Zappa可以使用以下命令轻松完成此操作:

zappa update dev

Additionally, if you want to add a ‘production’ lambda environment, all you need to do is add that new environment to your settings json file and deploy it. For this example, our settings file would change to:

此外,如果要添加“生产” lambda环境,您所需要做的就是将该新环境添加到您的设置json文件中并进行部署。 对于此示例,我们的设置文件将更改为:

{
    "dev": {
        "app_function": "api.app", 
        "profile_name": "default", 
        "s3_bucket": "DEV_BUCKET_NAME",
        "runtime": "python2.7",
        "aws_region": "us-east-1"
    }.
    "prod": {
        "app_function": "api.app", 
        "profile_name": "default", 
        "s3_bucket": "PROD_BUCKET_NAME",
        "runtime": "python2.7",
        "aws_region": "us-east-1"
    }
}

Next, do a deploy prod and your production environment is ready to go at a new endpoint.

接下来,做一个 deploy prod 并且您的生产环境已准备好在新的端点运行。

zappa deploy prod

与API的接口 (Interfacing with the API)

Our code is pushed to Lambda and ready to start accepting requests.  In this example’s case, all we are doing is returning “hello world” but you can see the power in this for other functionality.  To check out the results, just open a browser and enter your Zappa Deployment URL and append /hello to the end of it like this:

我们的代码被推送到Lambda,并准备开始接受请求。 在此示例的情况下,我们要做的只是返回“ hello world”,但是您可以看到其中的强大功能。 要查看结果,只需打开浏览器并输入Zappa部署URL,然后将/ hello附加到其末尾,如下所示:

https://4wq2muonbb.execute-api.us-east-1.amazonaws.com/dev/hello

You should see the standard “Hello World” response in your browser window.

您应该在浏览器窗口中看到标准的“ Hello World”响应。

You can find the code for the lambda api.py function here.

您可以在此处找到lambda api.py函数的代码。

Note: At some point, I’ll pull this endpoint down…but will leave it up for a bit for users to play around with.

注意:在某个时候,我会将该端点拉低...但是会将其留出一点,供用户使用。

 

Eric D. Brown , D.Sc. has a doctorate in Information Systems with a specialization in Data Sciences, Decision Support and Knowledge Management. He writes about utilizing python for data analytics at pythondata.com and the crossroads of technology and strategy at ericbrown.com
埃里克·布朗(Eric D.Brown) 拥有信息系统博士学位,专门研究数据科学,决策支持和知识管理。 他写了关于利用数据分析Python在 pythondata.com技术和战略的十字路口在 ericbrown.com

翻译自: https://www.pybloggers.com/2017/09/python-and-aws-lambda-a-match-made-in-heaven/

aws lambda

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值