Skip to main content

cdk-lambda-subminute

Project description

cdk-lambda-subminute

This construct creates a state machine that can invoke a Lambda function per time unit which can be less than one minute. You only need to craft a Lambda function and then assign it as an argument into the construct. An example is included. License Build Release Python pip npm version pypi evrsion Maven nuget

Serverless Architecture

image

Introduction

This construct library is reffered to thie AWS Architecture blog post, (A serverless solution for invoking AWS Lambda at a sub-minute frequency)[https://aws.amazon.com/tw/blogs/architecture/a-serverless-solution-for-invoking-aws-lambda-at-a-sub-minute-frequency/], written by Emanuele 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.

Example

Typescript

$ cdk --init language typescript
$ yarn add cdk-lambda-subminute
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
class TypescriptStack(cdk.Stack):
    def __init__(self, scope, id, props=None):
        super().__init__(scope, id, props)

        target_labmda = Function(self, "targetFunction",
            code=Code.from_inline("exports.handler = function(event, ctx, cb) { return cb(null, \"hi\"); })"), # It's just a simple function for demonstration purpose only.
            function_name="testTargetFunction",
            runtime=Runtime.NODEJS_12_X,
            handler="index.handler"
        )
        cron_job_example = "cron(50/1 15-17 ? * SUN-SAT *)"
        subminute_master = LambdaSubminute(self, "LambdaSubminute", target_function=target_labmda, conjob_expression=cron_job_example)

        cdk.CfnOutput(self, "OStateMachineArn", value=subminute_master.state_machine_arn)
        cdk.CfnOutput(self, "OIteratorFunctionArn", value=subminute_master.iterator_function.function_arn)

app = cdk.App()
TypescriptStack(app, "TypescriptStack")

Python

Java

C#

Statemachine Diagram

image

Known issue

Originally, I utilized PythonFuncion in the module of @aws-cdk/aws-lambda-python to 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)
Bundling did not produce any output. Check that content is written to /asset-output.

      64 |     }));
      65 |
    > 66 |     this.function = new PythonFunction(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,

      at AssetStaging.bundle (node_modules/@aws-cdk/core/lib/asset-staging.ts:484:13)
      at AssetStaging.stageByBundling (node_modules/@aws-cdk/core/lib/asset-staging.ts:328:10)
      at stageThisAsset (node_modules/@aws-cdk/core/lib/asset-staging.ts:194:35)
      at Cache.obtain (node_modules/@aws-cdk/core/lib/private/cache.ts:24:13)
      at new AssetStaging (node_modules/@aws-cdk/core/lib/asset-staging.ts:219:44)
      at new Asset (node_modules/@aws-cdk/aws-s3-assets/lib/asset.ts:127:21)
      at AssetCode.bind (node_modules/@aws-cdk/aws-lambda/lib/code.ts:277:20)
      at new Function (node_modules/@aws-cdk/aws-lambda/lib/function.ts:583:29)
      at new PythonFunction (node_modules/@aws-cdk/aws-lambda-python/lib/function.ts:106:5)
      at new IteratorLambda (src/cdk-lambda-subminute.ts:66:21)
      at new LambdaSubminute (src/cdk-lambda-subminute.ts:25:22)
      at Object.<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 thoughs or just post the issue onto the CDK Github repo.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk_lambda_subminute-0.1.4.tar.gz (138.7 kB view hashes)

Uploaded Source

Built Distribution

cdk_lambda_subminute-0.1.4-py3-none-any.whl (137.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page