I’m trying to make use of an existing table in my ...
# sst
r
I’m trying to make use of an existing table in my stack because despite using a PolicyStatement in my stack that looks like this:
Copy code
new PolicyStatement({
      actions: ['dynamodb:*'],
      effect: Effect.ALLOW,
      resources: [
        'arn:aws:dynamodb:eu-west-2:12345667889:table/mytable/*',
        'arn:aws:dynamodb:eu-west-2:12345667889:table/mytable',
      ],
    });
I’m still getting an error about not having the permission to PutItem so I wanted to try adding consumers to a table construct instead but I don’t quite understand the sample. From the doc here it shows:
Copy code
import { Table } from "@aws-cdk/aws-dynamodb";

new Table(this, "Table", {
  dynamodbTable: Table.fromTableArn(stack, "MyDynamoDBTable", tableArn),
});
Is this something i need to do from within my SST stack? If so, what is
stack
referencing?
f
Sorry are you not able to adding the PutItem permission to the existing table? Or are you looking for an example of adding consumers?
r
I want my lambda to have PutItem permission to a table that has already been defined within AWS
There may be a better way to achieve this. Basically, there’s an existing app with a tonne of data in DDB table that I’m migrating to SST
f
Say u r importing a table like this:
Copy code
import dynamodb from "@aws-cdk/aws-dynamodb";

const existingTable = dynamodb.Table.fromTableArn(this, "ImportedTable", tableArn);
const table = new sst.Table(this, "MyTable", {
  dynamodbTable: existingTable,
});
You can grant the permission like this:
Copy code
const fn = new sst.Function(this, "MyFunction", ...);
fn.attachPermissions([table]);
And similarly u can grant the permission to all function in an api like this:
Copy code
const api = new sst.ApiGatewayV1Api(this, "MyApi", ...);
api.attachPermissions([table]);
r
Cool - I’ll give that a try - the docs are very slightly different, they have new Table(this, “Table”, { dynamodbTable: Table.fromTableArn(stack, “MyDynamoDBTable”, tableArn), });
f
Yeah.. i think the two `Table`s in the snippet is confusing… and where that
stack
comes from..
Just updated the doc.
r
Cool - that worked too, thanks again.
f
👍 Thanks for pointing it out