Package: cdk-lambda-subminute
cdk-lambda-subminutenpm (JS/TS)PyPI (Python)Maven (Java)GoNuGetLinkLinkLinkLinkLinkThis construct creates a state machine that can invoke a Lambda function per time unit which can be less than one minute, such as invoking every 10 seconds. You only need to craft a Lambda function and then assign it as an argument into the construct. An example is included.Serverless ArchitectureIntroductionThis construct library is reffered to thie AWS Architecture blog post,A serverless solution for invoking AWS Lambda at a sub-minute frequency, written byEmanuele Menga. I made it as a constrcut library where you only need to care about a target Lambda function, how frequent and how long you want to execute.ExampleTypescriptYou could also refer tohere.$cdk--initlanguagetypescript
$yarnaddcdk-lambda-subminuteclassTypescriptStackextendscdk.Stack{constructor(scope:cdk.Construct,id:string,props?:cdk.StackProps){super(scope,id,props);consttargetLabmda=newFunction(this,'targetFunction',{code:Code.fromInline('exports.handler = function(event, ctx, cb) { return cb(null, "hi"); })'),//It's just a simple function for demonstration purpose only.functionName:'testTargetFunction',runtime:Runtime.NODEJS_18_X,handler:'index.handler',});constcronJobExample='cron(50/1 15-17 ? * SUN-SAT *)';constsubminuteMaster=newLambdaSubminute(this,'LambdaSubminute',{targetFunction:targetLabmda,cronjobExpression:cronJobExample});newcdk.CfnOutput(this,'OStateMachineArn',{value:subminuteMaster.stateMachineArn});newcdk.CfnOutput(this,'OIteratorFunctionArn',{value:subminuteMaster.iteratorFunction.functionArn});}}constapp=newcdk.App();newTypescriptStack(app,'TypescriptStack',{});PythonYou could also refer tohere.# upgrading related Python packages$python-mensurepip--upgrade
$python-mpipinstall--upgradepip
$python-mpipinstall--upgradevirtualenv# initialize a CDK Python project$cdkinit--languagepython# make packages installed locally instead of globally$source.venv/bin/activate
$cat<<EOL > requirements.txtaws-cdk.coreaws-cdk.aws-lambdacdk-lambda-subminuteEOL$python-mpipinstall-rrequirements.txtfromaws_cdkimportcoreascdkfromaws_cdk.aws_lambdaimportCode,Function,Runtimefromcdk_lambda_subminuteimportLambdaSubminuteclassPythonStack(cdk.Stack):def__init__(self,scope:cdk.Construct,construct_id:str,**kwargs)->None:super().__init__(scope,construct_id,**kwargs)target_lambda=Function(self,"targetFunction",code=Code.from_inline("exports.handler = function(event, ctx, cb) { return cb(null,\"hi\"); })"),function_name="testTargetFunction",runtime=Runtime.NODEJS_18_X,handler="index.handler")cron_job_example="cron(10/1 4-5 ? * SUN-SAT *)"subminute_master=LambdaSubminute(self,"LambdaSubminute",target_function=target_lambda,cronjob_expression=cron_job_example,frequency=7,interval_time=8)cdk.CfnOutput(self,"OStateMachineArn",value=subminute_master.state_machine_arn)cdk.CfnOutput(self,"OIteratorFunctionArn",value=subminute_master.iterator_function.function_arn)$deactivateJavaYou could also refer tohere.$cdkinit--languagejava
$mvnpackage.
.<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><cdk.version>2.87.0</cdk.version><constrcut.verion>2.0.201</constrcut.verion><junit.version>5.7.1</junit.version></properties>..<dependencies><!-- AWS Cloud Development Kit --><dependency><groupId>software.amazon.awscdk</groupId><artifactId>core</artifactId><version>${cdk.version}</version></dependency><dependency><groupId>software.amazon.awscdk</groupId><artifactId>lambda</artifactId><version>${cdk.version}</version></dependency><dependency><groupId>io.github.hsiehshujeng</groupId><artifactId>cdk-lambda-subminute</artifactId><version>${constrcut.verion}</version></dependency>...</dependencies>packagecom.myorg;importsoftware.amazon.awscdk.core.CfnOutput;importsoftware.amazon.awscdk.core.CfnOutputProps;importsoftware.amazon.awscdk.core.Construct;importsoftware.amazon.awscdk.core.Stack;importsoftware.amazon.awscdk.core.StackProps;importsoftware.amazon.awscdk.services.lambda.Code;importsoftware.amazon.awscdk.services.lambda.Function;importsoftware.amazon.awscdk.services.lambda.FunctionProps;importsoftware.amazon.awscdk.services.lambda.Runtime;importio.github.hsiehshujeng.cdk.lambda.subminute.LambdaSubminute;importio.github.hsiehshujeng.cdk.lambda.subminute.LambdaSubminuteProps;publicclassJavaStackextendsStack{publicJavaStack(finalConstructscope,finalStringid){this(scope,id,null);}publicJavaStack(finalConstructscope,finalStringid,finalStackPropsprops){super(scope,id,props);FunctiontargetLambda=newFunction(this,"targetFunction",FunctionProps.builder().code(Code.fromInline("exports.handler = function(event, ctx, cb) { return cb(null, \"hi\"); })")).functionName("estTargetFunction").runtime(Runtime.NODEJS_18_X).handler("index.handler").build());StringcronJobExample="cron(50/1 4-5 ? * SUN-SAT *)";LambdaSubminutesubminuteMaster=newLambdaSubminute(this,"LambdaSubminute",LambdaSubminuteProps.builder().targetFunction(targetLambda).cronjobExpression(cronJobExample).frequency(6).intervalTime(9).build());newCfnOutput(this,"OStateMachineArn",CfnOutputProps.builder().value(subminuteMaster.getStateMachineArn()).build());newCfnOutput(this,"OIteratorFunctionArn",CfnOutputProps.builder().value(subminuteMaster.getIteratorFunction().getFunctionName()).build());}}C#You could also refer tohere.$cdkinit--languagecsharp
$dotnetaddsrc/CsharppackageAmazon.CDK.AWS.Lambda
$dotnetaddsrc/CsharppackageLambda.Subminute--version2.0.201usingAmazon.CDK;usingAmazon.CDK.AWS.Lambda;usingScottHsieh.Cdk;namespaceCsharp{publicclassCsharpStack:Stack{internalCsharpStack(Constructscope,stringid,IStackPropsprops=null):base(scope,id,props){vartargetLambda=newFunction(this,"targetFunction",newFunctionProps{Code=Code.FromInline("exports.handler = function(event, ctx, cb) { return cb(null, \"hi\"); })"),FunctionName="testTargetFunction",Runtime=Runtime.NODEJS_18_X,Handler="index.handler"});stringcronJobExample="cron(50/1 6-7 ? * SUN-SAT *)";varsubminuteMaster=newLambdaSubminute(this,"LambdaSubminute",newLambdaSubminuteProps{TargetFunction=targetLambda,CronjobExpression=cronJobExample,Frequency=10,IntervalTime=6,});newCfnOutput(this,"OStateMachineArn",newCfnOutputProps{Value=subminuteMaster.StateMachineArn});newCfnOutput(this,"OIteratorFunctionArn",newCfnOutputProps{Value=subminuteMaster.IteratorFunction.FunctionArn});}}}GO# Initialize a new AWS CDK application in the current directory with the Go programming languagecdkinitapp-lgo# Add this custom CDK construct to your projectgogetgithub.com/HsiehShuJeng/cdk-lambda-subminute-go/cdklambdasubminute/v2@v2.0.226# Ensure all dependencies are properly listed in the go.mod file and remove any unused onesgomodtidy# Upgrade all Go modules in your project to their latest minor or patch versionsgoget-u./...Statemachine DiagramKnown issueOriginally, I utilizedPythonFuncionin the module of@aws-cdk/aws-lambda-pythonto build the iterator Lambda function. Every thing works fine, including test, on my local machine (MacBook Pro M1), until it comes to the CI in Github Actions, it awlays gave me the following message:## cdk version: 1.105.0 (build 4813992)Bundlingdidnotproduceanyoutput.Checkthatcontentiswrittento/asset-output.64|}));65|>66|this.function=newPythonFunction(this,'Iterator',{|^67|functionName:'lambda-subminute-iterator',68|description:'A function for breaking the limit of 1 minute with the CloudWatch Rules.',69|logRetention:RetentionDays.THREE_MONTHS,atAssetStaging.bundle(node_modules/@aws-cdk/core/lib/asset-staging.ts:484:13)atAssetStaging.stageByBundling(node_modules/@aws-cdk/core/lib/asset-staging.ts:328:10)atstageThisAsset(node_modules/@aws-cdk/core/lib/asset-staging.ts:194:35)atCache.obtain(node_modules/@aws-cdk/core/lib/private/cache.ts:24:13)atnewAssetStaging(node_modules/@aws-cdk/core/lib/asset-staging.ts:219:44)atnewAsset(node_modules/@aws-cdk/aws-s3-assets/lib/asset.ts:127:21)atAssetCode.bind(node_modules/@aws-cdk/aws-lambda/lib/code.ts:277:20)atnewFunction(node_modules/@aws-cdk/aws-lambda/lib/function.ts:583:29)atnewPythonFunction(node_modules/@aws-cdk/aws-lambda-python/lib/function.ts:106:5)atnewIteratorLambda(src/cdk-lambda-subminute.ts:66:21)atnewLambdaSubminute(src/cdk-lambda-subminute.ts:25:22)atObject.<anonymous>.test(test/integ.test.ts:23:3)I actually have tried many different methods according to the following threads but to no avail.  I'll attempt to test some thoughts or just post the issue onto the CDK Github repo.Asset BundlingChange the bundler's /asset-output local volume mount location #8589(aws-lambda-python: PythonFunction): unable to use bundling in BitBucket #14156BundlingDockerImage.cp() needs to be explained more in the README #11914