boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
8.96k stars 1.86k forks source link

Cannot update EventBridge event #3558

Open FANMixco opened 1 year ago

FANMixco commented 1 year ago

Describe the bug

Fellows,

I'm trying to update an event in my EventBridge, but I'm constantly getting this error:

An error occurred (ValidationException) when calling the UpdateSchedule operation: scheduler is not a supported service for a target.

import json
import boto3

from datetime import datetime, timedelta

start_api = datetime.now() + timedelta(minutes=60)
scheduler_client = boto3.client('scheduler')

def lambda_handler(event, context):

    scheduler_client.update_schedule(Name='MY_EVENT_NAME',
                                     ScheduleExpression=f'cron({start_api.minute} {start_api.hour} {start_api.day} {start_api.month} ? {start_api.year})',
                                     FlexibleTimeWindow={
                                        'MaximumWindowInMinutes': 1,
                                        'Mode': 'FLEXIBLE'
                                     },
                                     Target={
                                         'Arn': 'arn:aws:scheduler:MY_ARN',
                                         'RoleArn': 'arn:aws:iam::MY_ROLE_ARN'
                                     }
                                 )

    return {
        'statusCode': 200,
        'body': json.dumps('Working!')
    }

I even provided the admin role permissions to my Role and don't know why I'm getting this error. Any idea what am I doing wrong?

Expected Behavior

It should update the event.

Current Behavior

It's giving this error:

An error occurred (ValidationException) when calling the UpdateSchedule operation: scheduler is not a supported service for a target.

Reproduction Steps

  1. Create a Lambda Function.
  2. Download Boto3 to your PC: pip install boto3 -t ./python.
  3. Create a zip: zip -r layer.zip ./python
  4. Import Boto3 as a layer.
  5. Copy and paste the following code:
import json
import boto3

from datetime import datetime, timedelta

start_api = datetime.now() + timedelta(minutes=60)
scheduler_client = boto3.client('scheduler')

def lambda_handler(event, context):

    scheduler_client.update_schedule(Name='MY_EVENT_NAME',
                                     ScheduleExpression=f'cron({start_api.minute} {start_api.hour} {start_api.day} {start_api.month} ? {start_api.year})',
                                     FlexibleTimeWindow={
                                        'MaximumWindowInMinutes': 1,
                                        'Mode': 'FLEXIBLE'
                                     },
                                     Target={
                                         'Arn': 'arn:aws:scheduler:MY_ARN',
                                         'RoleArn': 'arn:aws:iam::MY_ROLE_ARN'
                                     }
                                 )

    return {
        'statusCode': 200,
        'body': json.dumps('Working!')
    }
  1. Give permission to the Lambda function to modify the EventBridge.
  2. Create a new test event.
  3. Run it.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.26.50

Environment details (OS name and version, etc.)

AWS, Lambda

aBurmeseDev commented 1 year ago

Hi @FANMixco - thanks for reaching out and sorry to hear about the issue.

I attempted to replicate the issue with the simple code snippet below and wasn't able reproduce the same behavior. According to API docs, the error you're getting indicates that "The input fails to satisfy the constraints specified by an AWS service.".

After deep dive and looking at your code, I suspect the issue might be with ScheduleExpression expression. Have you tried other expressions besides cron format?

Here's more on the schedule types and expressions for your reference: https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html

If the issue persists, please share debug logs without any sensitive information by adding boto3.set_stream_logger('') to your script. That would give us more insights on locating the issue. Thanks John

client = boto3.client('scheduler')

response = client.update_schedule(
    FlexibleTimeWindow={
        'MaximumWindowInMinutes': 123,
        'Mode': 'FLEXIBLE'
    },
    Name='string',
    ScheduleExpression='cron({start_api.minute} {start_api.hour} {start_api.day} {start_api.month} ? {start_api.year})',
    State='ENABLED',
    Target={
        'Arn': 'string',
        'RoleArn': 'string',
    }
)
print(response)
FANMixco commented 1 year ago

Hi @FANMixco - thanks for reaching out and sorry to hear about the issue.

I attempted to replicate the issue with the simple code snippet below and wasn't able reproduce the same behavior. According to API docs, the error you're getting indicates that "The input fails to satisfy the constraints specified by an AWS service.".

After deep dive and looking at your code, I suspect the issue might be with ScheduleExpression expression. Have you tried other expressions besides cron format?

Here's more on the schedule types and expressions for your reference: https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html

If the issue persists, please share debug logs without any sensitive information by adding boto3.set_stream_logger('') to your script. That would give us more insights on locating the issue. Thanks John

client = boto3.client('scheduler')

response = client.update_schedule(
    FlexibleTimeWindow={
        'MaximumWindowInMinutes': 123,
        'Mode': 'FLEXIBLE'
    },
    Name='string',
    ScheduleExpression='cron({start_api.minute} {start_api.hour} {start_api.day} {start_api.month} ? {start_api.year})',
    State='ENABLED',
    Target={
        'Arn': 'string',
        'RoleArn': 'string',
    }
)
print(response)

Hi @aBurmeseDev, after a call with the AWS support team, we concluded a couple of things:

Arn

RoleArn

I even wrote a tutorial since there are not many examples:

https://dev.to/fanmixco/how-to-update-eventbridge-schedules-with-lambdas-2482

What's more, I suggested to them that there are some values that should be taken by default if you don't provide anything:

These values make sense to be mandatory when you're creating a schedule, not when you're updating one.