I am looking at a way to trigger AWS Batch job from a react app (from a click of a button) and came across this SDK
https://www.npmjs.com/package/@aws-sdk/client-batch
I am new to react and I am having hard time to get this working. All I want is to start very simple like listing the batch jobs from AWS. The examples shown there are not detailed enough as I would I thought, but any help to get this starting is much appreciated.
Currently I've a button in my react app with this unfinished piece of code. I am not sure where to pass in the AWS credentials here, there are parameters for supplying region, job name, job queue etc. I want to know an efficient way to calling batch from the front end. Any example to trigger a batch job from this code will help me a long way.
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { BatchClient, ListJobsCommand } from "@aws-sdk/client-batch";
function App() {
const [count, setCount] = useState(0)
return (
<div>
<button type="button" onClick={handleClick}>
Click me
</button>
</div>
)
}
const handleClick = async () => {
const client = new BatchClient({ region: "us-east-1" });
const input = {
jobQueue: "react-python-jobqueue"
};
You want to authenticate a user that can assume a AWS IAM role to take actions on your AWS resources.
The AWS JavaScript SDK documentation has an example for using Cognito user pool to list the objects in a private S3 bucket from a HTTP client (in this case a browser).
Once the user is authenticated and assumed the role, they can submit requests to the AWS services that are allowed by that role. You can view the AWS Batch actions that can be specified in the role here. For a user that is only submitting and reviewing jobs, you will want to scope down the permissions to
Deny
things likeCreateComputeEnvironment
orDeleteComputeEnvironment
etc.