Repository navigation
Description
What is the problem?
Whenever a Lambda is added as a target to an event.Rule the function.configureAsyncInvoke() function needs to be called.
If select props (retryAttempts, maxEventAge, etc.) are included during Function creation, then an AWS::Lambda::EventInvokeConfig resource is created. If those same props are added during a rule.addTarget() call through events-targets.LambdaFunctionProps, then the function.configureAsyncInvoke() function is never called resulting in the AWS::Lambda::EventInvokeConfig resource not being created.
Reproduction Steps
import { Stack, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as lambda from 'aws-cdk-lib/aws-lambda';
const defaultCode = `exports.handler = async function (event, context, callback) {
try {
const data = {
statusCode: 201,
};
return data;
} catch (uncaughtError) {
console.error(uncaughtError);
throw uncaughtError;
}
}`;
export class DemoStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
const maxEventAgeInMinutes = 3;
const retryAttempts = 2;
new lambda.Function(
this,
`${id}-function-with`,
{
code: lambda.Code.fromInline(defaultCode),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
maxEventAge: Duration.minutes(maxEventAgeInMinutes),
retryAttempts: retryAttempts,
},
);
const lambdaFunctionWithoutEventInvokeConfig = new lambda.Function(
this,
`${id}-function-without`,
{
code: lambda.Code.fromInline(defaultCode),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
},
);
const rule = new events.Rule(this, `${id}-rule`, {
schedule: events.Schedule.rate(Duration.minutes(5)),
});
rule.addTarget(
new targets.LambdaFunction(
lambdaFunctionWithoutEventInvokeConfig,
{
maxEventAge: Duration.minutes(maxEventAgeInMinutes),
retryAttempts: retryAttempts,
},
),
);
}
}What did you expect to happen?
I expect a AWS::Lambda::EventInvokeConfig resource to be created whenever rule.addTarget() is called.
What actually happened?
No AWS::Lambda::EventInvokeConfig resource is created.
CDK CLI Version
2.17.0 (build f9cd009)
Framework Version
No response
Node.js Version
v16.13.1
OS
MacOS 11.6.4
Language
Typescript
Language Version
TypeScript (Version 4.5.2)
Other information
No response
Activity
peterwoodworth commentedon Mar 29, 2022
I don't think this is a bug - this seems out of the scope of what the
eventsTargets.LambdaFunctionconstruct is built for (adding targets for your events)The props (with the same names) that you're passing in to this construct are for configuring your event rule resource. If you want to configure lambda infrastructure, you'll have to create an
EventInvokeConfigwith a reference to your function, or simply pass in the necessary props to your function on creation if possible.thomasstep commentedon Mar 31, 2022
@peterwoodworth I appreciate the response. That flow wasn't clear to me from the documentation, but I can handle that. Thank you!
peterwoodworth commentedon Mar 31, 2022
Do you have any specific suggestions on how we could improve clarity here @thomasstep? I'm not sure exactly where you got tripped up