https://serverless-stack.com/ logo
#sst
Title
# sst
ö

Ömer Toraman

11/13/2021, 10:06 PM
What could I be doing wrong here when I’m trying to use an existing EventBus?
Copy code
import * as sst from "@serverless-stack/resources"
import * as events from '@aws-cdk/aws-events'

new sst.EventBus(this, 'myBus', {
    eventBridgeEventBus: {
      eventBusArn: events.EventBus.fromEventBusArn(this, 'ImportedBus', 'eventBusArn'
     }
})
Seems the same with an example in the docs: https://docs.serverless-stack.com/constructs/EventBus#importing-an-existing-eventbus
t

thdxr

11/13/2021, 10:07 PM
missing a parentheses? haha
what's the error
ö

Ömer Toraman

11/13/2021, 10:07 PM
Copy code
Type 'IEventBus' is not assignable to type 'string'.ts(2322)
And when I convert it to some random string,
Copy code
Type '{ eventBusArn: string; }' is not assignable to type 'IEventBus | EventBusProps | undefined'.
lol
I guess it should be like this:
Copy code
const ev = events.EventBus.fromEventBusArn(this, 'ImportedBus', 'order')
            new sst.EventBus(this, 'myBus', {
                  eventBridgeEventBus: ev
            })
t

thdxr

11/13/2021, 10:14 PM
ah yeah was just about to say that
ö

Ömer Toraman

11/13/2021, 10:15 PM
How can I add permission to my lambda to put events?
So lambda will be the source of events
Basically I want to add permission for
putEvents
to the route handler
Copy code
const api = new sst.Api(this, "Api", {
                  routes: {
                        "POST /": "src/lambda.handler",
                  },
            });

            const ev = events.EventBus.fromEventBusName(this, 'ImportedBus', 'order')
            new sst.EventBus(this, 'myBus', {
                  eventBridgeEventBus: ev
            })
t

thdxr

11/13/2021, 10:17 PM
Copy code
"POST /": {
  permissions: [bus],
  handler: "src/lambda.handler"
}
ö

Ömer Toraman

11/13/2021, 10:17 PM
thank you so much
t

thdxr

11/13/2021, 10:18 PM
if you want all functions in the api to be able to you can do it in
defaultFunctionProps
in the api
ö

Ömer Toraman

11/13/2021, 10:19 PM
And if I want to add only
putEvents
?
array of arrays I guess
Copy code
[ [bus, "putEvents"] ]
t

thdxr

11/13/2021, 10:21 PM
yeah I think it would be
grantPutEvents
but yeah
ö

Ömer Toraman

11/13/2021, 10:22 PM
Do we have a package for that as well, like some enum for a resource?
t

thdxr

11/13/2021, 10:27 PM
you can typically just see in autocomplete on the eventBus construct what
grant
methods it has
it's just a fancy way of calling that function with the lambda as a target
We'vebeen considering changing the api
ö

Ömer Toraman

11/13/2021, 10:28 PM
Yes I saw this method as well
So permissions for cdk is always prepended with
grants
?
I mean for CloudFormation or SAM, it’s like for example
putEvents
,
PutItem
etc
so it is whatever the name is
i.e., in this case
*grantPutEventsTo*
That’s what I deduce from this error
I created a PR: https://github.com/serverless-stack/serverless-stack/pull/1031 😄 Does this mean that we can only specify what is available on the construct?
I ended up not using
sst.EventBus
at all. I could not attach permission using it. This is what I did:
Copy code
import * as sst from "@serverless-stack/resources"
import * as events from '@aws-cdk/aws-events'
export class MainStack extends sst.Stack {
      constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
            super(scope, id, props);

            const ev = events.EventBus.fromEventBusName(this, 'ImportedBus', 'orders')
            const bus = new sst.EventBus(this, 'myBus', {
                  eventBridgeEventBus: ev
            })

            // Create a HTTP API
            const api = new sst.Api(this, "Api", {
                  routes: {
                        "POST /": {
                              permissions: [[bus, 'grantPutEventsTo']],
                              handler: "src/lambda.handler",
                        }
                  },
            });

            // Show the endpoint in the output
            this.addOutputs({
                  "ApiEndpoint": api.url,
            });
      }
}
And I kept getting the error above, and it makes sense honestly, since
sst.EventBus
does not have
grantPutEventsTo
instance method.
So I ended up using this:
Copy code
import * as sst from "@serverless-stack/resources"
import * as events from '@aws-cdk/aws-events'
export class MainStack extends sst.Stack {
      constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
            super(scope, id, props);

            const ev = events.EventBus.fromEventBusName(this, 'ImportedBus', 'orders')

            // Create a HTTP API
            const api = new sst.Api(this, "Api", {
                  routes: {
                        "POST /": {
                              // @ts-expect-error
                              permissions: [[ev, 'grantPutEventsTo']],
                              handler: "src/lambda.handler",
                        }
                  },
            });

            // Show the endpoint in the output
            this.addOutputs({
                  "ApiEndpoint": api.url,
            });
      }
}
And it worked, now I see that the Lambda has a permission to putEvents, however, I had to suprass typescript error, as it did not accept
ev
d

Derek Kershner

11/15/2021, 9:40 PM
ö

Ömer Toraman

11/15/2021, 10:30 PM
Nice catch @Derek Kershner Because when I got an error I took a look the source code and the first argument to the permissions doesn’t seem to require to implement the interface. It just needs to have the same method
d

Derek Kershner

11/15/2021, 10:30 PM
according to the fellas, it should be covered this sprint.
ö

Ömer Toraman

11/15/2021, 10:32 PM
I had to take a look at the source code when I got the error resulting from undefined cannot be called here: construct[methodName]()
Even created a PR and thankfully Dax merged it.
Nice catch. How do you handle the TS problem by the way?
d

Derek Kershner

11/15/2021, 10:37 PM
// @ts-expect-error
ö

Ömer Toraman

11/15/2021, 10:47 PM
then u get an eslint error right 😄