|
| 1 | +import { Stack, StackProps, custom_resources, aws_iam } from 'aws-cdk-lib'; |
| 2 | +import { Events } from '@aws-lambda-powertools/commons'; |
| 3 | +import { Construct } from 'constructs'; |
| 4 | +import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 5 | +import { Tracing } from 'aws-cdk-lib/aws-lambda'; |
| 6 | + |
| 7 | +export class CdkAppStack extends Stack { |
| 8 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 9 | + super(scope, id, props); |
| 10 | + |
| 11 | + const myFunctionWithStandardFunctions = new lambda.NodejsFunction(this, 'MyFunction', { tracing: Tracing.ACTIVE }); |
| 12 | + const myFunctionWithDecorator = new lambda.NodejsFunction(this, 'MyFunctionWithDecorator', { |
| 13 | + tracing: Tracing.ACTIVE, |
| 14 | + }); |
| 15 | + const myFunctionWithWithMiddleware = new lambda.NodejsFunction(this, 'MyFunctionWithMiddy', { |
| 16 | + tracing: Tracing.ACTIVE, |
| 17 | + }); |
| 18 | + |
| 19 | + // Invoke all functions twice |
| 20 | + for (let i = 0; i < 2; i++) { |
| 21 | + new custom_resources.AwsCustomResource(this, `Invoke-std-func-${i}`, { |
| 22 | + onUpdate: { |
| 23 | + service: 'Lambda', |
| 24 | + action: 'invoke', |
| 25 | + physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), |
| 26 | + parameters: { |
| 27 | + FunctionName: myFunctionWithStandardFunctions.functionName, |
| 28 | + InvocationType: 'RequestResponse', |
| 29 | + Payload: JSON.stringify(Events.Custom.CustomEvent), |
| 30 | + } |
| 31 | + }, |
| 32 | + policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ |
| 33 | + new aws_iam.PolicyStatement({ |
| 34 | + effect: aws_iam.Effect.ALLOW, |
| 35 | + resources: [ |
| 36 | + myFunctionWithStandardFunctions.functionArn, |
| 37 | + ], |
| 38 | + actions: ['lambda:InvokeFunction'], |
| 39 | + }), |
| 40 | + ]), |
| 41 | + }); |
| 42 | + new custom_resources.AwsCustomResource(this, `Invoke-dec-func-${i}`, { |
| 43 | + onUpdate: { |
| 44 | + service: 'Lambda', |
| 45 | + action: 'invoke', |
| 46 | + physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), |
| 47 | + parameters: { |
| 48 | + FunctionName: myFunctionWithDecorator.functionName, |
| 49 | + InvocationType: 'RequestResponse', |
| 50 | + Payload: JSON.stringify(Events.Custom.CustomEvent), |
| 51 | + } |
| 52 | + }, |
| 53 | + policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ |
| 54 | + new aws_iam.PolicyStatement({ |
| 55 | + effect: aws_iam.Effect.ALLOW, |
| 56 | + resources: [ |
| 57 | + myFunctionWithDecorator.functionArn, |
| 58 | + ], |
| 59 | + actions: ['lambda:InvokeFunction'], |
| 60 | + }), |
| 61 | + ]), |
| 62 | + }); |
| 63 | + new custom_resources.AwsCustomResource(this, `Invoke-middy-func-${i}`, { |
| 64 | + onUpdate: { |
| 65 | + service: 'Lambda', |
| 66 | + action: 'invoke', |
| 67 | + physicalResourceId: custom_resources.PhysicalResourceId.of(new Date().toISOString()), |
| 68 | + parameters: { |
| 69 | + FunctionName: myFunctionWithWithMiddleware.functionName, |
| 70 | + InvocationType: 'RequestResponse', |
| 71 | + Payload: JSON.stringify(Events.Custom.CustomEvent), |
| 72 | + } |
| 73 | + }, |
| 74 | + policy: custom_resources.AwsCustomResourcePolicy.fromStatements([ |
| 75 | + new aws_iam.PolicyStatement({ |
| 76 | + effect: aws_iam.Effect.ALLOW, |
| 77 | + resources: [ |
| 78 | + myFunctionWithWithMiddleware.functionArn, |
| 79 | + ], |
| 80 | + actions: ['lambda:InvokeFunction'], |
| 81 | + }), |
| 82 | + ]), |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments