npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ixor/aws-cdk-ixor-lambda

v1.3.0-rc-11

Published

## Overview

Downloads

105

Readme

@ixor/aws-cdk-ixor-lambda module

Overview

+----+ Ixor Lambda Construct+----------------------------------+
|                                                              |
| +--------------------+   +---------------------------------+ |
| |                    |   |                                 | |
| | CW Event Rule      |   | Lambda Function                 | |
| |                    |   |                                 | |
| |                    |   | if (event.dummy) {              | |
| |{                   |   |   sleep(event.dummySleepSeconds | |
| |  "count": 2,       |   |   return({})                    | |
| |  "lambdaArn": "arn"|   | }                               | |
| |}                   |   | ...                             | |
| |                    |   |                                 | |
| +---------------+----+   +---^-----------------------------+ |
|                 |            |                               |
+--------------------------------------------------------------+
                  |            +
                  |         count times
                  |            |
                  |            |
            +-----v------------+---------+
            |                            |
            |  keepWarmLambdaFunction    |
            |                            |
            |  {                         |
            |    "dummy": "dummy",       |
            |    "dummySleepSeconds": 1  |
            |  }                         |
            +----------------------------+

This is a aws-cdk construct that extends the default Lambda Function class with keepWarm functionality.

The Function class can have 4 optional properties that configure the keepWarm behaviour of the function:

interface IxorLambdaFunctionProps extends lambda.FunctionProps {
      keepWarm?: boolean,
      keepWarmEventSchedule?: events.Schedule,
      keepWarmLambdaFunction?: lambda.IFunction,
      keepWarmCount?: number
}

When keepWarm is true, the keepWarmLambdaFunction is required.

keepWarmCount defaults to 2 is not specified.

keepWarmEventSchedule defaults to business hours.

Prerequisite: the keep warm lambda function

This function should already exist in the AWS account. It is triggered by the CloudWatch rule with an event that looks like this:

{
  "count": 2,
  "lambdaArn": "arn-of-the-lambda-function-to-keep-warm"
}

The function should have invoke permissions on all Lambda's to keep warm, and it will asynchonously invoke the function in lambdaArn count times.

This is an implementation of the keep warm Lambda, it is also available on npm.

import json
import logging
import boto3
import os

logger = logging.getLogger()
logger.setLevel(logging.INFO)

client = boto3.client("lambda")

dummy_sleep_seconds = os.getenv('DUMMY_SLEEP_SECONDS', '1')

def lambda_handler(event, context):
    logger.info(json.dumps(event))
    count = event.get('count', 3)
    lambda_arn = event.get('lambdaArn')
    logger.debug(f"Lambda to warm: {lambda_arn}")
    logger.debug(f"How many? {count}")
    logger.info(f"Asynchronously starting {count} target lambdas")

    success_count = 0

    for i in range(count):
        logger.debug(f"Iteration {i+1}")
        try:
            client.invoke(
                FunctionName=lambda_arn,
                InvocationType='Event',
                Payload=f'{{"dummy": "dummy", "dummySleepSeconds": {dummy_sleep_seconds} }}'
            )
        except Exception as e:
            logger.error(f"Exception occured: {e}")
        else:
            success_count = success_count + 1

    logger.info(f"{success_count}/{count} successful invocations")

    return {
        'statusCode': 200,
        'body': json.dumps('Finished')
    }

How it works

The construct creates 2 resources:

  • A CloudWatch rule
  • The Lambda function (that optionally needs to be kept Warm)

The CloudWatch rule

The CloudWatch rule is configured to start the keep warm Lambda function with a preconfigured event. The event has 2 required properties:

  • count: How many instances of the target Lambda to try to keep warm
  • lambdaArn: The ARN of the Lambda function to keep warm

The Lambda Function

The Lambda function is created as documented in the AWS CDK Lambda Function class. But in order to be kept warm, the function should be able to process a dummy event. The keep warm lambda function (see the prerequisite section) will invoke the function with a dummy event that looks like this:

{
  "dummy": "dummy",
  "dummySleepSeconds": 1
}

The Lambda function should check for the dummy event and sleep the requested amount of time (in seconds).

Testing

A test stack is added in lib\test-aws-cdk-ixor-lambda-stack. Created in this stack:

  • keepWarmLambda: this lambda will send out a dummy event every 10 minutes.
  • lambdaToKeepWarm: this is a lambda created through the IxorFunction and needs to be kept warm
  • The appropriate permissions for the keepWarmLambda to invoke the lambdaToKeepWarm.

The code for both lambdas is situated in the test_aws_cdk_ixor_lambda_handler directory.

To test this stack, deploy the test/test-aws-cdk-ixor-lambda app to sandbox. Every 10 minutes, the keepWarm lambda will trigger the lambdaToKeepWarm with a dummy event, both log this in CloudWatch.