How to call an AWS Amplify API via another AWS Amplify API

Viewed 20

Background:

I have an AWS Amplify application that uses a number of REST APIs (Lambda and API Gateway). I have my first API, we will call API_A that contains all my main paths. I had to create another API we will call API_B that contains a single webhook for purchases via Stripe. The idea here is that API_B with the webhook will handle events, one event of which is to call an endpoint in API_A when an event occurs. What I cannot figure out is how to call one Amplify API in another.

What I tried:

  1. I tried to rely on the AWS documentation for React/JS6 to call the API as I normally would, however, I am unable to import aws-amplify. Anytime I run import { API } from 'aws-amplify'; or its equivalent const {API} = require("@aws-amplify"); I am met with a runtime error:

{ "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module '@aws-amplify/api'\nRequire stack:\n- /var/task/app.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/Runtime.js\n- /var/runtime/index.js", "stack": [ "Runtime.ImportModuleError: Error: Cannot find module '@aws-amplify/api'", "Require stack:", "- /var/task/app.js", "- /var/task/index.js", "- /var/runtime/UserFunction.js", "- /var/runtime/Runtime.js", "- /var/runtime/index.js", " at _loadUserApp (/var/runtime/UserFunction.js:221:13)", " at Object.module.exports.load (/var/runtime/UserFunction.js:279:17)", " at Object.<anonymous> (/var/runtime/index.js:43:34)", " at Module._compile (internal/modules/cjs/loader.js:1085:14)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", " at Module.load (internal/modules/cjs/loader.js:950:32)", " at Function.Module._load (internal/modules/cjs/loader.js:790:12)", " at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)", " at internal/main/run_main_module.js:17:47" ] }

  1. I tried to call the endpoint via axios where I import the library as const axios = require('axios'); and try to make a POST request. However, I do not fully understand the permissions settings of IAM in Amplify to get it to work. Ideally, the credentials should not be hard coded as axios will require.

Main Question:

What is the best practice to call one API from another API in the realm of AWS Amplify?

Thanks!

0 Answers
Related