Aws Lambda

An AWS lambda function is an app component that uses the lambda env and lambda application type.

Fork an example lambda function

Fork this lambda function to get started quickly with most of the boilerplate ready:

$bit
Copiedcopy

Note: If you need to use AWS lambda function urls, take a look at the http lambda example

Run the following to set the lambda env to your lambda app component.

Run the following to make the app loadable by your workspace:

$bit
Copiedcopy

Create a new lambda function

Run the following to create a new Node component:

$bit
Copiedcopy

Add the .lambda-app.ts app plugin file:

// @file: hello-world-lambda.lambda-app.ts

import type { LambdaAppOptions } from '@bitdev/aws.app-types.lambda-app-type';

export const HelloWorldLambdaApp: LambdaAppOptions = {
  /* the app's name (for Bit) */
  name: 'hello-world-lambda',
  /* an entry point for the app's build */
  entry: require.resolve('./hello-world-lambda.handler'),
  /* your aws host config and credentials */
  deployOptions: {
    clientConfig: {
      /* the hosting region */
      region: 'us-east-1',
      /* your aws credentials */
      credentials: {
        accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
      },
    },
      /* the lambda's name (for aws). Using an already used name will update the code. */
      functionName: 'hello-world-lambda-example',
      /* the runtime execution environment */
      runtime: 'nodejs18.x',
      /* a description for your lambda (will be displayed on the aws console) */
      description: 'Deployed from Bit',
      /* the name of the method within your code that Lambda calls to execute your function */
      handlerName: 'handler'
  },
  /* the port range for running the app (lambda) in development (bit run) */
  portRange: [3000, 5000],
};

export default SimpleLambdaApp;
CopiedCopy

Run the following to set the lambda env to your lambda app component.

Run locally

Run the following to make your app loadable by the workspace:

$bit
Copiedcopy

Run the following to run the app locally:

$bit
Copiedcopy

The output should be similar to the following:

hello-lambda-app app is running on http://localhost:3000

Run the following to test your lambda function:

curl --location --request POST 'http://localhost:3000' \
--header 'Content-Type: application/json' \
--data-raw '{
    "event":{
        "firstName": "Bit",
        "lastName": "Component"
    }
}'
CopiedCopy

The output should be similar to the following:

{
  "data": {
    "statusCode": 200,
    "headers": { "Content-Type": "application/json" },
    "body": "{\"greetings\":\"Hello Bit Component!. It is the year 2022\"}"
  }
}
CopiedCopy

Deploy to AWS

Configure the deploy options of your Lambda as shown below.

deployOptions: {
    clientConfig: {
      /* the aws hosting region */
      region: 'us-east-1',
      /* your aws credentials */
      credentials: {
        secretAccessKey: process.env.AWS_SECRET_KEY as string,
        accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
      },
    },
}
CopiedCopy

It's recommended to store your accessKeyId and secretAccessKey in your Ripple CI secrets in bit.cloud. Then the Lambda function will be deployed after building it in Ripple CI, once you tag and export it.

$bit
Copiedcopy

Note: If you need to deploy it from your local machine or using a different CI, run bit snap or bit tag with --build flag. Ensure that the accessKeyId and secretAccessKey environment variables are accessible from your CI job.