-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler4.py
66 lines (54 loc) · 1.78 KB
/
handler4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import boto3
import aws_assume_role_lib
from aws_error_utils import errors
ROLE_ARN = os.environ['ROLE_ARN']
BUCKET_NAME = os.environ['BUCKET_NAME']
TABLE_NAME = os.environ['TABLE_NAME']
USE_SOURCE_IDENTITY = os.environ.get('USE_SOURCE_IDENTITY', '').lower() in ['1', 'true']
KEY = 'Function4'
SESSION = boto3.Session()
LAMBDA_ROLE_ARN = SESSION.client('sts').get_caller_identity()['Arn']
if USE_SOURCE_IDENTITY:
ASSUMED_ROLE_SESSION = aws_assume_role_lib.assume_role(
SESSION,
ROLE_ARN,
SourceIdentity=aws_assume_role_lib.generate_lambda_session_name(), # will also set RoleSessionName
)
else:
ASSUMED_ROLE_SESSION = aws_assume_role_lib.assume_role(
SESSION,
ROLE_ARN,
# RoleSessionName=aws_assume_role_lib.generate_lambda_session_name(), # optional
)
ASSUMED_ROLE_ARN = ASSUMED_ROLE_SESSION.client('sts').get_caller_identity()['Arn']
S3 = ASSUMED_ROLE_SESSION.client('s3')
DYNAMODB = ASSUMED_ROLE_SESSION.resource('dynamodb')
TABLE = DYNAMODB.Table(TABLE_NAME)
def handler(event, context):
try:
response = S3.get_object(
Bucket=BUCKET_NAME,
Key=KEY,
)
s3_result = response['Body'].read()
except errors.AccessDenied:
s3_result = "Access denied!"
except Exception as e:
s3_result = str(e)
try:
response = TABLE.get_item(
Key={'pk': KEY},
)
ddb_result = response['Item']
except errors.AccessDeniedException:
ddb_result = "Access denied!"
except Exception as e:
ddb_result = str(e)
return {
'lambda_role_arn': LAMBDA_ROLE_ARN,
'assumed_role_arn': ASSUMED_ROLE_ARN,
'use_source_identity': USE_SOURCE_IDENTITY,
's3': s3_result,
'ddb': ddb_result,
}