您的位置:首页 > 其它

cron计划任务_Cron Job AWS Lambda函数教程–如何计划任务

2020-08-21 00:25 525 查看

cron计划任务

Cron jobs are usually used to schedule commands at a specific time. You can use them for tasks like running backups, monitoring the status of the system, or running system maintenance tasks.

Cron作业通常用于在特定时间安排命令。 您可以将它们用于执行任务,例如运行备份,监视系统状态或运行系统维护任务。

Cron jobs are a helpful utility for system administrators. And when you are administering a system in the cloud, cron jobs are still very useful – you still have to do a lot of administrative tasks on your systems.

Cron作业对系统管理员是有用的实用程序。 而且,当您在云中管理系统时,cron作业仍然非常有用–您仍然必须在系统上执行许多管理任务。

One way of running cron jobs in the cloud is to use a function as a service (FaaS), like Lambda in the AWS ecosystem.

在云中运行cron作业的一种方法是使用功能即服务(FaaS),例如AWS生态系统中的Lambda。

Functions execute when they are triggered to do so, and they run code in the cloud without the need to provision or maintain any infrastructure. Also functions can be configured to run at a certain time or with certain periodicity, like traditional cron jobs.

触发功能后,功能便会执行,并且功能可在云中运行代码,而无需设置或维护任何基础架构。 还可以将功能配置为在特定时间或以一定周期运行,例如传统的cron作业。

In this blog post, I will use the AWS ecosystem to show you a concrete example on how to create a cron job using a function in the cloud.

在此博客文章中,我将使用AWS生态系统向您展示有关如何使用云中的功能创建cron作业的具体示例。

Amazon CloudWatch活动 (Amazon CloudWatch events)

In order to use a Lambda function as a cron job, we need to understand Amazon CloudWatch events.

为了将Lambda函数用作cron作业,我们需要了解Amazon CloudWatch事件。

Amazon CloudWatch events are sent when there are changes in the AWS resources. These events can trigger an AWS Lambda function. When your AWS resources change state, they automatically send CloudWatch events to the event stream.

当AWS资源发生更改时,将发送Amazon CloudWatch事件。 这些事件可以触发AWS Lambda函数。 当您的AWS资源更改状态时,它们会自动将CloudWatch事件发送到事件流。

Therefore, you can create a rule that triggers a specific Lambda function when something happens. For example, you can automatically invoke a Lambda function when there is a change in an AutoScaling group.

因此,您可以创建一个规则,在发生某些情况时触发特定的Lambda函数。 例如,您可以在AutoScaling组中发生更改时自动调用Lambda函数。

In addition, CloudWatch events can invoke a Lambda function to execute on a regular schedule. And in this way you can have, for example, a Lambda function that turns off all your testing and development EC2 instances after 6pm and another one that turns them on after 8am.

此外,CloudWatch事件可以调用Lambda函数以定期执行。 这样,您可以拥有一个Lambda函数,该函数在下午6点之后关闭所有测试和开发EC2实例,而另一个在上午8点之后打开它们。

设置演示 (Setting up the demo)

I want to show you an example of a Lambda function that can perform actions on your EC2 instances. I will be using AWS SAM to define my Lambda function as infrastructure as code.

我想向您展示可以在EC2实例上执行操作的Lambda函数的示例。 我将使用AWS SAM将Lambda函数定义为基础架构(如代码)。

If you want to try this demo out, you need to have an AWS account and one or more EC2 instances configured in your AWS account. These are the ones that we are going to manipulate from the Lambda functions. EC2 instances are the AWS version of virtual machines in the cloud.

如果您想试用此演示,则需要拥有一个AWS账户,并在您的AWS账户中配置一个或多个EC2实例。 这些是我们将要从Lambda函数操纵的函数。 EC2实例是云中虚拟机的AWS版本。

You can try the demo on AWS Cloud9 IDE (a browser based IDE), as AWS SAM is already configured in that IDE. If you want to know how to use AWS Cloud9 IDE to operate Lambda functions you can check out this video.

您可以在AWS Cloud9 IDE (基于浏览器的IDE)上尝试该演示,因为该IDE中已经配置了AWS SAM。 如果您想知道如何使用AWS Cloud9 IDE操作Lambda函数,可以观看视频

In this example, we are going to start and stop EC2 instances using two different AWS Lambdas that get triggered at a given time. We are starting the instances at 8am everyday and turning them off at 6pm when the day is over.

在此示例中,我们将使用在给定时间触发的两个不同的AWS Lambda启动和停止EC2实例。 我们每天早上8点启动实例,一天结束后在下午6点关闭实例。

For that we are going to use a CloudWatch event to trigger the Lambda at the right time and also the AWS SDK to perform the operations in the instances.

为此,我们将使用CloudWatch事件在正确的时间触发Lambda,并使用AWS开发工具包在实例中执行操作。

