My setup is pretty simple. I am using sst to create a graphql lambda endpoint. Sst also creates an EventBridge Bus and an EventBridge Rules. So whenever I send the proper event to the bus this trigger a second lambda function that sends an email containing the report requested by the user. Everything works as expected till this point.
import { EventBus } from "@serverless-stack/node/event-bus";
import aws from "aws-sdk";
await new aws.EventBridge()
.putEvents({
Entries: [
{
EventBusName: EventBus["AffinityBus"]?.eventBusName,
DetailType: "reportInput",
Detail: JSON.stringify({
endDate: params.endDate,
startDate: params.startDate,
filters: params?.filters,
campaignId: params?.campaignId,
}),
Source: "sendCampaignReportByEmail",
},
],
})
.promise();
But my requirements demand that the user should be able to schedule this report email. This is the main reason I am using EventBridge in the first place. He its why I am trying to do
import {
CreateScheduleCommand,
FlexibleTimeWindowMode,
SchedulerClient,
} from "@aws-sdk/client-scheduler";
const ebc = new SchedulerClient({ region: awsConstants.AWS_REGION });
const command = new CreateScheduleCommand({
Name: "newScheduler",
GroupName: "affinity",
ScheduleExpression: `repeat(${calculateRepetition(params)} minute)`,
FlexibleTimeWindow: { Mode: FlexibleTimeWindowMode.OFF },
Target: {
Arn: "arn:aws:scheduler:::aws-sdk:eventbridge:putEvents",
RoleArn: "arn:aws:iam::xxxxx:role/name",
Input: JSON.stringify({
endDate: params.endDate,
startDate: params.startDate,
filters: params?.filters,
campaignId: params?.campaignId,
}),
},
StartDate: new Date(params.startDate),
EndDate: new Date(params.endDate),
});
await ebc.send(command)
This is the error I am facing, but that is very weird, because there is no Entries field on the awsv3 typescript types. What should I do??
2023-05-03T11:37:06.436Z c6dc2287-9444-4a58-8acd-775b70742f2d INFO ValidationException: The execution role you provide must allow AWS EventBridge Scheduler to assume the role.
at de_ValidationExceptionRes (/var/task/resolvers/query.js:49572:25)
at de_CreateScheduleCommandError (/var/task/resolvers/query.js:48998:23)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /var/task/resolvers/query.js:47664:24
at async /var/task/resolvers/query.js:52804:22
at async /var/task/resolvers/query.js:51678:42
at async /var/task/resolvers/query.js:50762:26
at async scheduleCampaignReport (/var/task/resolvers/query.js:76875:22)
at async handler149 (/var/task/resolvers/query.js:76915:23)
at async Runtime.main [as handler] (/var/task/resolvers/query.js:78241:16) {
'$fault': 'client',
'$metadata': {
httpStatusCode: 400,
requestId: '0c71a1bb-ccff-4a77-a8ed-3a6d9e8ccbb5',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
}
}
I can replicate the same error with the cli. By running the following command
aws scheduler create-schedule --name cevent-bridge-schedule --schedule-expression 'rate(5 minutes)' --target '{"Arn": "arn:aws:scheduler:::aws-sdk:eventbridge:putEvents", "RoleArn": "arn:aws:iam::xxxx:role/myrole", "Input": "{\"Entries\": [{\"EventBusName\":\"dev-affinity-appsync-AffinityBus\", \"DetailType\":\"reportInput\" , \"Detail\":\"{}\" , \"Source\": \"sendCampaignReportByEmail\" }]}" }' --flexible-time-window '{ "Mode": "OFF"}' --region us-east-1 --debug