AWS Lambda - NodeJS
Learn how to configure AWS Lambda functions for NodeJS for use with Filebase.

What is AWS Lambda?

AWS Lambda is a service offered by Amazon Web Services that offers a serverless computing platform for creating event-driven pieces of code. Lambda automatically manages any computing resources required for the code, so there is no configuration or development environment required by the end-user.
In this guide, we’ll use Lambda for creating a PutObject function using NodeJS. This can be modified for a variety of different API requests, such as GetObject and CreateBucket, and can be used with different SDKs and Runtimes. For a list of supported SDKs and associated API request examples, check out our SDK documentation here.
Read below to learn how to configure AWS Lambda functions for NodeJS use with Filebase.

Prerequisites:

  • Have an AWS Account configured to be used on the us-east-1 region.
  • Have your Filebase Access and Secret Keys. Learn how to view your access keys here.
  • Create a Filebase Bucket. Learn how to create a bucket here.

1. Login to the AWS Management Dashboard.

2. Search ‘Lambda’ in the search bar, then select the Lambda service.

3. Select ‘Create function’ from the left.

4. From the Create function menu, select ‘Author from scratch’, provide your function with a name, and select the Nodejs 16.x runtime. Then select ‘Create Function’.

5. Enter the following code in the code tab:

1
const AWS = require('aws-sdk');
2
const fs = require('fs');
3
4
exports.handler = async function(event, context) {
5
}
6
7
const s3 = new AWS.S3({
8
endpoint: 'https://s3.filebase.com',
9
region: 'us-east-1',
10
signatureVersion: 'v4',
11
accessKeyId: 'FILEBASE-ACCESS-KEY',
12
secretAccessKey: 'FILEBASE-SECRET-KEY',
13
});
14
15
fs.readFile('image.png', (err, data) => {
16
if (err) {
17
console.error(err);
18
return;
19
}
20
21
const params = {
22
Bucket: 'BUCKET-NAME',
23
Key: 'FILE-NAME.txt',
24
ContentType: 'image/png',
25
Body: data,
26
};
27
28
const request = s3.putObject(params);
29
request.on('httpHeaders', (statusCode, headers) => {
30
print(`CID: ${headers['x-amz-meta-cid']}`);
31
});
32
request.send();
33
});
Copied!
Replace the following values in the code to match your configuration:
  • accessKeyId: Your Filebase Access Key
  • secretAccessKey: Your Filebase Secret Key
  • Bucket: Your Filebase Bucket Name
  • Key: The Local Path To The Object To Be Uploaded
  • Content Type: The Type of Object Being Uploaded

6. Next, select ‘Configuration’, then ‘Edit’:

7. Change the ‘Timeout’ value to 30 seconds.

8. Save these configuration settings.

9. Select ‘Deploy’ to save your changes in the function.

10. Then select ‘Test’ and create a new test event with the following settings:

11. Then, run the test. The execution response should resemble the following:

12. Navigate to the Filebase web console dashboard to confirm the upload of your file:

If you have any questions, please join our Discord server, or send us an email at [email protected]