最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

aws event bridge - How to put the Targets Input correctly on the eventbridge rule using boto3 put_targets without manually click

programmeradmin0浏览0评论
        state_machine_input = {
            'STATE_MACHINE_DEFINITION_NAME': event_dict.get('workflowToBeTriggered'),
            'LDAP_USER': event_dict.get('updatedBy')
        }
        eventbridge_client = boto3.client('events')

        response = eventbridge_client.put_targets(
            Rule=f"{EVENT_NAMING_SUFFIX}{event_dict.get('name')}",
            Targets=[
                {
                    'Id': SM_INITIATION_LAMBDA_FUNCTION_NAME,
                    'Arn': SM_INITIATION_LAMBDA_FUNCTION_ARN,
                    'Input': json.dumps(state_machine_input, indent=2)
                }
            ]
        )

I am creating an eventbridge rule to trigger a lambda, and i need to pass constant input to the target. this part is my problem: 'Input': json.dumps(state_machine_input, indent=2) that is not working on the eventbridge. the rule is being created with the Target input but it is not working.

if i remove the indent=2, and then i manually click the prettify on the aws console eventbridge, then it works.

I already tried adding these separators=(',', ':'), ensure_ascii=False nothing works. only the prettify button.

which i cant do in production, it should be working already upon deployment without manual intervention.

prettify button

        state_machine_input = {
            'STATE_MACHINE_DEFINITION_NAME': event_dict.get('workflowToBeTriggered'),
            'LDAP_USER': event_dict.get('updatedBy')
        }
        eventbridge_client = boto3.client('events')

        response = eventbridge_client.put_targets(
            Rule=f"{EVENT_NAMING_SUFFIX}{event_dict.get('name')}",
            Targets=[
                {
                    'Id': SM_INITIATION_LAMBDA_FUNCTION_NAME,
                    'Arn': SM_INITIATION_LAMBDA_FUNCTION_ARN,
                    'Input': json.dumps(state_machine_input, indent=2)
                }
            ]
        )

I am creating an eventbridge rule to trigger a lambda, and i need to pass constant input to the target. this part is my problem: 'Input': json.dumps(state_machine_input, indent=2) that is not working on the eventbridge. the rule is being created with the Target input but it is not working.

if i remove the indent=2, and then i manually click the prettify on the aws console eventbridge, then it works.

I already tried adding these separators=(',', ':'), ensure_ascii=False nothing works. only the prettify button.

which i cant do in production, it should be working already upon deployment without manual intervention.

prettify button

Share Improve this question asked Nov 20, 2024 at 6:00 MondyMondy 74 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The issue is likely due to extra whitespaces or formatting introduced by the indent=2 argument when serializing the JSON using json.dumps

Turns out the issue is easy to fix. I needed to add permission to invoke the lambda by the specific rule. It only confuses me because of the fact that the prettify button will do the work which is nice but not actually the reason why its working. It is working whenever i made any changes and save it via the aws console, that way the eventbridge is smart enough to add the necessary permission to make it. But if you create the rule via a lambda(boto3) it will not automatically add that permission.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论