I’m trying to attach specific permissions and runn...
# help
c
I’m trying to attach specific permissions and running into an error. This syntax works –
Copy code
api.attachPermissionsToRoute('POST /', [eventHubBus]);
This syntax…
Copy code
api.attachPermissionsToRoute('POST /', [[eventHubBus, 'grantPutEventsTo']]);
…throws an error –
Copy code
TypeError: construct[methodName] is not a function
This seems to happen with using either ``attachPermissions` or
.attachPermissionsToRoute
. Am I monkeying up the syntax somehow or is there be another issue that might cause this? Thanks
Doing it via adding a list of IAM Policies does work but requires import of iam and of course is a lot more verbose –
Copy code
api.attachPermissionsToRoute('POST /', [
  new iam.PolicyStatement({
    actions: ["events:PutEvents"],
    effect: iam.Effect.ALLOW,
    resources: [ eventHubBus.eventBusArn ],
  }),
]);
t
is
eventHubBus
an sst construct or cdk
c
It is an SST construct created from an existing bus -
Copy code
const eventHubBus = new sst.EventBus(this, 'Event Hub Bus', {
  eventBridgeEventBus: events.EventBus.fromEventBusArn(this, 'EventHubBus', subsystemEventHubBusArn),
});
t
try this
Copy code
[eventHubBus.eventBridgeEventBus, "grantPutEventsTo"]
We definitely need to improve this, seems like people get tripped up by it a lot
c
When I try that syntax to reference the internal CDK bus I get this error -
Copy code
error TS2740: Type 'IEventBus' is missing the following properties from type 'Construct': onValidate, onPrepare, onSynthesize, validate, and 2 more.
t
Looks like we have too broad of a type on that. This is verbose but can you do
Copy code
[<events.EventBus>eventHubBus.eventBridgeEventBus, "grantPutEventsTo"]
@Frank curious if you considered making this api
Copy code
[eventBus.grantPutEventsTo]
Instead of a tuple with a string for the function name?
c
Ah, interesting. That worked. Thanks Dax. Out of curiosity - what is this additional syntax at the head with the carets called?
<events.EventBus>
t
In a typical statically typed language it's called casting, not sure if TS has different terminology
f
@thdxr
[eventBus.grantPutEventsTo]
Yeah definitely looks cleaner.. I wasn’t able to get it to work 😅 and went with the current way we have.
That said, I discovered after that some
grantX
calls take more than 1 argument. Not sure how the syntax will look like.