我正在研究从 CloudTrail 获取事件并分析它们的 lambda 函数。
我有这个脚本:
s3.download_file(bucket, key, download_path)
with gzip.open(download_path, "r") as f:
data = json.loads(f.read())
print json.dumps(data)
for event in data['Records']:
if event['eventName'] in event_list:
dateEvent = datetime.strptime(event['eventTime'], "%Y-%m-%dT%H:%M:%SZ")
for element in event['userIdentity']:
for session in element[0]['sessionContext']:
username = session['userName']
role = session['arn']
我无法摆脱事件的价值userName
和arn
。我收到此错误:
string indices must be integers: TypeError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 34, in lambda_handler
for session in element[0]['sessionContext']:
TypeError: string indices must be integers
如何使它工作?什么是正确的方法?
这是json字符串:
"userIdentity": {
"principalId": "aaaaaaaaaaaaaaaaaaaa",
"accessKeyId": "aaaaaaaaaaaaaaaaaaaaa",
"sessionContext": {
"sessionIssuer": {
"userName": "aaaaaaaaaaaaa",
"type": "Role",
"arn": "arn:aws:iam::aaaaaaaaaaaaaaaaaa:role/aaaaaaa",
"principalId": "aaaaaaaaaaaaaaaaaa",
"accountId": "aaaaaaaaaaaaaaaaaaa"
},
"attributes": {
"creationDate": "2017-09-14T15:03:08Z",
"mfaAuthenticated": "false"
}
},
"type": "AssumedRole",
"arn": "aaaaaaaaaaaaaaaaaaaaaaaa",
"accountId": "aaaaaaaaaaaaaaaaaa"
},