# AWS Lambda - NodeJS

## 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.

{% hint style="success" %}

### **Prerequisites:**

* [x] Have an [AWS Account](https://console.aws.amazon.com/console/home?nc2=h_ct\&src=header-signin\&region=us-east-1) configured to be used on the us-east-1 region.
* [x] [Sign up](https://filebase.com/signup) for a free Filebase account.&#x20;
* [x] Have your Filebase Access and Secret Keys. Learn how to view your access keys [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#working-with-access-keys).
* [x] Create a Filebase Bucket. Learn how to create a bucket [here](https://docs.filebase.com/getting-started-guides/getting-started-guide#creating-and-working-with-buckets).
  {% endhint %}

### 1. Login to the [AWS Management Dashboard](https://us-east-1.console.aws.amazon.com/).

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FFliLsjKPLRteserwR8gD%2Fimage.png?alt=media\&token=349b807e-14b7-4288-be58-6d21e560515c)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FPfswnShu7lnLf48tzQqu%2Fimage.png?alt=media\&token=c0cc53ab-93d8-4184-970d-942552d46153)

### 3. Select ‘Create function’ from the left.

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FoRb6y04kNw7Td3ZyiDF0%2Fimage.png?alt=media\&token=2938b52f-a41f-461c-90e4-03ea38ae6d75)

### 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’.

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FI6696rDWBwGowotAlmxb%2Fimage.png?alt=media\&token=ab66bce2-7946-4832-a05e-444792af5065)

### 5. Enter the following code in the code tab:

```jsx
const AWS = require('aws-sdk');
const fs = require('fs');

exports.handler =  async function(event, context) {
}

const s3 = new AWS.S3({
    endpoint: 'https://s3.filebase.com',
    region: 'us-east-1',
    signatureVersion: 'v4',
    accessKeyId: 'FILEBASE-ACCESS-KEY',
    secretAccessKey: 'FILEBASE-SECRET-KEY',
});

fs.readFile('image.png', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    
    const params = {
        Bucket: 'BUCKET-NAME',
        Key: 'FILE-NAME.txt',
        ContentType: 'image/png',
        Body: data,
    };
    
    const request = s3.putObject(params);
    request.on('httpHeaders', (statusCode, headers) => {
        print(`CID: ${headers['x-amz-meta-cid']}`);
    });
    request.send();
});
```

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FsfYDmGW12x34xYuu9Wdw%2Fimage.png?alt=media\&token=02eb2075-ff9b-4f7e-804c-e00b2ce73fee)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FoYlJUMq3YVKeX1BAvV9A%2Fimage.png?alt=media\&token=f6d822d5-9e21-491f-be97-e7432445a8b4)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FhqKLBJL4RSen58n0qbtl%2Fimage.png?alt=media\&token=c9d106e5-81a1-460a-94ff-0e0c07d6d135)

### 8. Save these configuration settings.

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FYs2ekyBnkoIHI9P2xxA1%2Fimage.png?alt=media\&token=23878320-0c7b-4a4e-872f-d38544cd50b1)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FO35R8B4LS4S3IxdlqayR%2Fimage.png?alt=media\&token=2e506cff-cd5e-45ce-a10f-ed179ff3064b)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2FNVQumLftbJNqR8gVbJ1C%2Fimage.png?alt=media\&token=d825f3aa-0107-4f27-a51d-076be9df0f83)

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

![](https://3861818989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lyjw7dWpiQtUFDa1pO0%2Fuploads%2Fpd6VFvGcgaEO6QqCkTpu%2Fimage.png?alt=media\&token=1138b9b7-cad2-4bbc-8b1a-bdc6268c5012)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.filebase.com/code-development-+-sdks/code-development/aws-lambda-nodejs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
