wing icon indicating copy to clipboard operation
wing copied to clipboard

Error: "undefined" is not valid JSON (Invalid references to Terraform tokens)

Open skyrpex opened this issue 2 years ago • 3 comments

I tried this:

bring cloud;
bring util;
bring "@cdktf/provider-aws" as tfaws;

class Table {
  table: tfaws.dynamodbTable.DynamodbTable;
  new() {
    this.table = new tfaws.dynamodbTable.DynamodbTable({
      name: this.node.addr,
      attribute: [
        {
          name: "id",
          type: "S",
        },
      ],
      hashKey: "id",
      billingMode: "PAY_PER_REQUEST",
    });
  }

  pub inflight logTableName() {
    log(this.table.name);
  }
}

let table = new Table();
new cloud.Function(inflight () => {
  table.logTableName();
});

This happened:

Invoking the function will fail with

{
  "errorType": "SyntaxError",
  "errorMessage": "\"undefined\" is not valid JSON",
  "trace": [
    "SyntaxError: \"undefined\" is not valid JSON",
    "    at JSON.parse (<anonymous>)",
    "    at <anonymous> (/var/test-function_c8a0b53c.js:15:38)",
    "    at <anonymous> (/var/test-function_c8a0b53c.js:19:11)",
    "    at Runtime.exports.handler (/var/test-function_c8a0b53c.js:27:11)",
    "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
  ]
}

The generated JS code references process.env["WING_TOKEN_TFTOKEN_TOKEN_13"]:

        const client2 = new TableClient({
          $this_table_name: JSON.parse(process.env["WING_TOKEN_TFTOKEN_TOKEN_13"])
        });

But there's no such token in main.tf.json:

        "environment": {
          "variables": {
            "NODE_OPTIONS": "--enable-source-maps",
            "WING_FUNCTION_NAME": "test-function-c8a0b53c",
            "WING_TARGET": "tf-aws",
            "WING_TOKEN_TFTOKEN_TOKEN_16": "${jsonencode(aws_dynamodb_table.test-table_tfawsdynamodbTableDynamodbTable_EE607F2C.name)}",
            "WING_TOKEN_TFTOKEN_TOKEN_17": "${jsonencode(aws_dynamodb_table.test-table_tfawsdynamodbTableDynamodbTable_EE607F2C.name)}"
          }
        },

I expected this:

It should work.

Is there a workaround?

Yes. If you store the table name as a property during construction, it will work.

bring cloud;
bring util;
bring "@cdktf/provider-aws" as tfaws;

class Table {
  tableName: str;
  new() {
    let table = new tfaws.dynamodbTable.DynamodbTable({
      name: this.node.addr,
      attribute: [
        {
          name: "id",
          type: "S",
        },
      ],
      hashKey: "id",
      billingMode: "PAY_PER_REQUEST",
    });
    this.tableName = table.name;
  }

  pub inflight logTableName() {
    log(this.tableName);
  }
}

let table = new Table();
new cloud.Function(inflight () => {
  table.logTableName();
});

Anything else?

No response

Wing Version

0.59.46

Node.js Version

No response

Platform(s)

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.

skyrpex avatar Mar 12 '24 08:03 skyrpex

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] avatar Jun 20 '24 06:06 github-actions[bot]

This still fails

staycoolcall911 avatar Jun 21 '24 14:06 staycoolcall911

Hi,

This issue hasn't seen activity in 90 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!

github-actions[bot] avatar Sep 20 '24 06:09 github-actions[bot]