1.4.1 • Published 29 days ago

@capybaracode/awslocal v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

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.

FieldTypeDefaultDescription
lambdaobjectLambda settings
lambda.pathstringRequired - Specify Lambda function file name
lambda.handlerstringhandlerLambda function handler name.
lambda.timeoutnumber3Seconds until lambda function timeout.
awsobjectAWS global settings
aws.regionstringus-east-1Sets the AWS region.
portnumber9000Starts awslocal in server mode listening to the specified port 1-65535
apigatewayobjectApigateway settings
apigateway.restApiIdstringObtains the configurations of resources, methods and whether authentication is required regarding the restApiId
apigateway.resourcesarrayThis 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[].resourcestringResource that would be configured in the api gateway
apigateway.resources[].methodstringHTTP method
apigateway.resources[].hasAuthorizerbooleanInforms that this endpoint requires authentication
apigateway.authorizerobjectIt will be included in the entry that apigateway sends to the lambda
apigateway.authorizer.contextMap<string, string\number\boolean>Context moke user key value, this context is disregarded if the functionName is informed
apigateway.authorizer.functionNamestringIf 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": []
      }
    }
  }
]'
1.4.1

29 days ago

1.4.0

29 days ago

1.3.4

4 months ago

1.3.3

5 months ago

1.3.2

5 months ago

1.3.1

5 months ago

1.3.0

5 months ago

1.2.4

5 months ago

1.2.3

5 months ago

1.2.2

6 months ago

1.2.1

6 months ago

1.2.0

6 months ago

1.1.0

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago