aws python库
The very first idea of creating my own app and deploying it on the cloud so that everyone could use it is super exciting to me, and this is what inspired me to write this post. If this idea also intrigues you, please follow through and from this post, you will learn how to deploy a python app step by step.
创建我自己的应用程序并将其部署到云上以使每个人都可以使用它的最初想法对我来说非常激动,这就是促使我写这篇文章的原因。 如果这个想法也引起您的兴趣,请继续阅读本博文,您将逐步学习如何部署python应用。
You will need to wrap your idea in an app, or say an API, which can process calls from the internet. An example is here. This is a flask app,
您需要将您的想法包装在可以处理来自互联网的调用的应用程序或API中。 这里有一个例子。 这是一个烧瓶应用
where the key lies in the app.py file, and this app receives your resume and help refer you internally. Notice that we don’t even need a docker file, the AWS Lambda is so light-weighted that you don’t even need to wrap your code in a container!
密钥位于app.py文件中,此应用程序会收到您的履历并帮助您在内部引荐您。 请注意,我们甚至都不需要docker文件,AWS Lambda的重量如此轻巧,甚至不需要将代码包装在容器中!
Zappa, a quote from the official docs, it
Zappa ,来自官方文档的报价,它
makes it super easy to build and deploy server-less, event-driven Python applications (including, but not limited to, WSGI web apps) on AWS Lambda + API Gateway. Think of it as “serverless” web hosting for your Python apps. That means infinite scaling, zero downtime, zero maintenance — and at a fraction of the cost of your current deployments!
使得在AWS Lambda + API Gateway上构建和部署无服务器,事件驱动的Python应用程序(包括但不限于WSGI Web应用程序)变得非常容易。 将其视为Python应用程序的“无服务器”网络托管。 这意味着无限扩展,零停机时间,零维护,而成本仅为当前部署的一小部分!
If you’ve been using AWS service for a while, you would know that to deploy a service on the cloud with the usage of multiple different services and configurations is no easy task, but Zappa comes to the rescue, that with simple commands(trust me, it’s really just a few lines!), all the heavy lifting configurations would be done!
如果您已经使用AWS服务已有一段时间,那么您会知道使用多种不同的服务和配置在云上部署服务并不是一件容易的事,但是Zappa可以通过简单的命令来解决(信任我,实际上只有几行!),所有繁重的配置都可以完成!
pip install zappaBTW, I assume you have all the packages installed in the project virtual environment, if not, do
顺便说一句,我假设您已经在项目虚拟环境中安装了所有软件包,如果没有,请执行
virtualenv -p `which python3` env(you need to have virtualenv pre-installed)
(您需要预先安装virtualenv)
Now do
现在做
zappa initGo through the settings one by one, if you don’t understand just use the default, and you will still be able to change it afterwards. After this, you would have a zappa_setting.json in your root folder, mine looks like this
如果您不了解该设置,请一一进行设置,之后仍然可以更改它。 之后,您的根文件夹中将有一个zappa_setting.json ,我的看起来像这样
{ "production": { "app_function": "app.app", "aws_region": "ap-southeast-1", "profile_name": "default", "project_name": "referral-api", "runtime": "python3.7", "s3_bucket": "zappa-referral-api-eu2hzy8sf" }}Now you need to go to your AWS console, but wait, you said Zappa would do all the AWS work for us? Yes, but think it this way, before Zappa could make any changes on your behalf, it needs access to you AWS resources, and this step is to give Zappa the credential to do such thing.
现在您需要转到AWS控制台,但是等一下,您说Zappa会为我们完成所有AWS工作吗? 是的,但是以这种方式考虑,在Zappa可以代表您进行任何更改之前,它需要访问您的AWS资源,并且此步骤是为Zappa提供执行此操作的凭据。
You can follow the steps here deploy-serverless-app (trust me, this one is really an illustrative guide through with all the images you need).
您可以按照此处deploy-serverless-app的步骤进行操作(相信我,这实际上是对您需要的所有映像的说明性指南)。
For the policy attached to the group, use this one! (The one in the link above does not enable you to update your app)
对于该组的附加政策,请使用此政策! (上面链接中的那个不能使您更新应用程序)
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iam:AttachRolePolicy", "iam:GetRole", "iam:CreateRole", "iam:PassRole", "iam:PutRolePolicy" ], "Resource": [ "arn:aws:iam::XXXXXXXXXXXXXXXX:role/*-ZappaLambdaExecutionRole" ] }, { "Effect": "Allow", "Action": [ "lambda:CreateFunction", "lambda:ListVersionsByFunction", "logs:DescribeLogStreams", "events:PutRule", "lambda:GetFunctionConfiguration", "cloudformation:DescribeStackResource", "apigateway:DELETE", "apigateway:UpdateRestApiPolicy", "events:ListRuleNamesByTarget", "apigateway:PATCH", "events:ListRules", "cloudformation:UpdateStack", "lambda:DeleteFunction", "events:RemoveTargets", "logs:FilterLogEvents", "apigateway:GET", "lambda:GetAlias", "events:ListTargetsByRule", "cloudformation:ListStackResources", "events:DescribeRule", "logs:DeleteLogGroup", "apigateway:PUT", "lambda:InvokeFunction", "lambda:GetFunction", "lambda:UpdateFunctionConfiguration", "cloudformation:DescribeStacks", "lambda:UpdateFunctionCode", "lambda:DeleteFunctionConcurrency", "events:DeleteRule", "events:PutTargets", "lambda:AddPermission", "cloudformation:CreateStack", "cloudformation:DeleteStack", "apigateway:POST", "lambda:RemovePermission", "lambda:GetPolicy" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "s3:ListBucketMultipartUploads", "s3:CreateBucket", "s3:ListBucket" ], "Resource": "arn:aws:s3:::zappa-*" }, { "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:AbortMultipartUpload", "s3:DeleteObject", "s3:ListMultipartUploadParts" ], "Resource": "arn:aws:s3:::zappa-*/*" } ]}Now you add the credentials you created for Zappa to your local file (credentials and configs), mine looks like this
现在,您将为Zappa创建的凭据添加到本地文件(凭据和配置)中,我的看起来像这样
[default]aws_access_key_id = ****aws_secret_access_key = ****[zappa]aws_access_key_id = ****aws_secret_access_key = ****and config like this
并像这样配置
[default]region = ap-southeast-1output = json[zappa]region = ap-southeast-1output = jsonI created one especially for Zappa, because I believe most of us would have a default credential for ourself, this setting would allow you to switch between different profiles.
我特别为Zappa创建了一个证书,因为我相信我们大多数人都将拥有自己的默认证书,此设置将允许您在不同的配置文件之间进行切换。
Now it is ready to deploy!
现在可以部署了!
export AWS_DEFAULT_PROFILE=zappazappa deploy productionYou would see your API url right after the command line. Now go to your AWS Lambda you should see our API deployed:
您将在命令行后看到您的API网址。 现在转到您的AWS Lambda,您应该看到已部署我们的API:
And in API Gateway you can see:
在API Gateway中,您可以看到:
Now that I can test the public endpoint on postman and get a response:
现在,我可以在邮递员上测试公共端点并得到响应:
Congrats! Now you have your app fully deployed to AWS with Lambda and API Gateway!
恭喜! 现在,您已使用Lambda和API Gateway将应用程序完全部署到AWS!
You can stop here if this already satisfies your need, but if you would like to distribute your API and add restrictions for it, you can continue to the next part.
如果已经满足您的需要,您可以在这里停止,但是如果您想分发您的API并为其添加限制,则可以继续下一部分。
You might have noticed that so far our API is publicly accessible, which means anyone can access our API and it is susceptible to malicious attack. To avoid such undesired visits, we need to add extra limitations on our API usage (Usage Plan) and credentials (x-api-key) to restrict visits.
您可能已经注意到,到目前为止,我们的API是可以公开访问的,这意味着任何人都可以访问我们的API,并且它容易受到恶意攻击。 为了避免此类意外访问,我们需要对API使用情况(使用计划)和凭据(x-api-key)添加更多限制,以限制访问。
To add a key to your API, follow the steps here.
要向您的API添加密钥,请按照此处的步骤操作。
RapidAPI is where for everyone to freely open and sell their API to the world as long as someone is willing to pay for it.
只要有人愿意为此付出代价, RapidAPI就是每个人都可以自由打开并向世界出售其API的地方。
Go to RapidAPI and click Add New API
转到RapidAPI并单击Add New API
Fill in the basic info of your API 填写您的API的基本信息 For the URL of your API, paste the AWS url we previous got after deploying though Zappa对于您的API的URL,粘贴通过Zappa部署后我们先前获得的AWS URLIn the access control add your api key as follow 在访问控制中,添加您的api密钥,如下所示5. Add a pricing plan and you are ready to launch it to the public!
5.添加一个定价计划,您就可以将其发布给公众了!
Lastly, if you are interested, you can check out my referral API. If you are looking for a new career, so far it helps you to refer you to the company of Bytedance and Grab through the internal portal.
最后,如果您有兴趣,可以查看我的引荐API 。 如果您正在寻找新的职业,到目前为止,它可以帮助您通过内部门户网站将您引荐到Bytedance和Grab的公司。
This is post is initially inspired by this, a great post helped me to start the plan, feel free to check that out!
这是后最初的灵感来自于这个,一个伟大的职位帮助我开始计划,随时检查出来!
翻译自: https://towardsdatascience.com/deploy-a-python-api-on-aws-c8227b3799f0
aws python库
相关资源:python-lambda, 在AWS中,开发和部署无服务器 python 代码的工具包.zip