What could I be doing wrong here when I’m trying t...
# sst
ö
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
missing a parentheses? haha
what's the error
ö
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
ah yeah was just about to say that
ö
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
Copy code
"POST /": {
  permissions: [bus],
  handler: "src/lambda.handler"
}
ö
thank you so much
t
if you want all functions in the api to be able to you can do it in
defaultFunctionProps
in the api
ö
And if I want to add only
putEvents
?
array of arrays I guess
Copy code
[ [bus, "putEvents"] ]
t
yeah I think it would be
grantPutEvents
but yeah
ö
Do we have a package for that as well, like some enum for a resource?
t
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
ö
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
ö
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
according to the fellas, it should be covered this sprint.
ö
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
// @ts-expect-error
ö
then u get an eslint error right 😄