如何配置Amazon CDK在部署后触发Lambda函数?

huangapple go评论82阅读模式
英文:

How can I configure Amazon CDK to trigger a Lambda function after deployment?

问题

我已经使用Amazon CDK部署了一个Lambda函数。我想每次部署时自动调用这个Lambda函数。是否可以使用Amazon CDK构建来实现这一点?

英文:

I have deployed a Lambda function using Amazon CDK. I would like to invoke this Lambda function automatically every time it is deployed. Is it possible to achieve this using the Amazon CDK construct?

答案1

得分: 2

以下是已翻译的内容:

"The canonical way to do this is using CDK triggers, but as @ipbearden correctly points out in the comments, the functionality to run a trigger on every deploy has not been added yet. You can use a hack to always recreate the Trigger on every deploy:

  1. import * as triggers from 'aws-cdk-lib/triggers';
  2. const func: lambda.Function;
  3. new triggers.Trigger(this, 'MyTrigger-' + Date.now().toString(), {
  4. handler: func,
  5. });

You can even have it execute after (or before) the deployment of a specific construct.

英文:

The canonical way to do this is using CDK triggers, but as @ipbearden correctly points out in the comments, the functionality to run a trigger on every deploy has not been added yet. You can use a hack to always recreate the Trigger on every deploy:

  1. import * as triggers from 'aws-cdk-lib/triggers';
  2. const func: lambda.Function;
  3. new triggers.Trigger(this, 'MyTrigger-' + Date.now().toString(), {
  4. handler: func,
  5. });

You can even have it execute after (or before) the deployment of a specific construct.

答案2

得分: 1

以下是已翻译的内容:

  1. 应该可以通过类似以下的CustomResource 来实现:
  2. ```typescript
  3. const lambdaTrigger = new cr.AwsCustomResource(this, 'MyFunctionTrigger', {
  4. policy: cr.AwsCustomResourcePolicy.fromStatements([
  5. new iam.PolicyStatement({
  6. actions: ['lambda:InvokeFunction'],
  7. effect: iam.Effect.ALLOW,
  8. resources: [myFunction.functionArn],
  9. }),
  10. ]),
  11. timeout: Duration.minutes(2),
  12. onCreate: {
  13. service: 'Lambda',
  14. action: 'invoke',
  15. parameters: {
  16. FunctionName: myFunction.functionName,
  17. InvocationType: 'Event',
  18. },
  19. physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
  20. },
  21. onUpdate: {
  22. service: 'Lambda',
  23. action: 'invoke',
  24. parameters: {
  25. FunctionName: myFunction.functionName,
  26. InvocationType: 'Event'
  27. },
  28. physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString())
  29. }
  30. });

通过在部署时将physicalResourceId设置为当前时间,应该会触发每次部署时执行它。

  1. <details>
  2. <summary>英文:</summary>
  3. You should be able to do this with a CustomResource similar to below:
  4. ```typescript
  5. const lambdaTrigger = new cr.AwsCustomResource(this, &#39;MyFunctionTrigger&#39;, {
  6. policy: cr.AwsCustomResourcePolicy.fromStatements([
  7. new iam.PolicyStatement({
  8. actions: [&#39;lambda:InvokeFunction&#39;],
  9. effect: iam.Effect.ALLOW,
  10. resources: [myFunction.functionArn],
  11. }),
  12. ]),
  13. timeout: Duration.minutes(2),
  14. onCreate: {
  15. service: &#39;Lambda&#39;,
  16. action: &#39;invoke&#39;,
  17. parameters: {
  18. FunctionName: myFunction.functionName,
  19. InvocationType: &#39;Event&#39;,
  20. },
  21. physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
  22. },
  23. onUpdate: {
  24. service: &#39;Lambda&#39;,
  25. action: &#39;invoke&#39;,
  26. parameters: {
  27. FunctionName: myFunction.functionName,
  28. InvocationType: &#39;Event&#39;
  29. },
  30. physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString())
  31. }
  32. });

By setting the physicalResourceId to the current time on deploy it should trigger it to execute each deployment.

huangapple
  • 本文由 发表于 2023年7月11日 03:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定