aws python库_使用AWS Chalice构建无服务器Python应用程序

aws python库

Shipping a web application usually involves having your code up and running on single or multiple servers. In this model, you end up setting up processes for monitoring, provisioning, and scaling your servers up or down. Although this seems to work well, having all the logistics around a web application handled in an automated manner reduces a lot of manual overhead. Enter Serverless.

交付Web应用程序通常涉及使您的代码在单个或多个服务器上运行。 在此模型中,最终将建立用于监视,置备和扩展或扩展服务器的过程。 尽管这看起来似乎很好,但是以自动化方式处理Web应用程序周围的所有物流可以减少大量的人工开销。 输入无服务器。

With Serverless Architecture, you don’t manage servers. Instead, you only need to ship the code or the executable package to the platform that executes it. It’s not really serverless. The servers do exist, but the developer doesn’t need to worry about them.

使用无服务器架构 ,您无需管理服务器。 取而代之的是,您只需要将代码或可执行程序包运送到执行它的平台。 这并不是真正的无服务器。 服务器确实存在,但是开发人员无需担心它们。

AWS introduced Lambda Services, a platform that enables developers to simply have their code executed in a particular runtime environment. To make the platform easy to use, many communities have come up with some really good frameworks around it in order to make the serverless apps a working solution.

AWS推出了Lambda Services ,该平台使开发人员可以在特定的运行时环境中简单地执行其代码。 为了使平台易于使用,许多社区提出了一些非常好的框架,以使无服务器应用程序成为可行的解决方案。

By the end of this tutorial, you’ll be able to:

在本教程结束时,您将能够

  • Discuss the benefits of a serverless architecture
  • Explore Chalice, a Python serverless framework
  • Build a full blown serverless app for a real world use case
  • Deploy to Amazon Web Services (AWS) Lambda
  • Compare Pure and Lambda functions
  • 讨论无服务器架构的好处
  • 探索Chalice,一个Python无服务器框架
  • 针对现实用例构建功能全面的无服务器应用程序
  • 部署到Amazon Web Services(AWS)Lambda
  • 比较Pure和Lambda函数

Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.

免费奖金: 关于Python精通的5个想法 ,这是针对Python开发人员的免费课程,向您展示了将Python技能提升到新水平所需的路线图和心态。

AWS Chalice入门 (Getting Started With AWS Chalice)

Chalice, a Python Serverless Microframework developed by AWS, enables you to quickly spin up and deploy a working serverless app that scales up and down on its own as required using AWS Lambda.

Chalice是AWS开发的Python无服务器微框架,可让您快速启动和部署可运行的无服务器应用程序,该应用程序可以使用AWS Lambda根据需要自行进行伸缩。

为什么选择圣杯? (Why Chalice?)

For Python developers accustomed to the Flask web framework, Chalice should be a breeze in terms of building and shipping your first app. Highly inspired by Flask, Chalice keeps it pretty minimalist in terms of defining what the service should be like and finally making an executable package of the same.

对于习惯于Flask Web框架的Python开发人员而言,Chalice在构建和交付您的第一个应用程序方面应该轻而易举。 受Flask的启发,Chalice在定义服务的外观并最终制作相同的可执行程序包方面保持极简主义。

Enough theory! Let’s start with a basic hello-world app and kick-start our serverless journey.

理论足够多! 让我们从一个基本的hello-world应用开始,开始我们的无服务器之旅。

项目设置 (Project Setup)

Before diving into Chalice, you’ll set up a working environment on your local machine, which will set you up for the rest of the tutorial.

在深入研究Chalice之前,您将在本地计算机上设置一个工作环境,这将为您完成本教程的其余部分。

First, create and activate a virtual environment and install Chalice:

首先,创建并激活虚拟环境并安装Chalice:

 $ python3.6 -m venv env
$ python3.6 -m venv env
$ $ source env/bin/activate
source env/bin/activate
(env)$ pip install chalice
(env)$ pip install chalice

Follow our comprehensive guide on the Pipenv packaging tool.

请遵循关于Pipenv打包工具的综合指南。

Note: Chalice comes with a user-friendly CLI that makes it easy to play around with your serverless app.

注意: Chalice带有一个用户友好的CLI,可以轻松地使用无服务器应用程序。

Now that you have Chalice installed on your virtual environment, let’s use the Chalice CLI to generate some boilerplate code:

现在您已经在虚拟环境中安装了Chalice,让我们使用Chalice CLI生成一些样板代码:

Enter the name of the project when prompted and hit return. A new directory is created with that name:

出现提示时输入项目名称,然后按回车键。 使用该名称创建一个新目录:

<project-name>/
|
├── .chalice/
│   └── config.json
|
├── .gitignore
├── app.py
└── requirements.txt
<project-name>/
|
├── .chalice/
│   └── config.json
|
├── .gitignore
├── app.py
└── requirements.txt

See how minimalist the Chalice codebase is. A .chalice directory, app.py, and requirements.txt is all that it requires to have a serverless app up and running. Let’s quickly run the app on our local machine.

了解Chalice代码库的简约程度。 .chalice目录, app.pyrequirements.txt是启动和运行无服务器应用requirements.txt的全部。 让我们在本地计算机上快速运行该应用程序。

Chalice CLI consists of really great utility functions allowing you to perform a number of operations from running locally to deploying in a Lambda environment.

Chalice CLI由非常好的实用程序功能组成,使您可以执行从本地运行到在Lambda环境中进行部署的许多操作。

在本地构建和运行 (Build and Run Locally)

You can simulate the app by running it locally using the local utility of Chalice:

您可以使用Chalice的local实用程序在local运行该应用程序来模拟该应用程序:

By default, Chalice runs on port 8000. We can now check the index route by making a curl request to http://localhost:8000/:

默认情况下,Chalice在端口8000上运行。我们现在可以通过向http://localhost:8000/发出curl请求来检查索引路由:

 $ curl -X GET http://localhost:8000/
$ curl -X GET http://localhost:8000/
{"hello": "world"}
{"hello": "world"}

Now if we look at app.py, we can appreciate the simplicity with which Chalice allows you to build a serverless service. All the complex stuff is handled by the decorators:

现在,如果我们看一下app.py ,我们将欣赏Chalice允许您构建无服务器服务的简单性。 所有复杂的东西都由装饰器处理:

Note: We haven’t named our app hello-world, as we will build our SMS service on the same app.

注意 :我们尚未将应用命名为hello-world ,因为我们将在同一应用上构建SMS服务。

Now, let’s move on to deploying our app on the AWS Lambda.

现在,让我们继续在AWS Lambda上部署我们的应用程序。

在AWS Lambda上部署 (Deploy on AWS Lambda)

Chalice makes deploying your serverless app completely effortless. Using the deploy utility, you can simply instruct Chalice to deploy and create a Lambda function that can be accessible via a REST API.

Chalice使部署无服务器应用程序变得毫不费力。 使用deploy实用程序,您可以简单地指示Chalice部署和创建可以通过REST API访问的Lambda函数。

Before we begin deployment, we need to make sure we have our AWS credentials in place, usually located at ~/.aws/config. The contents of the file look as follows:

在开始部署之前,我们需要确保已拥有我们的AWS凭证,通常位于~/.aws/config 。 该文件的内容如下所示:

 [default]
[default]
aws_access_key_idaws_access_key_id == <your-access-key-id>
<your-access-key-id>
aws_secret_access_keyaws_secret_access_key == <your-secret-access-key>
<your-secret-access-key>
regionregion == <your-region>
<your-region>

With AWS credentials in place, let’s begin our deployment process with just a single command:

有了AWS凭证后,让我们仅用一个命令就可以开始部署过程:

Note: The generated ARN and API URL in the above snippet will vary from user to user.

