aws-amplify / aws-amplify/amplify-cli
Crash with 8.0.2 from "amplify add function", .NET or Node.js, CRUD function, use existing table
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 825
- Avg merge
- 11d 23h
- Merged PRs (30d)
- 2
Description
### Before opening, please confirm:
- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-cli/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-cli/blob/master/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.
### How did you install the Amplify CLI?
curl -sL https://aws-amplify.github.io/amplify-cli/install | bash && $SHELL
### If applicable, what version of Node.js are you using?
v14.17.0
### Amplify CLI Version
8.0.2
### What operating system are you using?
Amazon Linux 2
### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.
No.
### Amplify Categories
storage, function
### Amplify Commands
add
### Describe the bug
The Amplify app has one DynamoDB data model, created using Amplify Studio and resulting in a GraphQL schema using the `@model` directive. Now I'm trying to add a Lambda function resolver for accessing the existing table, for encapsulating some proprietary business logic.
```
[ec2-user@ip-10-0-4-20 business-logic]$ amplify add function
? Select which capability you want to add: Lambda function (serverless function)
? Provide an AWS Lambda function name: AuthenticateExternalAPISession
? Choose the runtime that you want to use: NodeJS
? Choose the function template that you want to use: CRUD function for DynamoDB (Integration with API Gateway)
? Choose a DynamoDB data source option Use DynamoDB table configured in the current Amplify project
There are no DynamoDB resources configured in your project currently
? Choose a DynamoDB data source option Use DynamoDB table configured in the current Amplify project
There are no DynamoDB resources configured in your project currently
Error: Invalid option selected
at askDynamoDBQuestions (/snapshot/node_modules/amplify-nodejs-function-template-provider/lib/utils/dynamoDBWalkthrough.js:69:11)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async provideCrud (/snapshot/node_modules/amplify-nodejs-function-template-provider/lib/providers/crudProvider.js:14:28)
at async templateWalkthrough (/snapshot/node_modules/amplify-category-function/lib/provider-utils/awscloudformation/utils/functionPluginLoader.js:56:12)
at async Object.createWalkthrough (/snapshot/node_modules/amplify-category-function/lib/provider-utils/awscloudformation/service-walkthroughs/lambda-walkthrough.js:43:75)
at async addFunctionResource (/snapshot/node_modules/amplify-category-function/lib/provider-utils/awscloudformation/index.js:78:9)
at async Object.executeAmplifyCommand (/snapshot/node_modules/amplify-category-function/lib/index.js:262:5)
at async executePluginModuleCommand (/snapshot/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:142:5)
at async executeCommand (/snapshot/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:40:9)
at async Object.run (/snapshot/node_modules/@aws-amplify/cli-internal/lib/index.js:160:9)
There was an error adding the function resource
```
### Expected behavior
I'm hoping to generate boilerplate code for a CRUD operation (create) so that I can see how to access the Amplify-managed DynamoDB table from my own Lambda function code. That's my real goal here. I have found examples of how to access the DynamoDB table based on table naming conventions, but I have not found any AWS documentation or any good examples anywhere about the best way to access a model's DynamoDB table from a custom Lambda function resolver.
### Reproduction steps
1. Create an Amplify app
2. Add one simple model (ours has three simple `String` fields.)
3. Try to `amplify add function` to generate boilerplate CRUD, either either .NET or Node.js
4. error
### GraphQL schema(s)
```graphql
type ExternalAPISession
@model(mutations: { create: null })
@auth(rules: [{allow: public}]) {
id: ID!
username: String! @index(
name: "byUsername",
queryField: "getExternalAPISessionByUsername"
)
access_token: String!
refresh_token: String!
}
type Mutation {
createExternalAPISession(
username: String,
password: String):
ExternalAPISession @function(name: "CreateExternalAPISession-${env}")
}
```
### Log output
```
# Put your logs below this line
```
### Additional information
Our real question that we're trying to answer as we encounter this error is: What is the best way to access an Amplify-managed DynamoDB table for a `@model` from a custom Lambda function resolver?
Contributor guide
Research direction
Start with the `amplify add function` CRUD template flow, especially `amplify-nodejs-function-template-provider/lib/utils/dynamoDBWalkthrough.js` and `lib/providers/crudProvider.js` from the stack trace. Reproduce the existing `@model` table setup and the DynamoDB source selection; done means the command accepts the configured table and generates usable CRUD boilerplate without the invalid-option error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, graphql, nodejs
- Domain
- backend-api-design, cli, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100