I'm having some trouble with referencing a Table i...
# help
p
I'm having some trouble with referencing a Table in a different stack than it was created in. The problem appears when I call
npx sst build
- I get the following error (followed by a stacktrace I can post if helpful)
Copy code
Error: Resolution error: Resolution error: Resolution error: Trying to resolve() a Construct at /Resources/${Token[ps-dev-sketching-stack-websocket.websocket-api..connect.Resource.LogicalID.352]}/Properties/environment/variables/table_games_live/node.
If I stop referencing the table across stacks, everything builds just fine - though I can't access the table then. It seems like this should work, according to docs here: https://docs.serverless-stack.com/advanced/cross-stack-references I'll post code snippets in the thread.
Somewhat simplified:
index.js
Copy code
function main(app) 
{
  let stack_games = new Stack_Games(app, "stack-games", {});
  let stack_ws = new Stack_Websocket(app, "stack-websocket", { table: stack_games.table_games_live });
}
Somewhat simplified:
Stack_Websocket.js
Copy code
export class 
Stack_Websocket extends sst.Stack 
{
  constructor(scope, id, props) 
  {
    super(scope, id, props);
    let ws = new sst.WebSocketApi(this, "websocket-api", {
      defaultFunctionProps: { 
        environment: { table: props.table },
      },
      routes: { /* ...omitted for brevity, they work if I omit the environment above */ },
    });
    ws.attachPermissions([ props.table ]);
    this.addOutput({ ApiEndpoint: ws.url });
  }
}
t
Hey we're in the middle of transitioning some docs so if you don't mind a bit of reworking this is our new method: https://docs.serverless-stack.com/constructs/v1/Stack#sharing-resources-between-stacks
This uses our new functional stack concept
Can you share stack games stack code as well
p
Thanks! I don't mind reworking at all, but I'm not well versed in typescript - how would I use the
StackContext
in a javascript codebase?
Yep - I'm omitting the other things the stack does which are: creating another table (creation looks very similar to table_games_live) and adding lambdas to an api passed in via props.
Games_Stack.js
Copy code
export class 
Games_Stack extends sst.Stack 
{
  table_games_live;
  constructor(scope, id, props)
  {
    super(scope, id, props);
    this.table_games_live = new sst.Table(this, "games-live", {
      fields: { 
        systemId: sst.TableFieldType.STRING, 
        gameId:   sst.TableFieldType.STRING,
        // more fields all declared this way
      },
      primaryIndex: {
        partitionKey: "systemId",
        sortKey: "gameId"
      }
    });
  }
}
t
You can actually just ignore the StackContext part
Everything else should work
p
yea I misread the docs, I thought you were specifying the function type somehow, not the argument to it
Thanks!
t
I think your issue might be you're passing the table construct as an environment variable when maybe you want to pass table name?
props.table.tableName
p
Oh. yep!
Yea, just got the whole thing up and running with
npx sst start
and it seems to be working - Thank you for helping!