1

I have followed typescriptLambda inscructions to create basic typescript lambda. Now , I would like to execute the code locally like in testNodeJsLambda , but I run into difficults to define the event and the context.

import {APIGatewayProxyCallback, Context} from 'aws-lambda';
import {APIGatewayEvent} from "aws-lambda";

     export const handler = async (
         event: APIGatewayEvent, context: Context, callback: APIGatewayProxyCallback ) => {
         console.log (`Test`)
         callback(null, {
             statusCode: 200,
          body: JSON.stringify(recordingStatus)});   
    }

How do I define the event and the context ? empty object {} is not acceptable.

The code for event is :

import {APIGatewayEvent, Context} from 'aws-lambda';
const event : APIGatewayEvent = {
        body: null,
        headers: {},
        multiValueHeaders: {},
        httpMethod: "POST",
        isBase64Encoded: false,
        path: "/path/to/resource",
        pathParameters : null,
        queryStringParameters : null,
        multiValueQueryStringParameters : null,
        stageVariables : null,
        requestContext: {
            accountId: "123456789012",
            apiId: "1234567890",
            authorizer: {},
            protocol: "HTTP/1.1",
            httpMethod: "POST",
            identity: {
                accessKey: null,
                accountId: null,
                apiKey : null,
                apiKeyId : null,
                caller: null,
                clientCert: null,
                cognitoAuthenticationProvider: null,
                cognitoAuthenticationType: null,
                cognitoIdentityId: null,
                cognitoIdentityPoolId: null,
                principalOrgId : null,
                sourceIp: "127.0.0.1",
                userArn: null,
                userAgent: "Custom User Agent String",
                user: null
            },
            path: "/prod/path/to/resource",
            stage: "prod",
            requestId: "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
            requestTime: "09/Apr/2015:12:34:56 +0000",
            requestTimeEpoch: 1428582896000,
            resourceId: "123456",
            resourcePath: "/{proxy+}",
        },
        resource: "/{proxy+}"
    };
2
  • you can probably look at the context structure used from the Node.js documentation that might help you defining your own event and context locally (docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html). For the event object, it probably depens on the service whose invoking the lambda. What is calling this lambda ? Commented Aug 16, 2022 at 6:25
  • You can find the context and the event types used by AWS installing @types/aws-lambda Commented Aug 16, 2022 at 8:12

1 Answer 1

5

You can use this one, if you're using the AWS-SDK v2.

import { Context } from 'aws-lambda';

const mockedContext: Context = {
    callbackWaitsForEmptyEventLoop: false,
    functionName: 'mocked',
    functionVersion: 'mocked',
    invokedFunctionArn: 'mocked',
    memoryLimitInMB: 'mocked',
    awsRequestId: 'mocked',
    logGroupName: 'mocked',
    logStreamName: 'mocked',
    getRemainingTimeInMillis(): number {
        return 999;
    },
    done(error?: Error, result?: any): void {
        return;
    },
    fail(error: Error | string): void {
        return;
    },
    succeed(messageOrObject: any): void {
        return;
    }
};

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.