以下是使用AWS创建Lambda函数的三种常用方法

by Sam Williams

通过山姆·威廉姆斯

以下是使用AWS创建Lambda函数的三种常用方法 (Here are three common ways to create your Lambda functions with AWS)

AWS Lambda functions are incredible! They’re functions that are hosted on Amazon Web Services that can be triggered in many different ways.

AWS Lambda函数令人难以置信! 它们是托管在Amazon Web Services上的功能,可以通过许多不同方式触发。

One of the best parts is that you only pay for the time the Lambda function is running. Got something that only runs once an hour and only takes 2 seconds? You’ll only be charged for 48 seconds a day! That’s insane compared to running a 24/7 AWS EC2 instance or your own private server.

最好的部分之一是,您只需为Lambda函数的运行时间付费。 有只每小时运行一次且只需2秒的东西吗? 您一天只需支付48秒的费用! 与运行24/7 AWS EC2实例或您自己的私有服务器相​​比,这真是太疯狂了。

Today we’ll create a Lambda function and look at the three best ways to work with the code.

今天,我们将创建一个Lambda函数,并研究使用代码的三种最佳方法。

创建Lambda函数 (Creating a Lambda Function)

Once you’ve got your AWS account set up, there are a few ways to create a new Lambda function. We’re going to be using the AWS Console.

设置好AWS账户后,有几种方法可以创建新的Lambda函数。 我们将使用AWS控制台。

AWS控制台 (AWS Console)

Within the AWS Console, you can find AWS Lambda under Services which takes you to the Lambda console.

在AWS控制台中,您可以在服务下找到AWS Lambda 将您带到Lambda控制台。

This is what you’ll see if this is your first Lambda. Click that Create a function button to start setting up your first function.

如果这是您的第一个Lambda,则将看到此内容。 单击创建功能按钮以开始设置您的第一个功能。

You’ll end up on the setup page where you configure some aspects of the function (name, runtime, role). You can create a Lambda from Blueprints or Serverless Application Repos but in this example, we’ll Author it from scratch.

最后,您将在设置页面上配置功能的某些方面(名称,运行时,角色)。 您可以从Blueprints或无服务器应用程序仓库创建Lambda,但在此示例中,我们将从头开始编写。

Enter the name for your function (this must be unique to your user or sub-account), choose your Runtime (We’ll use Node.js 8.10), and select a role.

输入函数的名称(该名称对于用户或子帐户必须是唯一的),选择运行时(我们将使用Node.js 8.10),然后选择一个角色。

You’ll have to create a new role if you don’t already have one. Create one from template and you can leave Policy templates blank.

如果您还没有角色,则必须创建一个新角色。 从模板创建一个,您可以将策略模板留为空白。

编写Lambda函数代码 (Writing Your Lambda Function Code)

One of the big advantages with Lambdas is that you can choose how you write and edit them. There are three main ways to do so

Lambdas的一大优点是您可以选择编写和编辑它们的方式。 可以通过以下三种主要方法

  • Lambda Console

    Lambda控制台
  • Cloud9

    云9
  • On your local machine

    在您的本地机器上

I’m going to cover all three and discuss advantages and disadvantages for each of them.

我将介绍这三个方面,并讨论它们各自的优点和缺点。

方法1:Lambda控制台 (Method 1: Lambda Console)

This is the screen you got sent to when you created the function. You’ll see that there is a lot of stuff going on. The bit that we care about for now is the Function code section, about half way down.

这是创建函数时发送到的屏幕。 您会发现其中发生了很多事情。 我们现在关心的是功能代码 部分,大约下降一半。

In here we have a basic editor. I believe it’s based on the Cloud 9 IDE and works pretty damn well for simple Lambda functions. You can see below that the handler is an async function because I chose to use Node 8.10. If you prefer callbacks then Node 6.10 is the runtime for you.

在这里,我们有一个基本的编辑器。 我相信它基于Cloud 9 IDE,并且对于简单的Lambda函数非常有效。 您可以在下面看到该处理程序是一个异步函数,因为我选择使用Node 8.10。 如果您更喜欢回调,那么Node 6.10是您的运行时。

优点 (The advantages)
  • It’s a decent editor.

    这是一个体面的编辑。
  • You can access it from any computer through your AWS console.

    您可以通过AWS控制台从任何计算机访问它。
劣势 (The Disadvantages)
  • It doesn’t seem to be 100% stable. Sometimes it doesn’t let you save so you have to copy all of your work to a local file, reload the page, and copy your work back. I hope that this gets fixed soon!

    它似乎不是100%稳定的。 有时,它不允许您保存,因此您必须将所有工作复制到本地文件,重新加载页面,然后再复制工作。 我希望这个问题能尽快解决!
  • It doesn’t have a terminal. This means that you can’t install packages using NPM using this method alone.

    它没有终端。 这意味着您不能仅使用此方法使用NPM安装软件包。

方法2:Cloud9编辑器 (Method 2: Cloud9 Editor)

Amazon recently acquired Cloud9, an online development platform. It seems to run a very basic version of Ubuntu that is integrated with the rest of the AWS platform.

亚马逊最近收购了在线开发平台Cloud9。 它似乎运行了非常基本的Ubuntu版本,该版本已与其余AWS平台集成在一起。

Search for Cloud9 in the AWS console, go to the page and select Create environment. From here you give your environment a name and go to the next step.

搜索Cloud9 在AWS控制台中,转到页面并选择Create environment 从这里为您的环境命名,然后转到下一步。

Here you get to choose what you want to run this environment on. The great thing is that t2.micro is Free-tier eligible so you can use this method without getting charged anything if you’re on the free tier. I’ve never needed anything more powerful than a t2.micro.

在这里,您可以选择运行此环境的对象。 很棒的是t2.micro符合免费套餐资格,因此如果您处于免费套餐,则可以使用此方法而无需支付任何费用。 我再也不需要比t2.micro更强大的功能了。

Continue on from here and you’ll end up in your new Cloud9 environment!

从这里继续,您将进入新的Cloud9环境!

The cool thing about this is that you have access to all of your Lambda functions from inside your Cloud9 environment. Click AWS Resources and under Remote Functions you’ll find all of your functions. Click on the Lambda function you want to edit and then hit the download icon above to import it into your environment.

很棒的事情是您可以从Cloud9环境中访问所有Lambda函数。 单击AWS资源 在“ 远程功能”下,您将找到所有功能。 单击要编辑的Lambda函数,然后单击上方的下载图标以将其导入您的环境。

Once that’s done, it’ll just be like you’re working on it locally.

一旦完成,就好像您在本地上工作一样。

Once you’re finished, just select the function you’ve been working on from the local list and hit the upload button. Within a few seconds it’ll be live with all your changes.

完成后,只需从本地列表中选择您正在使用的功能,然后单击上载按钮即可。 几秒钟之内,所有更改都会生效。

优点 (The Advantages)
  • Again, this is all remote so you don’t need to worry about forgetting to commit your work or save it to a memory stick if you work on multiple machines.

    同样,这都是远程的,因此,如果您在多台计算机上工作,则无需担心忘记提交工作或将其保存到记忆棒中。
  • Getting your functions and uploading them is super easy. This is by far the best bit about this method.

    获取功能并上传它们非常简单。 到目前为止,这是有关此方法的最好之处。
  • You now have an integrated terminal, allowing you to install npm packages and do everything else you want to do using the terminal.

    现在,您有了一个集成的终端,可以安装npm软件包并使用该终端执行您想做的所有其他事情。
劣势 (The Disadvantages)
  • It still has the same stability issues that the Lambda editor has. I’ve had multiple occasions where I’ve tried to save the function but couldn’t, having to copy to local, refresh, and recopy to Cloud 9. After a few times, I gave up and moved to local editing.

    它仍然具有与Lambda编辑器相同的稳定性问题。 我曾多次尝试保存函数,但是无法复制到本地,刷新并重新复制到Cloud9。但是几次之后,我放弃了,转而进行本地编辑。

方法3:本地编辑 (Method 3: Local Editing)

I’m going to do this one a bit differently, I’ll list the advantages and disadvantages then show you how to make it so much better.

我将做一些不同的事情,我将列出优点和缺点,然后向您展示如何使其变得更好。

优点 (The Advantages)
  • Local editing is how most developers will work. We can use our favourite IDE, extensions, and colour schemes.

    本地编辑是大多数开发人员的工作方式。 我们可以使用我们最喜欢的IDE,扩展名和配色方案。
  • It’s stable (as long as your computer is).

    它很稳定(只要您的计算机是稳定的)。
劣势 (The Disadvantages)
  • There’s no fancy button to get and upload your work to AWS.

    没有花哨的按钮来获取您的工作并将其上传到AWS。
  • Your work is local, so having multiple users or just working on multiple devices is more complex.

    您的工作是本地的,因此拥有多个用户或仅在多个设备上工作更加复杂。
本地编辑技巧 (Local Editing Tricks)

Because the advantages for this method are so appealing (or the disadvantages of other methods are so appalling), we’re going to utilise some basic workarounds. It should take about 15 minutes to set up everything we need!

因为此方法的优点非常吸引人(或者其他方法的缺点令人震惊),所以我们将利用一些基本的变通方法。 设置我们需要的所有内容大约需要15分钟!

AWS CLI (AWS CLI)

To upload our work to AWS we can use the AWS CLI. This allows us to upload a zip file to our AWS account that populates a given Lambda.

要将我们的工作上传到AWS,我们可以使用AWS CLI。 这使我们可以将zip文件上传到填充给定Lambda的AWS账户。

To do this, we first need to setup AWS CLI. You can install it using this tutorial or by typing npm install -g aws-cli into your terminal. Now we need to set up a user for our CLI to log in as.

为此,我们首先需要设置AWS CLI。 您可以使用本教程或在终端中键入npm install -g aws-cli 。 现在,我们需要为CLI设置用户以登录。

In IAM Management, click Add User, give the user a name and select Programmatic Access. This will allow us to act as the user remotely.

在“ IAM管理”中,单击“ 添加用户” 为用户命名,然后选择“ 程序访问” 这将使我们能够远程充当用户。

In the permissions screen, choose to Attach existing policies directly and select AdministatorAccess. This will let you do whatever you want through your CLI. You can set stricter policies on this user if you want, or if this is for another person to access.

在权限屏幕中,选择直接附加现有策略,然后选择AdministatorAccess 这样,您就可以通过CLI进行任何操作。 您可以根据需要对此用户设置更严格的策略,或者供其他人访问。

There’s another screen before you end up being shown your access keys. Copy your access keys and open a terminal. Run the command aws configure which will ask you for 4 things.

在最终显示访问键之前,还有另一个屏幕。 复制您的访问密钥并打开终端。 运行命令aws configure ,它将询问您四件事。

AWS Access Key ID [None]: "Your Access Key ID"AWS Secret Access Key [None]: "Your Secret Access Key"Default region name [eu-west-1]:Default output format [json]:

The first two are found on the last page of the user creation and the next two can be left as default or changed to whatever you want.

前两个位于用户创建的最后一页,后两个可以保留为默认值或更改为所需的任何值。

使用AWS CLI (Using the AWS CLI)

Now that we’ve got the CLI set up, we can use it to make our lives so much easier. If you have a folder with a Lambda function stored in there, we can run a few simple commands to upload it to AWS.

现在我们已经设置了CLI,我们可以使用它来简化我们的生活。 如果您有一个其中存储了Lambda函数的文件夹,我们可以运行一些简单的命令将其上传到AWS。

cd MyLambdaFunctionrm index.zipzip –X –r ./index.zip *aws lambda update-function-code     --function-name MyLambdaFunction     --zip-file fileb://index.zipcd ..
AWS CLI构建脚本 (AWS CLI Build Script)

This is great, but typing this all out every time you want to upload a new Lambda version would become tiresome. So we’re going to use a build script.

很棒,但是每次您要上传新的Lambda版本时都输入所有内容会很烦。 因此,我们将使用构建脚本。

For this exact script to work, you need to have a folder structure like this. Each lambda has a folder with the relevant files and a region.txt file.

为了使此确切的脚本起作用,您需要具有这样的文件夹结构。 每个lambda都有一个包含相关文件和region.txt文件的文件夹。

This script not only runs the basic AWS CLI commands, but it also does extra checks, runs npm install , and echos out details about the progress.

该脚本不仅运行基本的AWS CLI命令,还进行额外的检查,运行npm install ,并回显有关进度的详细信息。

This may look like a complex script, but it can be easy broken down. The first 32 lines move into the folder of the Lambda function, run npm install and check that AWS CLI is installed. Line 38 zips the folder, except for certain files, and line 42 uploads the zip file.

这看起来像一个复杂的脚本,但很容易分解。 前32行移至Lambda函数的文件夹中,运行npm install并检查是否已安装AWS CLI。 第38行压缩了文件夹(某些文件除外),第42行上传了压缩文件。

Now all you need to do is to navigate to the main folder wher the Lambdas function resides and run

现在,您需要做的就是在Lambdas函数驻留并运行时导航到主文件夹

./build.sh example-lambda

This script could be modified and expanded to include region-specific uploading, batch uploading multiple Lambda functions or Git integration.

该脚本可以进行修改和扩展,以包括特定于区域的上载,批量上载多个Lambda函数或Git集成。

吉特 (Git)

Most people reading this will use Git on a daily basis. There’s a reason for that: it makes life simpler.

大多数阅读此书的人每天都会使用Git。 这是有原因的:它使生活更简单。

Having a Git repository for all of your Lambda functions is a great way to work with teams of developers or by yourself on multiple machines.

为所有Lambda函数提供一个Git存储库是与开发人员团队或自己在多台计算机上合作的好方法。

摘要 (Summary)

There are three common ways to edit Lambda functions: in the Lambda console, Cloud 9, and locally.

编辑Lambda函数的常见方法有以下三种:在Lambda控制台,Cloud 9和本地中。

There are advantages and disadvantages of all three methods, but personally I think the best choice is to write the function locally and deploy it using a deployment script.

这三种方法各有利弊,但我个人认为最好的选择是在本地编写该功能,然后使用部署脚本进行部署。

If you found this article useful then give it some claps and follow me for more AWS tutorials and developer articles!

如果您发现本文有用,请给它鼓掌,并关注我以获取更多AWS教程和开发人员文章!

NEXT → Say Hello to Your Own Amazon Lex Chat Bot

下一页→ 向您自己的Amazon Lex聊天机器人打个招呼

翻译自: https://www.freecodecamp.org/news/aws-lambda-offering-developers-ultimate-flexibility-d8939ff4220/

你可能感兴趣的:(python,java,大数据,git,linux)