I have this simple form executed into React page:
import React from 'react';
import { Link } from 'react-router-dom';
import {LambdaClient, InvokeCommand, LogType} from "@aws-sdk/client-lambda"; // ES Modules import
const FooterOne = ({ footerLight, style, footerGradient }) => {
const invoke = async (funcName, payload) => {
const client = new LambdaClient({});
const command = new InvokeCommand({
FunctionName: funcName,
Payload: JSON.stringify(payload),
LogType: LogType.Tail,
});
const { Payload, LogResult } = await client.send(command);
const result = Buffer.from(Payload).toString();
const logs = Buffer.from(LogResult, "base64").toString();
return { logs, result };
};
return (
<>
<form>
<input
type='text'
className='input-newsletter form-control me-2'
placeholder='Enter your email'
name='email'
required=''
autoComplete='off'
/>
<input
type='submit'
value='Subscribe'
data-wait='Please wait...'
/>
</form>
</>
);
};
export default FooterOne;
Lambda code into AWS Lambda:
export const handler = async (event) => {
// TODO implement
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
I created IAM user with the fallowing permissions:
Do you know how I can call the AWS Lambda code when I submit this simple form with JavaScript aws-sdk
?
2
Answers
Listen for onSubmit Event on form by passing function to form tag like this
As you have added
type: submit
in your button, form submit event will be triggered on form when someone will click that button.And you can write handleSubmit like this to invoke the lambda
Try this, Hope it’s working.
AWS Lambda Permissions
AWS IAM Permissions (for React App)