3

In my Lambda CDK stack, I want to setup an event rule to send an event to my Lambda every 10 minutes.

This works and deploys the rule with the lambda as the target

    // ... setup lambdaFunction construct...
    // -- EVENTBRIDGE RULES --

    const meetingSyncEvent = {
        path: "/v1/meeting/sync",
        httpMethod: "GET"
    };

    const eventTarget = new awsEventTarget.LambdaFunction(lambdaFunction, {
        event: awsEvents.RuleTargetInput.fromObject(meetingSyncEvent)
    });



    /**
     TODO how do I add target to ARN with alias/version
     * */

    const eventRule = new awsEvents.Rule(this, generateConstructName("event-rule"), {
        enabled: true,
        description: "event to invoke GET /meeting/sync",
        ruleName: "meeting-sync-rule",
        targets : [
             eventTarget
        ],
        schedule: awsEvents.Schedule.rate(EVENT_MEETING_SYNC_RATE)
    });


    // -- end EVENTBRIDGE RULES --

The problem is this only targets the base ARN without an Alias or version (effectively always pointing to $Latest). There is this option in AWS console to set an Alias or version for a target (pics below), how can I do this in the CDK?

aws console UI allows alias and version for target target arn has alias when configured through UI

baumannalexj
  • 766
  • 1
  • 9
  • 20
  • no answers :[ I opened up a feature request for this https://github.com/aws/aws-cdk/issues/12522 feel free to bump! – baumannalexj Jan 15 '21 at 17:57

1 Answers1

2

I found it: the event rule takes type IFunction, and since IAlias and IVersion both extend IFunction, so we can take the alias we created for our function, and provide the alias as the function param (AWS differentiates between functions, alias-functions, and version-functions)

        const lambdaAlias =  lambdaFunction.latestVersion
                                           .addAlias(ENVIRONMENT_UPPERCASE)


        const eventTarget = new awsEventsTargets.LambdaFunction(lambdaAlias, {
            event: awsEvents.RuleTargetInput.fromObject(meetingSyncEvent)
        });

        const eventRule = new awsEvents.Rule(this, "event-rule", {
            enabled: true,
            description: `event to invoke GET /meeting/sync for ${FUNCTION_NAME}`,
            ruleName: `${FUNCTION_NAME}-invoke-meetingsync`,
            targets: [
                eventTarget
            ],
            schedule: awsEvents.Schedule.rate(EVENT_MEETING_SYNC_RATE)
        });

baumannalexj
  • 766
  • 1
  • 9
  • 20
  • Could you confirm if the above extends IAM role attached to this lambda to allow ‘events.amazonaws.com’ to invoke the Lambda function? – demsey May 29 '21 at 11:16
  • This event invoked my lambda, did this not work for you? I did not create a custom role for this lambda though, so if you assigned a custom role to the Lambda, you may need to import that role and inject into the awsEvents.Rule constructor? Can you try like the above and let us know? – baumannalexj May 30 '21 at 22:22
  • @demsey - it's not the IAM role of the Lambda function that should be updated, it should be the function resource policy. The difference is that the role gives Lambda access to perform actions, whereas the resource policy allows other resources (such as EventBridge in this case) to access/invoke the function. – Sidharth Ramesh May 11 '23 at 11:09