Function handler

  • The interface that AWS Lambda will invoke your handler with. There are more specialized types for many cases where AWS services invoke your lambda, but you can directly use this type for when you are invoking your lambda directly.

    See the AWS documentation for more information about the runtime behavior, and the AWS Blog post introducing the async handler behavior in the 8.10 runtime.

    Example

    Defining a custom handler type

    import { Handler } from 'aws-lambda'

    interface NameEvent {
    fullName: string
    }
    interface NameResult {
    firstName: string
    middleNames: string
    lastName: string
    }
    type PersonHandler = Handler<NameEvent, NameResult>

    export const handler: PersonHandler = async (event) => {
    const names = event.fullName.split(' ')
    const firstName = names.shift()
    const lastName = names.pop()
    return { firstName, middleNames: names, lastName }
    }

    Example

    Logs the contents of the event object and returns the location of the logs

    import { Handler } from 'aws-lambda'

    export const handler: Handler = async (event, context) => {
    console.log("EVENT: \n" + JSON.stringify(event, null, 2))
    return context.logStreamName
    }

    Example

    AWS SDK with Async Function and Promises

    import { Handler } from 'aws-lambda'
    import AWS from 'aws-sdk'

    const s3 = new AWS.S3()

    export const handler: Handler = async (event) => {
    const response = await s3.listBuckets().promise()
    return response?.Buckets.map((bucket) => bucket.Name)
    }

    Example

    HTTP Request with Callback

    import { Handler } from 'aws-lambda'
    import https from 'https'

    let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"

    export const handler: Handler<void, number> = (event, context, callback) => {
    https.get(url, (res) => {
    callback(null, res.statusCode)
    }).on('error', (e) => {
    callback(Error(e))
    })
    }

    Returns

    A promise that resolves with the lambda result payload value, or rejects with the execution error. Note that if you implement your handler as an async function, you will automatically return a promise that will resolve with a returned value, or reject with a thrown value.

    Parameters

    • event: any

      Parsed JSON data in the lambda request payload. For an AWS service triggered lambda this should be in the format of a type ending in Event, for example the S3Handler receives an event of type S3Event.

    • context: Context

      Runtime contextual information of the current invocation, for example the caller identity, available memory and time remaining, legacy completion callbacks, and a mutable property controlling when the lambda execution completes.

    • callback: Callback<any>

      NodeJS-style completion callback that the AWS Lambda runtime will provide that can be used to provide the lambda result payload value, or any execution error. Can instead return a promise that resolves with the result payload value or rejects with the execution error.

    Returns void | Promise<any>

Generated using TypeDoc