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

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

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:

import * as triggers from 'aws-cdk-lib/triggers';

const func: lambda.Function;

new triggers.Trigger(this, 'MyTrigger-' + Date.now().toString(), {
  handler: func,
});

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:

import * as triggers from 'aws-cdk-lib/triggers';

const func: lambda.Function;

new triggers.Trigger(this, 'MyTrigger-' + Date.now().toString(), {
  handler: func,
});

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

答案2

得分: 1

以下是已翻译的内容:

应该可以通过类似以下的CustomResource 来实现:

```typescript
const lambdaTrigger = new cr.AwsCustomResource(this, 'MyFunctionTrigger', {
  policy: cr.AwsCustomResourcePolicy.fromStatements([
    new iam.PolicyStatement({
      actions: ['lambda:InvokeFunction'],
      effect: iam.Effect.ALLOW,
      resources: [myFunction.functionArn],
    }),
  ]),
  timeout: Duration.minutes(2),
  onCreate: {
    service: 'Lambda',
    action: 'invoke',
    parameters: {
      FunctionName: myFunction.functionName,
      InvocationType: 'Event',
    },
    physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
  },
  onUpdate: {
    service: 'Lambda',
    action: 'invoke',
    parameters: {
      FunctionName: myFunction.functionName,
      InvocationType: 'Event'
    },
    physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString())
  }
});

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


<details>
<summary>英文:</summary>

You should be able to do this with a CustomResource similar to below:

```typescript
const lambdaTrigger = new cr.AwsCustomResource(this, &#39;MyFunctionTrigger&#39;, {
  policy: cr.AwsCustomResourcePolicy.fromStatements([
    new iam.PolicyStatement({
      actions: [&#39;lambda:InvokeFunction&#39;],
      effect: iam.Effect.ALLOW,
      resources: [myFunction.functionArn],
    }),
  ]),
  timeout: Duration.minutes(2),
  onCreate: {
    service: &#39;Lambda&#39;,
    action: &#39;invoke&#39;,
    parameters: {
      FunctionName: myFunction.functionName,
      InvocationType: &#39;Event&#39;,
    },
    physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString()),
  },
  onUpdate: {
    service: &#39;Lambda&#39;,
    action: &#39;invoke&#39;,
    parameters: {
      FunctionName: myFunction.functionName,
      InvocationType: &#39;Event&#39;
    },
    physicalResourceId: cr.PhysicalResourceId.of(Date.now().toString())
  }
});

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:

确定