注意 :上面的代码段中生成的ARN和API URL会因用户而异。

Wow! Yes, it is really this easy to get your serverless app up and running. To verify simply make a curl request on the generated Rest API URL:

哇! 是的,启动并运行无服务器应用程序确实很容易。 要验证,只需对生成的Rest API URL发出curl请求:

 $ curl -X GET https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/
$ curl -X GET https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/
{"hello": "world"}
{"hello": "world"}

Typically, this is all that you need to get your serverless app up and running. You can also go to your AWS console and see the Lambda function created under the Lambda service section. Each Lambda service has a unique REST API endpoint that can be consumed in any web application.

通常,这是启动和运行无服务器应用程序所需的全部。 您也可以转到AWS控制台,查看在Lambda服务部分下创建的Lambda函数。 每个Lambda服务都有一个唯一的REST API终结点,可以在任何Web应用程序中使用。

Next, you will begin building your Serverless SMS Sender service using Twilio as an SMS service provider.

接下来,您将开始使用Twilio作为SMS服务提供商来构建无服务器SMS Sender服务。

构建无服务器SMS发送器服务 (Building a Serverless SMS Sender Service)

With a basic hello-world app deployed, let’s move on to building a more real-world application that can be used along with everyday web apps. In this section, you’ll build a completely serverless SMS-sending app that can be plugged into any system and work as expected as long as the input parameters are correct.

部署了基本的hello-world应用程序后,让我们继续构建可与日常Web应用程序一起使用的更加真实的应用程序。 在本节中,您将构建一个完全无服务器的SMS发送应用程序,只要输入参数正确,该应用程序便可以插入任何系统并按预期工作。

In order to send SMS, we will be using Twilio, a developer-friendly SMS service. Before we begin using Twilio, we need to take care of a few prerequisites:

为了发送SMS,我们将使用开发者友好的SMS服务Twilio 。 在开始使用Twilio之前,我们需要注意一些先决条件:

  • Create an account and acquire ACCOUNT_SID and AUTH_TOKEN.
  • Get a mobile phone number, which is available for free at Twilio for minor testing stuff.
  • Install the twilio package in our virtual environment using pip install twilio.
  • 创建一个帐户并获取ACCOUNT_SIDAUTH_TOKEN
  • 获取一个手机号码,Twilio可以免费使用它进行次要测试。
  • 安装twilio使用我们的虚拟环境包pip install twilio

With all the above prerequisites checked, you can start building your SMS service client using Twilio’s Python library. Let’s begin by cloning the repository and creating a new feature branch:

选中上述所有前提条件之后,您就可以使用Twilio的Python库开始构建SMS服务客户端。 让我们开始克隆存储库并创建一个新的功能分支:

Now make the following changes to app.py to evolve it from a simple hello-world app to enable support for Twilio service too.

现在对app.py进行以下更改,以使其从一个简单的hello-world应用程序演变为也支持Twilio服务。

First, let’s include all the import statements:

首先,让我们包括所有导入语句:

 from from os os import import environ environ as as env

env

# 3rd party imports
# 3rd party imports
from from chalice chalice import import ChaliceChalice , , Response
Response
from from twilio.rest twilio.rest import import Client
Client
from from twilio.base.exceptions twilio.base.exceptions import import TwilioRestException

TwilioRestException

# Twilio Config
# Twilio Config
ACCOUNT_SID ACCOUNT_SID = = envenv .. getget (( 'ACCOUNT_SID''ACCOUNT_SID' )
)
AUTH_TOKEN AUTH_TOKEN = = envenv .. getget (( 'AUTH_TOKEN''AUTH_TOKEN' )
)
FROM_NUMBER FROM_NUMBER = = envenv .. getget (( 'FROM_NUMBER''FROM_NUMBER' )
)
TO_NUMBER TO_NUMBER = = envenv .. getget (( 'TO_NUMBER''TO_NUMBER' )
)

Next, you’ll encapsulate the Twilio API and use it to send SMS:

接下来,您将封装Twilio API并使用它发送SMS:

In the above snippet, you simply create a Twilio client object using ACCOUNT_SID and AUTH_TOKEN and use it to send messages under the send_sms view. send_sms is a bare bones function that uses the Twilio client’s API to send the SMS to the specified destination. Before proceeding further, let’s give it a try and run it on our local machine.

在上面的代码片段中,您只需使用ACCOUNT_SIDAUTH_TOKEN创建一个Twilio客户端对象,并使用它在send_sms视图下发送消息。 send_sms是使用Twilio客户端的API将SMS发送到指定目标的基本函数。 在继续进行之前,让我们尝试一下并在本地计算机上运行它。

在本地构建和运行 (Build and Run Locally)

Now you can run your app on your machine using the local utility and verify that everything is working fine:

现在,您可以使用local实用程序在计算机上运行您的应用程序,并验证一切运行正常:

 (env)$ chalice (env)$ chalice local
local

Now make a curl POST request to http://localhost:8000/service/sms/send with a specific payload and test the app locally:

现在使用特定的有效负载向http://localhost:8000/service/sms/send curl POST请求,并在本地测试应用程序:

The above request responds as follows:

上面的请求响应如下:

 {
  {
  "status""status" : : "success""success" ,
  ,
  "data""data" : : "SM60f11033de4f4e39b1c193025bcd5cd8""SM60f11033de4f4e39b1c193025bcd5cd8" ,
  ,
  "message""message" : : "SMS successfully sent"
"SMS successfully sent"
}
}

The response indicates that the message was successfully sent. Now, let’s move on to deploying the app on AWS Lambda.

响应指示消息已成功发送。 现在,让我们继续在AWS Lambda上部署应用程序。

在AWS Lambda上部署 (Deploy on AWS Lambda)

As suggested in the previous deployment section, you just need to issue the following command:

如上一部署部分中所建议,您只需发出以下命令:

Note: The above command succeeds, and you have your API URL in the output as expected. Now on testing the URL, the API throws an error message. What went wrong?

注意 :上面的命令成功执行,并且输出中的API URL与预期的一样。 现在在测试URL时,API会引发错误消息。 什么地方出了错?

As per AWS Lambda logs, twilio package is not found or installed, so you need to tell the Lambda service to install the dependencies. To do so, you need to add twilio as a dependency to requirements.txt:

根据AWS Lambda日志 ,未找到或未安装twilio软件包,因此您需要告诉Lambda服务安装依赖项。 为此,您需要将twilio添加为对requirements.txt的依赖项:

 twiliotwilio ==== 6.186.18 .. 1
1

Other packages such as Chalice and its dependencies should not be included in requirements.txt, as they are not a part of Python’s WSGI runtime. Instead, we should maintain a requirements-dev.txt, which is applicable to only the development environment and contains all Chalice-related dependencies. To learn more, check out this GitHub issue.

其他软件包(例如Chalice及其依赖项)不应包含在requirements.txt ,因为它们不是Python WSGI运行时的一部分。 相反,我们应该维护一个requirements-dev.txt ,它仅适用于开发环境,并且包含所有与Chalice相关的依赖项。 要了解更多信息,请查看GitHub问题

Once all the package dependencies are sorted, you need to make sure all the environment variables are also shipped along and set correctly during the Lambda runtime. To do so, you have to add all the environment variables in .chalice/config.json in the following manner:

对所有程序包依赖项进行排序后,您需要确保在Lambda运行时中还附带并正确设置了所有环境变量。 为此,您必须按照以下方式在.chalice/config.json中添加所有环境变量:

Now we’re good to deploy:

现在我们可以部署了:

 Creating deployment package.
Creating deployment package.
Updating policy for IAM role: sms-shooter-dev
Updating policy for IAM role: sms-shooter-dev
Updating lambda function: sms-shooter-dev
Updating lambda function: sms-shooter-dev
Updating rest API
Updating rest API
Resources deployed:
Resources deployed:
  - Lambda ARN: arn:aws:lambda:ap-south-1:679337104153:function:sms-shooter-dev
  - Lambda ARN: arn:aws:lambda:ap-south-1:679337104153:function:sms-shooter-dev
  - Rest API URL: https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/
  - Rest API URL: https://fqcdyzvytc.execute-api.ap-south-1.amazonaws.com/api/

Do a sanity check by making a curl request to the generated API endpoint:

通过向生成的API端点发出curl请求来进行完整性检查:

The above request responds as expected:

上面的请求按预期响应:

 {
  {
  "status""status" : : "success""success" ,
  ,
  "data""data" : : "SM60f11033de4f4e39b1c193025bcd5cd8""SM60f11033de4f4e39b1c193025bcd5cd8" ,
  ,
  "message""message" : : "SMS successfully sent"
"SMS successfully sent"
}
}

Now, you have a completely serverless SMS sending service up and running. With the front end of this service being a REST API, it can be used in other applications as a plug-and-play feature that is scalable, secure, and reliable.

现在,您已经建立并运行了一个完全无服务器的SMS发送服务。 由于该服务的前端是REST API,因此可以在其他应用程序中用作可扩展,安全和可靠的即插即用功能。

重构 (Refactoring)

Finally, we will refactor our SMS app to not contain all the business logic in app.py completely. Instead, we will follow the Chalice prescribed best practices and abstract the business logic under the chalicelib/ directory.

最后,我们将重构我们的SMS应用程序,使其不完全包含app.py所有业务逻辑。 相反,我们将遵循Chalice规定的最佳实践,并在chalicelib/目录下抽象业务逻辑。

Let’s begin by creating a new branch:

让我们从创建一个新分支开始:

First, create a new directory in the root directory of the project named chalicelib/ and create a new file named sms.py:

首先,在名为chalicelib/的项目的根目录中创建一个新目录,并创建一个名为sms.py的新文件:

 (env)$ mkdir chalicelib
(env)$ mkdir chalicelib
(env)$ touch chalicelib/sms.py
(env)$ touch chalicelib/sms.py

Update the above created chalicelib/sms.py with the SMS sending logic by abstracting things from app.py:

通过从app.py提取内容,使用SMS发送逻辑更新上面创建的chalicelib/sms.py app.py

The above snippet only accepts the input params and responds as required. Now to make this work, we need to make changes to app.py as well:

上面的代码段仅接受输入参数,并根据需要进行响应。 现在要进行这项工作,我们还需要对app.py进行更改:

 # Core imports
# Core imports
from from chalice chalice import import ChaliceChalice , , Response
Response
from from twilio.base.exceptions twilio.base.exceptions import import TwilioRestException

TwilioRestException

# App level imports
# App level imports
from from chalicelib chalicelib import import sms

sms

app app = = ChaliceChalice (( app_nameapp_name == 'sms-shooter''sms-shooter' )


)


@app@app .. routeroute (( '/''/' )
)
def def indexindex ():
    ():
    return return {{ 'hello''hello' : : 'world''world' }


}


