Trying to create an EventBridge rule to get event 'CreateRole' from source 'aws.iam'.
The events coming from cloudtrail is having an array 'resources' with 3 elements, rolename, arn, account_id. And they have a pattern like:
"Resources": [
{
"ResourceType": "AWS::IAM::Role",
"ResourceName": "********gdggdgd***"
},
{
"ResourceType": "AWS::IAM::Role",
"ResourceName": "arn:aws:iam::<account_id>:role/sample-test-5"
},
{
"ResourceType": "AWS::IAM::Role",
"ResourceName": "sample-test-5"
}
]
But the order of these elements is random, in some events role name - sample-test-5 comes as 1st element and in some events it comes in mid or last.
Now inside lambda, using json to extract the rolename, like this:
role_name = event['Resources'][0]['ResourceName']
so that using role_name can get a client for boto3. But because of random order of elements in array, in some cases unable to get correct role_name and fails to get boto3 client.
Trying to get the rolename directly using boto3 client but that's unavailable. To get the role, parameter rolename needs to be passed:
response = client.get_role(
RoleName='string'
)
So how can I get the rolename from the raw events having order of elements in a random faishon.
Can I also define a rule in EventBridge rule, so that the sent events comes in a pattern specified by user?
Please share any pointers or examples.
Thanks