Read/Download S3 files using Lambda Functions
Tags: aws, s3, lambda, javascript
Published: 2023-02-15 17:35:49

To get started with this tutorial, you'll first need to create an Amazon S3 bucket and upload a sample object to it. 

## Step 1 Create a bucket and upload a sample object:
1. Open the Amazon S3 console and choose "Create bucket".
2. Give your bucket a name (e.g. MyTestBucket07) and choose a region.
3. Click "Create bucket".
4. Select the bucket you just created and go to the "Objects" tab.
5. Choose "Upload" and select a test file (e.g. MyTestFile.txt) from your local machine. This file can be a text file containing anything you want.
6. Click "Upload".

Once your bucket is set up, you can create a Lambda function using a function blueprint. A blueprint is a sample function that demonstrates how to use Lambda with other AWS services. 

## Step 2 Create an IAM role:
Create an IAM role with a policy that provides read and write access to S3

## Step 3 Create the Lambda Function:
Here are the steps to create a Lambda function in the console:

1. Open the **Functions** page on the Lambda console.
2. Choose "Create function".
3. Select "Use a blueprint".
4. Search for "s3" under Blueprints and choose the s3-get-object blueprint for Node.js.
5. Click "Configure".
6. Under "Basic information", 

- Enter a name for your function (e.g. ReadS3File)
- Choose "Create a new role from AWS policy templates" for the execution role.
- Enter a name for your role (e.g. S3ReadAccess).
- From Policy template select **Amazon S3 object read-only permissions**
7. Under "S3 trigger", choose the bucket you created earlier.
8. Click "Create function".
Next, you can review the function code, which retrieves the source S3 bucket name and the key name of the uploaded object from the event parameter it receives. The function uses the Amazon S3 getObject API to retrieve the content type of the object.

## Step 3: To view the function code in the Lambda console:

1. Go to the "Code" tab while viewing your function.
2. Look under "Code source".
3. AWS provided code need some changes, 

```
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});

exports.handler = async (event, context) => {
    // Set the bucket name and object key based on the file name
    const bucketName = "MyTestBucket07";
    const objectKey = event.filePath;
    
    const params = {
        Bucket: bucketName,
        Key: objectKey
    };

    try {
        // Read the object
        const s3Object = await s3.getObject(params).promise();
        console.log(s3Object.Body.toString());
        
        return s3Object.Body.toString();
    } catch (err) {
        console.log(err);
        throw err;
    }
};
```

## Step 4: Test the Lambda Function
1. On the Code tab, under Code source, choose the arrow next to Test, and then choose Configure test events from the dropdown list.
2. In the Configure test event window, do the following:
Choose Create new test event named 'TestCase01'
3. Add the following code in Event JSON

```
{
  "filePath": "MyTestFile.txt"
}
```
4. Choose Create.

- To invoke the function with your test event, under Code source, choose Test. The Execution results tab displays the response, function logs, and request ID