How to get MONTHLY cpu metrics using AWS Lambda?

0

Hi, I'm running EC2 instance and every month I get CPU usage when I need to. But from now on, I want get CPU usgage every month using AWS Lambda. and my question is, how to set up a monthly date to get monthly usage of CPU?

appearently this is what I wrote at Lambda to get exact month for CPU usage.

"start": "2022-06-30T15:00:00.000Z",

"end": "2022-07-31T14:59:59.000Z",

And I want to make it Monthly. How should I fix it?

1개 답변
1

Hello,

You can make use of the EventBridge in order to schedule the lambda function to run every month using a cron expression.

[+] https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-cron-expressions

You can configure an event bridge rule that will schedule the lambda function to run every month to obtain the CPU metrics of your EC2 instance. This can be done by following the below steps:

➞ Open the Functions page on the Lambda console and choose the lambda function.

➞ Under Function Overview, click on Add trigger.

➞ Set the trigger type to EventBridge (CloudWatch Events).

➞ For Rule, select Create a new rule, and then provide the rule name and rule description.

➞ For rule type, choose Schedule expression.

➞ In the Schedule expression field, enter a cron expression. For example, to run the function at 8 AM UTC on the first day of every month, you can use cron(0 8 1 * ? *)

[+] https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/scheduled-events-invoking-lambda-run.html

You can find more information on how to write cron expressions in the below document:

[+] https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-cron-expressions

Please note that you would have to configure the lambda function code such that it is able to obtain the CPU usages of the EC2 instance.

AWS
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인