英文:
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, '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())
}
});
By setting the physicalResourceId
to the current time on deploy it should trigger it to execute each deployment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论