The finalised code for this example is available in this GitHub repository. To get this code working in AWS Cloud9 IDE, you need to configure your GitHub account in the IDE to be able to clone the project and then clone it inside the IDE.

该示例的最终代码可在此GitHub存储库中找到。 要使此代码在AWS Cloud9 IDE中工作,您需要在IDE中配置GitHub帐户 ,以便能够克隆项目,然后在IDE内部对其进行克隆。

When you have that ready, just run this command inside the cloned directory:

准备好之后,只需在克隆目录中运行以下命令:

$ sam deploy --guided

When running that command you will get a set of questions that you need to answer in order to configure this project to run successfully.

运行该命令时,您将得到一系列需要回答的问题,以配置该项目成功运行。

The first thing you need to define is a name for your project. Then you'll set the region where it is getting deployed - pick the same where your EC2 instances are. Next we need to give the deploy script a list of the instances that we want to manipulate. And then we are done – it will deploy the project to our AWS account.

您需要定义的第一件事是项目的名称 。 然后,您将设置要部署的区域 -选择与EC2实例相同的区域 。 接下来,我们需要为部署脚本提供我们要操作的实例列表 。 然后我们完成了–它会将项目部署到我们的AWS账户。

定义AWS Lambda函数 (Defining the AWS Lambda function)

The first thing I want to show you is how we define a AWS Lambda function that gets triggered in a specific time using AWS SAM. This definition will be in the file called "template.yml".

我想向您展示的第一件事是我们如何定义一个AWS Lambda函数,该函数在特定时间内使用AWS SAM触发。 该定义将在名为“ template.yml”的文件中。

This is how a function looks. Let's look at the important lines:

这就是函数的外观。 让我们看一下重要的几行:

The first line is the name of the function, in this case “StartInstanceFunction”.

第一行是函数的名称,在本例中为“ StartInstanceFunction ”。

Then we have the “Properties” definition. The first property is the “Handler”. Here we will specify the module (file) where the code that needs to execute is and then the method inside that module.

然后,我们有了“ Properties ”定义。 第一个属性是“ Handler ”。 在这里,我们将指定需要执行代码的模块(文件),然后指定该模块中的方法。

And then we have the “CodeUri”, which is the path that shows you where to find that file. In this case, our code will be inside a directory called “cron” in a file called “handler.js” and in a method called “startInstance”.

然后,我们有了“ CodeUri ”,这是向您显示在哪里找到该文件的路径。 在这种情况下,我们的代码将位于文件“ handler.js”和方法“ startInstance”中的“ cron”目录中。

After that we have the “Runtime” definition. I will be using NodeJS version 12, but you can use Python, Java, Go, C#, or whatever makes you happy. Lambda supports multiple runtimes out of the box and you can bring your own runtime if you want to.

之后,我们有了“ Runtime ”的定义。 我将使用NodeJS版本12,但您可以使用Python,Java,Go,C#或任何令您满意的东西。 Lambda支持开箱即用的多个运行 时,如果需要, 您可以自带运行时

Then we have the “Environment” definition that we will use to define the one environmental variable. This variable will allow us to send to the code dynamically different instances ids, depending on the configuration when we deploy.

然后,我们有了“ Environment ”定义,将用于定义一个环境变量。 此变量将允许我们根据部署时的配置,将不同的实例ID动态发送给代码。

After that we have a section called “Policies” which is where we define the permissions that this particular Lambda function will have.

之后,我们有一个名为“ Policy ”的部分,在其中定义了该特定Lambda函数将拥有的权限。

It is important to know that all Lambda functions are created without any permissions. That means that they cannot do anything on any other AWS resources.

重要的是要知道所有Lambda函数都是在没有任何权限的情况下创建的。 这意味着他们无法在任何其他AWS资源上执行任何操作。

In order for this Lambda function to start an EC2 instance, it needs permissions to do that particular action on that particular AWS resource. In this particular policy we are granting permissions to start ALL EC2 instances in this AWS account. ALL is represented with the “*” in the resources section.

为了使此Lambda函数启动EC2实例,它需要权限才能对特定的AWS资源执行该特定操作。 在此特定策略中,我们授予启动该AWS账户中所有EC2实例的权限。 在资源部分中,所有内容均以“ *”表示。

If you have this piece of code running in production, I recommend that you limit the resources to exactly the ones that you want this Lambda to be able to start.

如果您在生产中运行了这段代码,我建议您将资源限制为您希望此Lambda能够启动的资源。

And finally, the last section is the “Events” section. Here we will define how this Lambda function will get triggered. This function will get triggered with a scheduled CloudWatch event that triggers the Lambda everyday at 8 in the morning. Basically at 8 every day it will turn on all the EC2 instances that you specify.

最后,最后一部分是“ 事件 ”部分。 在这里,我们将定义如何触发此Lambda函数。 此功能将通过计划的CloudWatch事件触发,该事件每天早上8点触发Lambda。 基本上每天8点,它将打开您指定的所有EC2实例。