@app@app .. routeroute (( '/service/sms/send''/service/sms/send' , , methodsmethods == [[ 'POST''POST' ])
])
def def send_smssend_sms ():
    ():
    request_body request_body = = appapp .. current_requestcurrent_request .. json_body
    json_body
    if if request_bodyrequest_body :
        :
        trytry :
            :
            resp resp = = smssms .. sendsend (( request_bodyrequest_body )
            )
            if if respresp :
                :
                return return ResponseResponse (( status_codestatus_code == 201201 ,
                                ,
                                headersheaders == {{ 'Content-Type''Content-Type' : : 'application/json''application/json' },
                                },
                                bodybody == {{ 'status''status' : : 'success''success' ,
                                      ,
                                      'data''data' : : respresp .. sidsid ,
                                      ,
                                      'message''message' : : 'SMS successfully sent''SMS successfully sent' })
            })
            elseelse :
                :
                return return ResponseResponse (( status_codestatus_code == 200200 ,
                                ,
                                headersheaders == {{ 'Content-Type''Content-Type' : : 'application/json''application/json' },
                                },
                                bodybody == {{ 'status''status' : : 'failure''failure' ,
                                      ,
                                      'message''message' : : 'Please try again!!!''Please try again!!!' })
        })
        except except TwilioRestException TwilioRestException as as excexc :
            :
            return return ResponseResponse (( status_codestatus_code == 400400 ,
                            ,
                            headersheaders == {{ 'Content-Type''Content-Type' : : 'application/json''application/json' },
                            },
                            bodybody == {{ 'status''status' : : 'failure''failure' ,
                                  ,
                                  'message''message' : : excexc .. msgmsg })
})

In the above snippet, all the SMS sending logic is invoked from the chalicelib.sms module, making the view layer a lot cleaner in terms of readability. This abstraction lets you add much more complex business logic and customize the functionality as required.

在以上代码段中,所有的SMS发送逻辑都从chalicelib.sms模块中调用,从而使视图层在可读性方面更加整洁。 这种抽象使您可以添加更复杂的业务逻辑并根据需要自定义功能。

完整性检查 (Sanity Check)

After refactoring our code, let’s ensure it is running as expected.

重构我们的代码后,让我们确保它按预期运行。

在本地构建和运行 (Build and Run Locally)

Run the app once again using the local utility:

使用local实用程序再次运行该应用程序:

Make a curl request and verify. Once that’s done, move on to deployment.

发出卷曲请求并进行验证。 完成后,继续进行部署。

在AWS Lambda上部署 (Deploy on AWS Lambda)

Once you are sure everything is working as expected, you can now finally deploy your app:

一旦确定一切都能按预期进行,现在就可以最终部署您的应用程序:

 (env)$ chalice deploy
(env)$ chalice deploy

As usual, the command executes successfully and you can verify the endpoint.

像往常一样,命令成功执行,您可以验证端点。

结论 (Conclusion)

You now know how to do the following:

您现在知道如何执行以下操作:

  • Build a serverless application using AWS Chalice in accordance with best practices
  • Deploy your working app on the Lambda runtime environment
  • 根据最佳实践,使用AWS Chalice构建无服务器应用程序
  • 在Lambda运行时环境上部署您的工作应用程序

Lambda services under the hood are analogous to pure functions, which have a certain behavior on a set of input/output. Developing precise Lambda services allows for better testing, readability, and atomicity. Since Chalice is a minimalist framework, you can just focus on the business logic, and the rest is taken care of, from deployment to IAM policy generation. This is all with just a single command deployment!

底层的Lambda服务类似于纯功能,它们对一组输入/输出具有一定的行为。 开发精确的Lambda服务可实现更好的测试,可读性和原子性。 由于Chalice是一个极简主义的框架,因此您可以只关注业务逻辑,其余的工作都从部署到IAM策略生成来处理。 仅需单个命令部署即可完成所有工作!

Moreover, Lambda services are mostly focused on heavy CPU bound processing and scale in a self-governed manner, as per the number of requests in a unit of time. Using serverless architecture allows your codebase to be more like SOA (Service Oriented Architecture). Using AWS’s other products in their ecosystem that plug in well with Lambda functions is even more powerful.

此外,根据单位时间内的请求数,Lambda服务主要集中于繁重的CPU绑定处理和以自治方式进行扩展。 使用无服务器体系结构可使您的代码库更像SOA(面向服务的体系结构)。 在其生态系统中使用AWS的其他产品,这些产品可以很好地与Lambda函数配合使用,功能甚至更加强大。

翻译自: https://www.pybloggers.com/2018/12/building-serverless-python-apps-using-aws-chalice/

aws python库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值