简体   繁体   English

如何使用AWS SDK PHP将触发器添加到AWS Lambda函数?

[英]How do I add trigger to an AWS Lambda function using AWS SDK PHP?

I have an existing AWS Lambda function, I test it on AWS console and it works great. 我有一个现有的AWS Lambda函数,我在AWS控制台上测试它,效果很好。 my application is creating S3 bucket automatically so I need the add dynamically the trigger to the buckets. 我的应用程序是自动创建S3存储桶所以我需要动态添加触发器到存储桶。

I can't find this in the documentation of AWS SDK PHP. 我在AWS SDK PHP的文档中找不到这个。 Please help... 请帮忙...

I don't truly understand your question. 我真的不明白你的问题。 The more details that you provide the better will be the responses. 您提供的详细信息越多,响应就越多。

S3 does not publish an event on bucket creation. S3不会在存储桶创建时发布事件。 S3 does have events for objects inside a bucket (PUT, POST, DELETE, etc but not GET). S3确实有桶内对象的事件(PUT,POST,DELETE等,但不是GET)。

The only service that I can think of that would detect bucket creation is CloudTrail. 我能想到的唯一可以检测存储桶创建的服务是CloudTrail。 You would then have to filter every API event and then call your Lambda on bucket create. 然后,您必须过滤每个API事件,然后在存储桶上调用您的Lambda创建。 Enabling CloudTrail is a good idea and is enabled by default for all new AWS accounts. 启用CloudTrail是个好主意,默认情况下会为所有新AWS账户启用。

You could create an object inside the bucket after creating the bucket. 您可以在创建存储桶后在存储桶内创建对象。 This could trigger your Lambda function. 这可能会触发您的Lambda函数。 However, you would need to filter that also as you would get an event for every object create event. 但是,您需要对每个对象创建事件的事件进行过滤。

Probably the best solution is John Veldboom's answer where your code just calls Lambda after creating the bucket. 可能最好的解决方案是John Veldboom的答案,你的代码在创建存储桶后调用Lambda。

Note: you mention that you are creating buckets dynamically. 注意:您提到您正在动态创建存储桶。 You have a limit of 100 buckets per AWS account (which can be increased). 每个AWS账户限制为100个桶(可以增加)。 I hope your design is not creating a lot of buckets. 我希望你的设计不会产生很多桶。 A better approach is to create folders inside a bucket. 更好的方法是在存储桶中创建文件夹。

To call a Lambda function you'll need to use the invoke method. 要调用Lambda函数,您需要使用invoke方法。

$result = $client->invoke([
    'FunctionName' => 'MyFunctionName', // REQUIRED
    'Payload' => 's3-bucket-name'
]);

The function you're looking for is createEventSourceMapping() . 您正在寻找的函数是createEventSourceMapping()

After you create the S3 bucket, you can then call this function to add a mapping from your new S3 bucket to your Lambda function. 创建S3存储桶后,可以调用此函数以添加从新S3存储桶到Lambda函数的映射。

To help you out with the parameters, try using getEventSourceMapping() or listEventSourceMappings() to see your existing event source mappings to your function. 要帮助您使用这些参数,请尝试使用getEventSourceMapping()listEventSourceMappings()来查看您的函数的现有事件源映射。

It's not clear from your question what you're trying to achieve. 从你的问题中不清楚你想要实现什么。

You can call the createBucket method asynchronously, and then invoke your lambda function with the SDK if there is no specific triggering event for that: 您可以异步调用createBucket方法, 然后如果没有特定的触发事件,则使用SDK invoke lambda函数:

$client->createBucketAsync([/* ... */])->then(
   function($success_response) {
      $client->invoke(
        'FunctionName'   => 'λ_Name',
        'InvocationType' => 'Event',
        'Payload'        => json_encode( $some_data )
   },
   function($error) {
      // something went wrong see $error
   }
);

That would trigger your lambda function after the bucket was successfully created. 这会在成功创建存储桶后触发lambda函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM