创建新的AWS EC2实例时运行AWS Lambda代码 [英] Run AWS Lambda code when creating a new AWS EC2 instance
问题描述
在创建新的EC2实例时,我想使用Lambda运行一些代码.查看蓝图config-rule-change-triggered
,我可以根据各种配置更改来运行代码,但不能在创建代码时运行.有什么方法可以做我想要的吗?还是我误解了Lambda的用例?
I'd like to run some code using Lambda on the event that I create a new EC2 instance. Looking the blueprint config-rule-change-triggered
I have the ability to run code depending on various configuration changes, but not when one is created. Is there a way to do what I want? Or have I misunderstood the use case of Lambda?
推荐答案
您可以通过在EC2实例启动用户数据中插入代码并让该代码显式调用Lambda函数来完成此操作,但这并不是最好的方法.
You could do this by inserting code into your EC2 instance launch userdata and have that code explicitly invoke a Lambda function, but that's not the best way to do it.
更好的方法是结合使用CloudTrail和Lambda.如果您启用CloudTrail日志记录(每个时间,每个区域的所有区域均应启用此功能),则CloudTrail会将您帐户中进行的所有API调用记录到S3.然后,通过配置S3将事件发布到Lambda,将其连接到Lambda.您的Lambda函数将收到一个S3事件,然后可以检索API日志,查找RunInstances API调用,然后根据新实例的启动执行所需的任何工作.
A better way is to use a combination of CloudTrail and Lambda. If you enable CloudTrail logging (every a/c should have this enabled, all the time, in all regions) then CloudTrail will log to S3 all of the API calls made in your account. You then connect this to Lambda by configuring S3 to publish events to Lambda. Your Lambda function will receive an S3 event, can then retrieve the API logs, find RunInstances API calls, and then do whatever work you need to as a consequence of the new instance being launched.
这篇关于创建新的AWS EC2实例时运行AWS Lambda代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!