4

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?

Edward Foyle
  • 191
  • 1
  • 9

3 Answers3

11

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.

Edward Foyle
  • 191
  • 1
  • 9
  • If you are using the NoSQL workbench which generates its own credentials, then you will have to modify the `index.js` file referenced in the answer. Change the `--sharedDb` option to `true` to enable other connections to access the table created by `amplify mock` – beevor May 14 '20 at 11:47
  • @edward-foyle How do we add profile and make use of it in the amplify mock . I've added the fake creds in the ~/.aws/credentials and fake region in the ~/.aws/config referring to the profile. It's not able to connect to DynamoDB URL . Gives Error: NetworkingError: connect ECONNREFUSED . Any suggestions ? – Shiva MSK May 21 '20 at 08:38
0

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();
Ansari Maksud
  • 316
  • 2
  • 5
  • 20
-4

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.

Nouman Khalid
  • 68
  • 1
  • 6
  • I realize that's a "fake" hostname but since I'm trying to use DynamoDB locally I figured that's probably a value that Amplify is setting. However, I can't figure out were it is setting it. My team-provider-info.json file specifies a region of us-west-2 and the CloudFormation template for my lambda is passing in the AWS::Region variable as the DynamoDB region. My understanding of `amplify mock` is that the region of the checked out environment should be used as the AWS::Region but maybe that is not the case. – Edward Foyle Nov 15 '19 at 21:52