I've been experimenting with the AWS CDK (Typescript) to enhance my knowledge. I'm interested in setting up a lambda function to run daily at a specific time or at intervals of N minutes. Since I anticipate having multiple functions, it would be beneficial to use a construct that bundles a lambda function, an EventBridge rule, and other necessary utilities.
Instead of creating my own construct, I discovered aws-eventbridge-lambda and decided to give it a try. In my project, I have a lambda
directory containing a simple hello.py
file with a basic lambda_handler
definition. My stack setup is as follows:
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 deployment process is working fine through a pipeline that I set up following instructions 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'
]
}
)
});
}
}
After examining the resulting CloudFormation stack, I am puzzled. Although I see an EventBridge rule, there seems to be no provision for the Lambda function.
Could it be that I have misconceived the purpose of the construct, or perhaps made an oversight somewhere?