Skip to content

ValidationException: Invalid Schedule Expression at(2024-10-20T17:19:10Z) - EventBridge SDK - TypeScript

0

Lambda function name - sendTgNtfcnAppointmentEnter image description here

My code

 const scheduleExpression = "2024-10-20T17:19:10Z"

  const params = {
    Name: initialEmailScheduleName,
    ScheduleExpression: `at(${scheduleExpression})`, // Correct format
    Target: {
      Arn: `arn:aws:lambda:${process.env.NEXT_PUBLIC_AWS_REGION}:${process.env.AWS_ACCOUNT_ID}:function:${process.env.NEXT_PUBLIC_LAMBDA_FN_NAME}`,
      RoleArn: `arn:aws:iam::${process.env.AWS_ACCOUNT_ID}:role/service-role/SendScheduledEmails-role-wk1opcl8`, // this role allows invoke lambda function
      Input: JSON.stringify({
        dateTime: formatTime(newScheduledAtISO),
        message,
        channel,
        sendNotificationTo,
        inputNotificationTo,
      }),
    } as Target,
    FlexibleTimeWindow: {
      Mode: "OFF" as const,
    },
    State: "ENABLED" as const,
  }

 try {
    const command = new CreateScheduleCommand(params)
    const response = await client.send(command)
    console.log(58, "response - ", response)
  } catch (error) {
    console.error("Error creating one-time schedule:", error)
  }

Expected result

I create 1 time schedule with EventBridge SDK

Current result

I see error

Error creating one-time schedule: ValidationException: Invalid Schedule Expression at(2024-10-20T17:19:10Z).
    at de_ValidationExceptionRes (webpack-internal:///(action-browser)/./node_modules/.pnpm/@aws-sdk+client-scheduler@3.675.0/node_modules/@aws-sdk/client-scheduler/dist-es/protocols/Aws_restJson1.js:493:23)
    at de_CommandError (webpack-internal:///(action-browser)/./node_modules/.pnpm/@aws-sdk+client-scheduler@3.675.0/node_modules/@aws-sdk/client-scheduler/dist-es/protocols/Aws_restJson1.js:410:25)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (webpack-internal:///(action-browser)/./node_modules/.pnpm/@smithy+middleware-serde@3.0.7/node_modules/@smithy/middleware-serde/dist-es/deserializerMiddleware.js:8:24)
    at async eval (webpack-internal:///(action-browser)/./node_modules/.pnpm/@smithy+core@2.4.8/node_modules/@smithy/core/dist-es/middleware-http-signing/httpSigningMiddleware.js:25:20)
    at async eval (webpack-internal:///(action-browser)/./node_modules/.pnpm/@smithy+middleware-retry@3.0.23/node_modules/@smithy/middleware-retry/dist-es/retryMiddleware.js:41:46)
    at async eval (webpack-internal:///(action-browser)/./node_modules/.pnpm/@aws-sdk+middleware-logger@3.667.0/node_modules/@aws-sdk/middleware-logger/dist-es/loggerMiddleware.js:9:26)
    at async scheduleTgNtfctnAction (webpack-internal:///(action-browser)/./app/(site)/actions/scheduleTgNtfctnAction.ts:58:26)
    at async /home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:39:418
    at async rw (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:38:7978)
    at async r6 (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js:41:1256)
    at async doRender (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1438:30)
    at async cacheEntry.responseCache.get.routeKind (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1599:28)
    at async DevServer.renderToResponseWithComponentsImpl (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1507:28)
    at async DevServer.renderPageComponent (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1931:24)
    at async DevServer.renderToResponseImpl (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:1969:32)
    at async DevServer.pipeImpl (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/base-server.js:816:17)
    at async /home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/home/kali/Documents/GitHub/14_portfolio/node_modules/.pnpm/next@14.2.5_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/next/dist/server/lib/start-server.js:141:13) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '32aed00f-4c11-40ed-b7e3-193ca7c69cff',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  }
}

Here is policy that I added in permission

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "iam:PassRole",
            "Resource": "arn:aws:iam::ACCOUNTID:role/service-role/SendScheduledEmails-role-wk1opcl8",
            "Condition": {
                "StringEquals": {
                    "aws:userid": "arn:aws:iam::ACCOUNTID:user/nicitaacom"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "scheduler:CreateSchedule",
                "scheduler:DeleteSchedule",
                "scheduler:GetSchedule",
                "scheduler:UpdateSchedule",
                "events:PutRule",
                "events:PutTargets",
                "events:DescribeRule",
                "events:DeleteRule",
                "events:RemoveTargets"
            ],
            "Resource": [
                "arn:aws:scheduler:eu-north-1:ACCOUNTID:schedule/default/*",
                "arn:aws:lambda:eu-north-1:ACCOUNTID:function:SendScheduledEmails",
                "arn:aws:lambda:eu-north-1:ACCOUNTID:function:sendTgNtfcnAppointment",
                "arn:aws:events:eu-north-1:ACCOUNTID:rule/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "lambda:InvokeFunction"
            ],
            "Resource": [
                "arn:aws:lambda:eu-central-1:ACCOUNTID:function:testLambdaFnTgNtfctn:*",
                "arn:aws:lambda:eu-central-1:ACCOUNTID:function:testLambdaFnTgNtfctn",
                "arn:aws:lambda:eu-central-1:ACCOUNTID:function:sendTgNtfcnAppointment:*",
                "arn:aws:lambda:eu-central-1:ACCOUNTID:function:sendTgNtfcnAppointment",
                "arn:aws:lambda:eu-north-1:ACCOUNTID:function:SendScheduledEmails"
            ]
        }
    ]
}

Note - I changed my actuall account id to ACCOUNTID

2 Answers
1
Accepted Answer

Hello.

Make the code as below.
As stated in the documentation, the schedule format is "at(yyyy-mm-ddThh:mm:ss)".
If you want to specify the time zone of the timestamp, please set it in "ScheduleExpressionTimezone".
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/scheduler/command/CreateScheduleCommand/

const scheduleExpression = "2024-10-20T17:19:10"
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Wrong

const scheduleExpression = "2024-10-20T17:19:10Z"

Right

const scheduleExpression = "2024-10-20T17:19:10"

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.