Best-practice for using aws-amplify node module from within a Lambda function?
See original GitHub issue** Which Category is your question related to? ** Functions
** What AWS Services are you utilizing? ** Lambda
** Provide additional details e.g. code snippets **
I can const Amplify = require('aws-amplify') from within my Lambda function, but is there a clean way to pass it the aws_exports.js file needed for Amplify.configure()? My use case is that I would like to be able to call Auth.signUp from within the Lambda function (so that I can specify some custom attributes “server side”).
Issue Analytics
- State:
- Created 5 years ago
- Comments:22 (6 by maintainers)
Top Results From Across the Web
Best practices for working with AWS Lambda functions
Function code. Separate the Lambda handler from your core logic. This allows you to make a more unit-testable function. In Node.js this may...
Read more >How to Reuse Node.js Packages with AWS Lambda Functions ...
In this article, you'll learn how to inject custom packages on AWS Lambda Functions' Runtime by using AWS Lambda Layers.
Read more >Functions - Overview - AWS Amplify Docs
Use Amplify CLI to add powerful Lambda functions to your cloud-based mobile and web app with a simple guided workflow. - AWS Amplify...
Read more >Serverless Functions in Depth with AWS Amplify - YouTube
Twitch Live Stream - Learn how to build, deploy, update, delete & test serverless functions using AWS Amplify. We'll also learn how to...
Read more >How To Use AWS AppSync in Lambda Functions - Medium
In this tutorial, you are going to learn how you can execute queries and mutations in a Lambda function on an AppSync GraphQL...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

And yes, I had to use a polyfill. It looks something like this:
@undefobj @kaustavghosh06 @nikhil-dabhade I have a couple of core use cases where this is an issue:
Side-stepping Cognito for interactions outside of a user context (e.g. 3rd Party Systems Integrations)
As part of my app I expose a REST API for use by external systems (e.g. Stripe) that offers a restricted set of capabilities (not pure CRUD) related to my data model. I have created this API via Amplify as a backend API using the “REST API → Serverless Express” option. My core data model is created via Amplify as a GraphQL api, which auto-generated the underlying DynamoDB storage. My options from my custom API are to either (a) go directly against the underlying DynamoDB storage or (b) go through the Amplify.API library. Either use case requires that I have configuration values pointing to resources that are dynamically generated for each environment (the DynamoDB table names in the former and the GraphQL endpoint in the latter). I use no Amplify-configured inbound authentication to the API, use IAM authenticate to retrieve secrets from Secret Manager, use those secrets to bootstrap a Cognito User identity as a service account, and then make the eventual calls into the GraphQL endpoint using that Cognito user.
I would also include things like cron jobs in this category.
Running code on the server to circumvent the overly-permissive nature of the AppSync
@authrules (e.g. input validation)Certain operations against the data model require server-side code to run and cannot fully trust the client. An example of this is changes to the billing status of a tenant. The user may have permission to update the
planfield but not thepricefield; there is no way to do this with Amplify today so I circumvent this by instead locking down all writes from users except (again) a special service account user. Then the user places a call into a separate REST API created via Amplify, but this time with Cognito authentication enabled. I then do some nonsense to actually get access to who the requesting user is (something that I would expect would be a common need if you’ve selected that the API should be protected by Cognito). Then I perform my business logic within the Lambda function and ultimately execute the change against the GraphQL endpoint using the service account rather than the end-user’s Cognito identity.I’d be happy to schedule some time to walk you through my specific codebase and application if that’d be helpful. I’m based in California.