Amplify-cli: Missing region in config error on mocking a function

Created on 25 Oct 2020  路  5Comments  路  Source: aws-amplify/amplify-cli

Describe the bug
getting following error message when trying to run 'amplify mock function func'

ERROR => {"message":"Missing region in config","code":"ConfigError","time":"2020-10-25T16:29:18.930Z"}

To Reproduce
Steps to reproduce the behavior:

  1. amplify add function
  2. Choose Lambda function with Node.js runtime
  3. amplify function build
  4. amplify mock function func
  5. See error

Expected behavior
Error free

Code Snippet

const AWS = require('aws-sdk');
const iotData = new AWS.IotData({ endpoint: process.env.AWS_IOT_ENDPOINT });

exports.handler = async(event) => {
    var params = {
        topic: "test/topic",
        payload: JSON.stringify(event),
        qos: 0
    };
    console.log(process.env.AWS_IOT_ENDPOINT);
    return iotData.publish(params, function(err, data) {
        if (err) {
            console.log("ERROR => " + JSON.stringify(err));
        }
        else {
            console.log("Success");
        }
    }).promise();
};

Screenshots
If applicable, add screenshots to help explain your problem.

What is Configured?
If applicable, please provide what is configured for Amplify CLI:

  • Which steps did you follow via Amplify CLI when configuring your resources.
  • Which resources do you have configured?

    • If applicable, please provide your aws-exports file:

      const awsmobile = { "aws_project_region": "eu-central-1", "aws_cognito_identity_pool_id": "eu-central-1:xxx", "aws_cognito_region": "eu-central-1", "aws_user_pools_id": "eu-central-1_xxx", "aws_user_pools_web_client_id": "xxx", "oauth": {}, "aws_appsync_graphqlEndpoint": "https://xxx.appsync-api.eu-central-1.amazonaws.com/graphql", "aws_appsync_region": "eu-central-1", "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS" };

npx envinfo --system --binaries --browsers --npmPackages --npmGlobalPackages
System:

    OS: Linux 5.4 Ubuntu 18.04.5 LTS (Bionic Beaver)
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 427.82 MB / 15.49 GB
    Container: Yes
    Shell: 4.4.20 - /bin/bash
  Binaries:
    Node: 12.18.3 - ~/.nvm/versions/node/v12.18.3/bin/node
    npm: 6.14.8 - ~/.nvm/versions/node/v12.18.3/bin/npm
  Browsers:
    Chrome: 85.0.4183.121
    Firefox: 81.0.2
  npmPackages:
    @aws-amplify/ui-react: ^0.2.24 => 0.2.24
    @testing-library/jest-dom: ^4.2.4 => 4.2.4
    @testing-library/react: ^9.5.0 => 9.5.0
    @testing-library/user-event: ^7.2.1 => 7.2.1
    aws-amplify: ^3.3.4 => 3.3.4
    aws-amplify-react: ^4.2.8 => 4.2.8
    aws-iot-device-sdk: ^2.2.6 => 2.2.6
    aws-iot-device-sdk-v2: ^1.3.2 => 1.3.2
    react: ^16.14.0 => 16.14.0
    react-dom: ^16.14.0 => 16.14.0
    react-scripts: ^3.4.4 => 3.4.4
  npmGlobalPackages:
    @aws-amplify/cli: 4.29.7
    node-gyp: 7.1.2
    npm-check: 5.9.2
    npm: 6.14.8
mock pending-triage

All 5 comments

@alonsnir I will transfer this issue to cli repo where can handle it better than me.

Hi @alonsnir, can you see if adding the region in the IotData constructor params object helps?

const iotData = new AWS.IotData({ endpoint: process.env.AWS_IOT_ENDPOINT, region: 'eu-central-1' });

Hi @jhockett, that works, thanks, however am i supposed to hard-code region? I have tried process.env.REGION, but when mocking it gets resolved as us-east-1-fake
I see #4635 issue is about the same

Just to clarify what i meant by 'works' was only related to 'AWS.IotData' initialization not the entire function. This is what it says on attempt to publish on MQTT topic

ERROR => {"message":null,"code":"ForbiddenException","time":"2020-10-27T13:41:26.594Z","requestId":"2871cbd1-2e4f-d9e3-aa5f-85d5e6ef1ae7","statusCode":403,"retryable":fals
e,"retryDelay":9.883665848858758}

Apologies @alonsnir, I didn't look beyond the error message. You are correct, not all Lambda runtime environment variables are available in mock. This is the same issue as #4635 and #4234. Closing this issue as a duplicate.

Consider adding a thumbs up to those issues as we loosely prioritize enhancements based on engagement.

Was this page helpful?
0 / 5 - 0 ratings