0
votes

How do I add my own lambda without going to AWS console manually and, most importantly, how do I call it from my React app?

Currently, if I want to execute my own lambda function, I go to AWS console and create it there manually. Clearly, there is a way to do this locally in VS Code, since serverless-framework has created its functions already using the fullstack-app deployment.

This is my current Lambda function (send an email from contact form using Amazon SES) created in AWS using console.

var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-1'});
exports.handler = (event, context, callback) => {
    
     var params = {
        Destination: {
            ToAddresses: [`${event.toEmail}`]
        },
        Message: {
            Body: {Html: { Data: `${event.body}`}
            },
            Subject: { Data: `${event.subject}`}
        },
        Source: `${event.fromEmail}`
    };

     ses.sendEmail(params, function (err, data) {
        callback(null, {err: err, data: data});
        if (err) {
            console.log(err);
            context.fail(err);
        } else {
            console.log(data);
            context.succeed(event);
        }
    });
};

I have created a REST API for it in AWS and I call it from my React app with axios:

  axios.post(`https://xxxxxxxx.execute-api.us-east-1.amazonaws.com/default/contactFormSend`, email)
  .then(res => {console.log(res)})

My goal is to not create the lambda function manually in AWS console, but write it locally using serverless-framework architecture and find a way to call it.

I have looked everywhere, but I feel like I have missed something very important during my learning about serverless-framework architecture.

1

1 Answers

0
votes

How do I add my own lambda without going to AWS console manually and, most importantly ?

I hope you have serverless.yml file with your function config. Here is a template with possible configs https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/

If everything is setup deploying is super easy , just by using serverless deploy

https://www.serverless.com/framework/docs/providers/aws/guide/deploying/

Here is a very simple one from serverless examples - https://github.com/serverless/examples/tree/master/aws-node-rest-api

How do I call it from my React app ?

You need an exposed public endpoint either you take directly the one generated by API Gateway or you create custom domain and map it to your existing domain.