Skip to main content

AWS Lambda (Node.js)

AWS Lambda functions can read from and write to Filebase buckets just like they would AWS S3 — by using the AWS SDK for JavaScript with the Filebase endpoint and credentials.

Setup

Bundle these dependencies into your Lambda deployment package or layer:

npm install @aws-sdk/client-s3 @aws-sdk/lib-storage

Set the Filebase credentials as Lambda environment variables — never bake them into the code:

VariableValue
FILEBASE_KEYYour access key ID
FILEBASE_SECRETYour secret access key
FILEBASE_BUCKETYour bucket name

For production, use AWS Secrets Manager or AWS Systems Manager Parameter Store instead of plain env vars.

Lambda handler — write to Filebase

handler.ts
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';

const filebase = new S3Client({
endpoint: 'https://s3.filebase.io',
region: 'auto',
credentials: {
accessKeyId: process.env.FILEBASE_KEY!,
secretAccessKey: process.env.FILEBASE_SECRET!,
},
});

export const handler = async (event: any) => {
const key = `events/${Date.now()}.json`;

await filebase.send(
new PutObjectCommand({
Bucket: process.env.FILEBASE_BUCKET!,
Key: key,
Body: JSON.stringify(event, null, 2),
ContentType: 'application/json',
}),
);

return { statusCode: 200, body: JSON.stringify({ key }) };
};

Pattern: AWS S3 → Filebase pipeline

A common Lambda pattern is to mirror objects from AWS S3 to Filebase, taking advantage of Filebase's free egress for downstream consumers:

import { S3Client as AwsS3 } from '@aws-sdk/client-s3';
import {
S3Client as FbS3,
GetObjectCommand,
PutObjectCommand,
} from '@aws-sdk/client-s3';

const aws = new AwsS3({ region: 'us-east-1' }); // execution role provides credentials
const fb = new FbS3({
endpoint: 'https://s3.filebase.io',
region: 'auto',
credentials: {
accessKeyId: process.env.FILEBASE_KEY!,
secretAccessKey: process.env.FILEBASE_SECRET!,
},
});

export const handler = async (event: any) => {
// S3 trigger event
const record = event.Records[0].s3;
const srcBucket = record.bucket.name;
const srcKey = decodeURIComponent(record.object.key.replace(/\+/g, ' '));

const obj = await aws.send(
new GetObjectCommand({ Bucket: srcBucket, Key: srcKey }),
);

await fb.send(
new PutObjectCommand({
Bucket: process.env.FILEBASE_BUCKET!,
Key: srcKey,
Body: obj.Body,
ContentType: obj.ContentType,
}),
);
};

Wire this Lambda to an AWS S3 bucket's PUT event and every new object lands on Filebase automatically.

Lambda layer

For multiple functions, build the AWS SDK once into a Lambda layer and reuse it:

mkdir -p layer/nodejs
cd layer/nodejs
npm install @aws-sdk/client-s3 @aws-sdk/lib-storage
cd ..
zip -r aws-sdk-layer.zip nodejs/

aws lambda publish-layer-version \
--layer-name aws-sdk-s3 \
--zip-file fileb://aws-sdk-layer.zip \
--compatible-runtimes nodejs20.x

Attach the layer to your functions and remove the @aws-sdk/* packages from each function's dependencies.

Cold-start tip

Initialize the S3 client outside the handler. Lambda reuses the module-level scope between invocations on the same warm container, so the client (and its TLS connection pool) gets reused.

What's next