There are many rules to form this cron expression: for example, to say that you would like this to run only Monday through Friday, write cron(0 8 ? * MON-FRI *). You can find more info in the documentation site of CloudWatch events here: https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html.

形成此cron表达式有很多规则:例如,假设您希望此代码仅在星期一至星期五运行,请编写cron(0 8?* MON-FRI *)。 您可以在以下CloudWatch事件的文档站点中找到更多信息: https ://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html。

编码AWS Lambda函数 (Coding the AWS Lambda function)

Now that we have defined the Lambda function we need to provide some code to it. In the folder “cron”, in the file “handler.js”, we need to add the method called “startInstance” that looks like this:  

现在我们已经定义了Lambda函数,我们需要为其提供一些代码。 在文件夹“ cron”中的文件“ handler.js”中,我们需要添加名为“ startInstance ”的方法,如下所示:

This method will get called when the function is triggered every day at 8 am. It will get the list of EC2 instances from an environmental variable that we passed all the instances ids during deployment time. Then it will create an array of them.

每天上午8点触发函数时,将调用此方法。 它将从我们在部署期间传递所有实例ID的环境变量获取EC2实例列表。 然后它将创建一个数组。

When it has that, it will call the AWS SDK and send the array of instances id as a parameter. And if there is any error it will log it and complete. Immediately after this Lambda finishes execution, you can go to your EC2 console and see how your instances turn on.

如果有,它将调用AWS开发工具包并发送实例ID数组作为参数。 如果有任何错误,它将记录并完成。 Lambda完成执行后,您可以立即转到EC2控制台,查看实例如何打开。

The function to turn off the EC2 instances is very similar with a few differences. You can find the code for that function in this link and check it out.

关闭EC2实例的功能非常相似,但有一些区别。 您可以在此链接中找到该功能的代码并检出。

执行Cron工作 (Running the cron job)

To run this cron job, there is not much left to do. After the two functions are deployed in your AWS account, in the same region as your instances, they will execute and do what they were programmed to do.

要执行此cron工作,没有什么可做的了。 在您的实例所在的区域中将这两个功能部署到您的AWS账户中后,它们将执行并按照编程进行操作。

Now you need to wait until 8am or 6pm to see if they work. Or if you want to test it out right now, change the event time in the Lambda definition to a time that works for you. Make sure that the instance is on if you are planning to turn them off or the other way around, so you can see the changes.

现在,您需要等到上午8点或下午6点才能查看它们是否起作用。 或者,如果您想立即对其进行测试,请将Lambda定义中的事件时间更改为适合您的时间。 如果您打算关闭实例或以其他方式将其关闭,请确保实例处于打开状态,以便可以看到更改。

Now wait and see what happens in the EC2 console. Right after the time you set up, you will see the instance go off or on and then do the opposite at the other time you setup. This will go forever until you remove the Lambda functions.

现在等待,看看在EC2控制台中会发生什么。 在设置时间之后,您会立即看到实例打开或关闭,然后在另一次设置时执行相反的操作。 这将永远存在,直到您删除Lambda函数。

清理您的AWS账户 (Cleaning up your AWS account)

After completing this demo I recommend you that you turn off (or remove the instance you created to test) and remove the Lambda functions you just created.

完成此演示之后,我建议您关闭(或删除创建的要测试的实例)并删除刚刚创建的Lambda函数。

Removing the lambda functions is as easy as going into your CloudFormation service in your AWS management console and removing the stack of resources that AWS SAM created.

删除lambda函数就像在AWS管理控制台中进入CloudFormation服务并删除AWS SAM创建的资源堆栈一样容易。

Also don’t forget to terminate and remove the EC2 instances if you created them for this demo.

另外,如果您为此演示创建了EC2实例,也不要忘记终止并删除它们。

总结一下 (To conclude)

AWS Lambda functions are a very useful tool to perform all kinds of tasks in your AWS account. You can basically get notifications of any changes in the AWS resources through CloudWatch events and then you can access almost all the services using the AWS SDK. So you can perform all kinds of maintenance tasks and automated tasks over your infrastructure.

AWS Lambda函数是在您的AWS账户中执行各种任务的非常有用的工具。 您基本上可以通过CloudWatch事件获取有关AWS资源中任何更改的通知,然后您可以使用AWS开发工具包访问几乎所有服务。 因此,您可以在基础架构上执行各种维护任务和自动化任务。

Thanks for reading.

谢谢阅读。

I’m Marcia Villalba, Developer Advocate for AWS and the host of a youtube channel called FooBar where I have over 250 video tutorials on Serverless, AWS and software engineer practices.

我是AWS开发代言人Marcia Villalba,并且是一个名为FooBar的youtube频道的主持人,在这里我有250多个有关无服务器,AWS和软件工程师实践的视频教程。

翻译自: https://www.freecodecamp.org/news/using-lambda-functions-as-cronjobs/

cron计划任务

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: