Code walkthrough of the first Lambda function

  1. At AWS Console
  • Choose Lambda
  1. At the create function interface
  • Choose author from scratch
  • Function name: Lambda-ChatApp-Proxy
  • Choose Runtime: Nodejs 18.x
  • Choose Create function
  • Use the created role: Lambda-Role-ChatApp
  1. Enter the code for the function Copy the code and paste it into index.js
import {GetObjectCommand, S3Client} from '@aws-sdk/client-s3';

const client = new S3Client({});

const bucket = '<your bucket name>';

export const handler = async function () {
    try {
        const response = await client.send(new GetObjectCommand({
            Bucket: bucket,
            Key: 'data/conversations.json'
        }));
        return done(null, JSON.parse(await response.Body.transformToString()));
    } catch (e) {
        return done(e);
    }
};

function done(err, res) {
    if (err) {
        console.error(err);
    }
    return {
        statusCode: err ? '400' : '200',
        body: err ? JSON.stringify(err) : JSON.stringify(res),
        headers: {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Origin': '*'
        }
    };
}
  • Choose Deploy