Given the following definition ```const table = /...
# help
m
Given the following definition
Copy code
const table = /* table stack */

const api = new sst.Api(this, 'Api', {
  routes: {
    'GET /restaurants': {
      handler: `src/functions/get-restaurants.main`,
      environment: {
        RESTAURANTS_TABLE_NAME: table.dynamodbTable.tableName,
      },
    },
  },
})
I want to give that particular route only scan access to dynamodb. Is the code below correct or do I need to use to specify a complete IAM policy
Copy code
api.attachPermissionsToRoute('GET /restaurants', [table, 'dynamodb:Scan'])
Also is it possible to specify permissions in-line with the route definition?
f
You are actually granting two permissions to the Lambda function: • table - this grants
dynamodb:*
to this table • dynamodb:Scan - this grants
dynamodb:Scan
to all tables
Copy code
api.attachPermissionsToRoute('GET /restaurants', [table, 'dynamodb:Scan'])
You can just do this to grant
dynamodb:Scan
to all tables:
Copy code
api.attachPermissionsToRoute('GET /restaurants', ['dynamodb:Scan'])
Or you can do this to grant
dynamodb:Scan
to just a specific table:
Copy code
api.attachPermissionsToRoute('GET /restaurants', [
  new iam.PolicyStatement({
    actions: ["dynamodb:Scan"],
    effect: iam.Effect.ALLOW,
    resources: [ table.tableArn ],
  }),
])
m
Is it possible to specify this inline with the route definition?
f
good point. I was actually just thinking about this the other day.
Do u think you are going to have custom permissions for each route?
m
Yep .. I'm trying to follow the principle of least privilege
f
Sounds good. Lemme give it some thoughts. Most likely I will put it into the next release or the one after.
m
Appreciate the help
f
Hey @Michael Wolfenden You can now set route
permissions
inline in v0.10.11. You can also configure
permission
inside
defaultFunctionProps
. And the default permissions will be merged with the route permissions if both are configured for a given route. Here are a couple of examples - https://docs.serverless-stack.com/constructs/Api#specifying-function-props-for-all-the-routes
m
@Frank you've made me a very happy man