0
votes

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

1
no answers :[ I opened up a feature request for this github.com/aws/aws-cdk/issues/12522 feel free to bump!baumannalexj

1 Answers

0
votes

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)
        });