2

I'm looking at the documentation for AWS Amplify and am confused about the correct way to use Authentication to call an API Gateway Endpoint.

At the bottom of the page it says this:

THIS IS NOT A RECOMMENDED ARCHITECTURE and we highly recommend you leverage AWS AppSync or API Gateway as the endpoint to invoke your Lambda functions.

and I need some help getting on the right track because I'm very confused

Chris Lang
  • 384
  • 2
  • 19
  • Same issue here. The aws-amplify libraries that worked great with page api routes not working with app api routes. No documentation or examples. – lentyai May 10 '23 at 03:25

2 Answers2

0

I think the THIS IS NOT A RECOMMENDED ARCHITECTURE comment is referring to the code example immediately above it. The code example shows an example of directly invoking a lambda function, not proxying it through the API Gateway.

API: {
    endpoints: [
        {
            name: "MyCustomLambda",
            endpoint: "https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/yourFuncName/invocations",
            service: "lambda",
            region: "us-east-1"
        }
    ]
}
James Newell
  • 652
  • 8
  • 12
  • The documentation indeed is confusing. I think @CL gets what recommended. He asks for the guidance on implementation. And so am I, because aws-amplify just refuse to work with new router. – lentyai May 10 '23 at 03:27
-2

Integrating AWS AppSync, API Gateway, and AWS Amplify can be a powerful way to build scalable and flexible applications with real-time data capabilities.

Set up AWS AppSync: Run amplify add api and choose the "GraphQL" option to add a GraphQL API to your project.Follow the prompts to configure your API. Choose "Amazon Cognito User Pool" for authentication, which will allow you to use Amplify's authentication capabilities with your AppSync API. After configuring the API, run amplify push to create the backend resources in your AWS account.

Integrate AWS AppSync with Amplify: With the AppSync API set up, you can now use Amplify to interact with it. In your frontend code, install the Amplify libraries and set up the necessary configurations. For example, if you are using React, run: npm install aws-amplify @aws-amplify/api @aws-amplify/pubsub In your app's entry point (e.g., index.js or App.js), import and configure Amplify:

import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);

Now you can use the API object provided by Amplify to make GraphQL queries and mutations to your AppSync API. For example: ```js import { API } from 'aws-amplify';

 // Example GraphQL query
 API.graphql({ query: queryName, variables: { /* variables if any */ } })
   .then(response => {
     console.log(response.data);
   })
   .catch(error => {
     console.log(error);
   });
 ```

Use AWS AppSync subscriptions for real-time data: AWS AppSync supports real-time data with GraphQL subscriptions. To use this feature, you can use Amplify's PubSub module: ```js import { API, graphqlOperation } from 'aws-amplify'; import { onCreateTodo } from './graphql/subscriptions';

 const subscription = API.graphql(graphqlOperation(onCreateTodo)).subscribe({
   next: eventData => {
     console.log('New Todo:', eventData.value.data.onCreateTodo);
   },
   error: errorData => {
     console.log('Error:', errorData);
   }
 });

 // To unsubscribe from the subscription when it's no longer needed
 subscription.unsubscribe();
 ```

Setting up AWS API Gateway: If you wish to have a RESTful API in addition to your AppSync GraphQL API, you can set up AWS API Gateway and integrate it with your existing Amplify project. To do this, run amplify add api and choose the "REST" option instead. Follow the prompts to configure the API, and then run amplify push to create the API Gateway resources in your AWS account.