4
votes

I am using AWS Amplify to set up an AppSync GraphQL API. I have a schema with an @model annotation and I am trying to write a lambda resolver that will read/write to the DynamoDB table that @model generates. However, when I attempt to test locally using amplify mock my JS function throws

error { UnknownEndpoint: Inaccessible host: `dynamodb.us-east-1-fake.amazonaws.com'. This service may not be available in the `us-east-1-fake' region.

I can't seem to find much documentation around this use case at all (most examples of lambda resolvers read from other tables / APIs that are not part of the amplify app) so any pointers are appreciated. Is running this type of setup even supported or do I have to push to AWS in order to test?

3
I wouldn't suggest not to try it locally. Does the schema works with 'amplify push'?meck373

3 Answers

10
votes

New Answer:

Amplify now has documentation on this use case: https://docs.amplify.aws/cli/usage/mock#connecting-to-a-mock-model-table

You can set environment variables for mock that will point the DDB client in the mock lambda to the local DDB instance

=====================================================================

Original Answer:

After some digging into the Amplify CLI code, I have found a solution that will work for now.

Here is where amplify mock initializes DynamoDB Local. As you can see, it does not set the --sharedDb flag which based on the docs means that the created database files will be prefixed with the access key id of the request and then the region. The access key id of requests from Amplify will be "fake" and the region is "us-fake-1" as defined here. Furthermore, the port of the DynamoDB Local instance started by Amplify is 62224 defined here.

Therefore, to connect to the tables that are created by Amplify, the following DynamoDB config is needed

const ddb = new AWS.DynamoDB({
  region: 'us-fake-1',
  endpoint: "http://172.16.123.1:62224/",
  accessKeyId: "fake",
  secretAccessKey: "fake"
})

If you want to use the AWS CLI with the tables created by Amplify, you'll have to create a new profile with the region and access keys above.

I'll still need to do some additional work to figure out a good way to have those config values switch between the local mock values and the actual ones, but this unblocks local testing for now.

As for another question that I had about where AWS::Region of "us-east-1-fake" was being set, that gets set here but it does not appear to be used anywhere else. ie, it gets set as a placeholder value when running amplify mock but using it as a region in other places for testing locally doesn't seem to work.

0
votes

Please try the below setting, It's working fine for me,

const AWS = require('aws-sdk');

// Local
const dynamoDb = new AWS.DynamoDB.DocumentClient({
    region: 'us-fake-1',
    endpoint: "http://localhost:62224/",
    accessKeyId: "fake",
    secretAccessKey: "fake"
});

// Live
// const dynamoDb = new AWS.DynamoDB.DocumentClient();
-3
votes

your dynamodb host is incorrect. dynamodb.us-east-1-fake is not a valid host. Please update it with real dynamodb host name.
If you are running locally setup aws configure on cli first.