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

@capybaracode/awslocal

v1.4.7

Published

awslocal is a simple AWS Lambda runtime emulator aimed at providing faster development...

Downloads

255

Readme

awslocal

awslocal lets you test NodeJS Amazon Lambda functions on your local machine, by providing a simplistic API and command-line tool.

It does not aim to be perfectly feature proof as projects like serverless-offline or docker-lambda, but rather to remain very light (it still provides a fully built Context, handles all of its parameters and functions, and everything is customizable easily).

The main target are unit tests and running lambda functions locally.

ATTENTION: awslocal was based on lambda-local.

Prerequisites

Install

Global installation

npm install -g awslocal 

Project installation

npm install awslocal --save-dev

About: CLI

Arguments

  • init - Create awslocal settings file. [more details]

  • -i, --init Create awslocal settings file. more details

  • -c, --config Path to the config file. Default: /.awslocal.json

  • -l, --lambda-path <path> Path to the lambda handler

  • -h, --lambda-handler <handler> Handler name

  • -t, --timeout <number> Timeout in seconds

  • -p, --profile <profile> AWS profile

  • -r, --region <region> AWS region

  • -e, --env-path <path> Path to the .env file

  • -E, --event-path <path> Path to the event file

  • -P, --port <number> Port

  • -V, --version Output the version number.

  • -H, --help Show help.

Settings

By default awslocal looks for the .awslocal.json file in the root of the project. This file contains the basic settings to emulate a lambda, apigateway proxy integration, sns, sqs and other aws services that integrate with AWS Lambda.

|Field|Type|Default|Description| |-|-|-|-| |lambda|object||Lambda settings| |lambda.path|string||Required - Specify Lambda function file name| |lambda.handler|string|handler|Lambda function handler name.| |lambda.timeout|number|3|Seconds until lambda function timeout.| |aws|object||AWS global settings| |aws.region|string|us-east-1|Sets the AWS region.| |port|number|9000|Starts awslocal in server mode listening to the specified port [1-65535]| |apigateway|object||Apigateway settings| |apigateway.restApiId|string||Obtains the configurations of resources, methods and whether authentication is required regarding the restApiId| |apigateway.resources|array||This is the list of paths/resources that will be created in the future in apigateway. Note: If you have restApiId configured, the emulator combines the two resource configurations.| |apigateway.resources[].resource|string||Resource that would be configured in the api gateway| |apigateway.resources[].method|string||HTTP method| |apigateway.resources[].hasAuthorizer|boolean||Informs that this endpoint requires authentication| |apigateway.authorizer|object||It will be included in the entry that apigateway sends to the lambda| |apigateway.authorizer.context|Map<string, string\|number\|boolean>||Context moke user key value, this context is disregarded if the functionName is informed| |apigateway.authorizer.functionName|string||If you want the endpoint to be authenticated with and have the real context that apigateway sends, enter the functionName of the lambda that checks "CustomAuthorizer" access|

.awslocal.json

{
    "lambda": {
        "path": "path/to/handler",
        "handler": "handler",
        "timeout": 3,
        "env": ".env"
    },
    "aws": {
        "region": "us-east-1",
        "profile": "default"
    },
    "port": 9000,
    "apigateway": {
        "restApiId": "your-rest-api-id",
        "resources": [
            {
                "resource": "your/path/{id}",
                "method": "GET",
                "hasAuthorizer": false
            }
        ],
        "authorizer": {
            "context": {
                "yourKey": "your-value"
            },
            "functionName": "your-authorizer-function-name"
        }
    }
}

tsconfig.json

Add the configuration below to your tsconfig.json

{
  ...
  "ts-node": {
    "esm": true
  }
}

Usage

It is possible to use awslocal via command line or via vscode debug, below are some examples of use.

Step 1

create the .awslocal.json configuration file. To make it easier, you can use the command below.

npx awslocal --init

After this command, the .awslocal.json file will be created in the root of the project, with the default settings.

Remember that it is necessary to define the lambdaPath path in the created file.

Step 2

In this step we will actually emulate AWS Lambda on our local machine. The two ways to do this, the first via the command line and the second via the vscode debug, both create a server that will wait to receive the inputs

Via command line

npx awslocal

Via vscode debug

To run via vscode, you need to create a .vscode/launch.json file and copy and paste the json below.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debugs via awslocal",
      "skipFiles": ["<node_internals>/**"],
      "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/awslocal",
      "cwd": "${workspaceFolder}",
      "args": ["--config", ".awslocal.json"],
      "outputCapture": "std"
    }
  ]
}

After this configuration, just press F5 or the run debug button

Without .awslocal.json

you can emulate a lambda directly from the command line, passing the basic data. Path to the file that contains the lambda handler + path to the json file that will be the lambda input.

npx awslocal -l /path/to/lambda/file -E /path/to/test-event.json

Step 3

Now use Postman or any program that can make an http call, so you can pass the input you would pass to the lambda, see the example below

curl --location 'http://localhost:9000/lambda-invoke' \
--header 'Content-Type: application/json' \
--data '{}'

Examples

Lambda

curl --location 'http://localhost:9000/lambda-invoke' \
--header 'Content-Type: application/json' \
--data '{}'

Lambda + API Gateway proxy integration

curl --location --request PUT 'http://localhost:9000/apigateway-invoke/users/1234567489' \
--header 'Content-Type: application/json' \
--data '{
    "foo": "foo"
}'

Lambda + SNS integration

curl --location --request PUT 'http://localhost:9000/sns-invoke' \
--header 'Content-Type: application/json' \
--data '[
  {
    "subject": "Optional subject",
    "message": {
      "yourProperty": "Your data object"
    },
    "messageAttributes": {
      "yourProperty": {
        "DataType": "String",
        "StringValue": "Your data object"
      }
    }
  }
]'

Lambda + SQS integration

curl --location --request PUT 'http://localhost:9000/sqs-invoke' \
--header 'Content-Type: application/json' \
--data '[
  {
    "messageGroupId": "message-group-id",
    "messageDeduplicationId": "message-deduplication-id",
    "message": {
      "yourProperty": "Your data object"
    },
    "messageAttributes": {
      "yourProperty": {
        "dataType": "String",
        "stringValue": "Your data object",
        "stringListValues": [],
        "binaryListValues": []
      }
    }
  }
]'