Task 4: Deploy the Backend API
In this task, you will use AWS Amplify to configure a custom data query to use Amazon Bedrock as data source
Introduction
Implementation
Set up Amplify Data
1. Update the resource.ts file
On your local machine, navigate to the ai-recipe-generator/amplify/data/resource.ts file, and update it with the following code. Then, save the file.
The following code defines the askBedrock query that takes an array of strings called ingredients and returns a BedrockResponse. The .handler(a.handler.custom({ entry: "./bedrock.js", dataSource: "bedrockDS" })) line sets up a custom handler for this query, defined in bedrock.js, using bedrockDS as its data source.

Resource.ts file
Replace your code with this code
import { type ClientSchema, a, defineData } from "@aws-amplify/backend";
const schema = a.schema({
BedrockResponse: a.customType({
body: a.string(),
error: a.string(),
}),
askBedrock: a
.query()
.arguments({ ingredients: a.string().array() })
.returns(a.ref("BedrockResponse"))
.authorization((allow) => [allow.authenticated()])
.handler(
a.handler.custom({ entry: "./bedrock.js", dataSource: "bedrockDS" })
),
});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "apiKey",
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});
2. Deploy resources
Open a new terminal window, navigate to your apps project folder (ai-recipe-generator), and run the following command to deploy cloud resources into an isolated development space so you can iterate fast.
npx ampx sandbox

3. View confirmation message
Once the cloud sandbox has been fully deployed, your terminal will display a confirmation message.

4. Verify outputs file creation
Verify that the amplify_outputs.json file was generated and added to your project.

Conclusion
You have configured a GraphQL API to define a custom query to connect to Amazon Bedrock and generate recipes based on a list of ingredients.
Build the Frontend
Did you find what you were looking for today?
Let us know so we can improve the quality of the content on our pages