#DDB Lambda Trigger TS

4 messages · Page 1 of 1 (latest)

hidden lantern
#

Hey all, I'm wondering if there's a standard way to convert lambda triggers to typescript, I see in the docs there's some info about a function being set up with typescript, but are there any special cases regarding setting up a dynamodb strem lambda trigger with typescript?

azure bolt
#

I put my Lambda code into amplify/backend/functions/nameOfLambda/myLambda/tscr.

Handler types depend on the type of trigger... SNS, SQS, etc. Here's an example /tscr/index.ts for a CloudWatch trigger (I don't have DDB stream trigger handy):

import { ScheduledHandler } from "aws-lambda"
import './env'

export const handler: ScheduledHandler = async (event) => {
  console.log('EVENT', JSON.stringify(event, null, 2))

  const isCloudWatchCronEvent = ((event["detail-type"] === "Scheduled Event")
    && (event["source"] === "aws.events"))
  console.log('isCloudWatchCronEvent', isCloudWatchCronEvent ? 'YES' : 'NO')

  if (isCloudWatchCronEvent && (process.env.ENV !== 'prod')) {
    console.log('CLOUDWATCH EVENT: Skipping non-prod environment')
  }

  // only PRODUCTION environment or manually triggered events from this point on

  // You do you...
}

Package.json uses esbuild and tsc. Add them as dev dependencies.

  "scripts": {
    "build": "rm -f src/index.js && rm -rf dist && tsc && esbuild ./tsrc --bundle --platform=node --target=node18 --main-fields=module,main --outfile=src/index.js",
  },

And /nameOfLambda/tscconfig.json may look like this:

{
  "extends": "../../../../tsconfig.json",
  "compilerOptions": {
    "noEmit": true,
    "noImplicitAny": false,
    "lib": [
      "ESNext",
      "DOM"
    ],
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "baseUrl": "./",
    "paths": {
      "lambdashared/*": [
        "tsrc/lambdashared/*",
      ],
      "frontend/*": [
        "../../../../src/*"
      ],
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "tsrc/**/*"
  ]
}

Hope that gets you over the hump.

frail pier
#

^^ this is a good approach.

#

We do something similar