I'm playing around with the AWS CDK (Typescript) for some self-learning. Specifically, I'd like to have a lambda function execute daily at a specific time or say at every N minutes. Especially since I think I'd like to have many such functions, it probably wouldn't be a bad idea to use a construct that encapsulates a lambda function, an EventBridge rule for it, and perhaps some logging etc. utilities that go with it.
Instead of writing my own construct, I realized there's aws-eventbridge-lambda so I gave it a try. In my project, I have a lambda folder in the root, which contains hello.py which has an extremely simple lambda_handler definition. Then, my stack is the following:
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { EventbridgeToLambdaProps, EventbridgeToLambda } from '@aws-solutions-constructs/aws-eventbridge-lambda';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as events from 'aws-cdk-lib/aws-events';
export class TimedLambdaStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const constructProps: EventbridgeToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.PYTHON_3_9,
handler: 'hello.lambda_handler'
},
eventRuleProps: {
schedule: events.Schedule.rate(Duration.minutes(5))
}
};
new EventbridgeToLambda(this, 'test-eventbridge-lambda', constructProps);
}
}
My cdk deploys fine. I'm doing it via a pipeline which is essentially copied from the CDK Workshop:
import * as cdk from 'aws-cdk-lib';
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import { Construct } from 'constructs';
import {CodeBuildStep, CodePipeline, CodePipelineSource} from "aws-cdk-lib/pipelines";
export class TimedPipelineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const repo = new codecommit.Repository(this, 'TimedRepo', {
repositoryName: "TimedRepo"
});
const pipeline = new CodePipeline(this, 'Pipeline', {
pipelineName: 'TimedLambdaPipeline',
synth: new CodeBuildStep('SynthStep', {
input: CodePipelineSource.codeCommit(repo, 'master'),
installCommands: [
'npm install -g aws-cdk'
#'npm install -s @aws-solutions-constructs/aws-eventbridge-lambda'
],
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
]
}
)
});
}
}
Looking into the resulting CloudFormation stack, I'm confused. I see there's an EventBridge rule but I see no Lambda function provisioned.
Am I misunderstanding what the construct is for, or doing something silly?