-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
104 lines (95 loc) · 3.25 KB
/
Copy pathutils.py
File metadata and controls
104 lines (95 loc) · 3.25 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import boto3
import json
from boto3.session import Session
import botocore
import requests
import time
s3 = boto3.client("s3")
def create_agentcore_gateway_role(gateway_name):
iam_client = boto3.client('iam')
agentcore_gateway_role_name = f'agentcore-{gateway_name}-role'
boto_session = Session()
region = boto_session.region_name
account_id = boto3.client("sts").get_caller_identity()["Account"]
role_policy = {
"Version": "2012-10-17",
"Statement": [{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"bedrock-agentcore:*",
"bedrock:*",
"agent-credential-provider:*",
"iam:PassRole",
"secretsmanager:GetSecretValue",
"lambda:InvokeFunction"
],
"Resource": "*"
}
]
}
assume_role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AssumeRolePolicy",
"Effect": "Allow",
"Principal": {
"Service": "bedrock-agentcore.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"aws:SourceAccount": f"{account_id}"
},
"ArnLike": {
"aws:SourceArn": f"arn:aws:bedrock-agentcore:{region}:{account_id}:*"
}
}
}
]
}
assume_role_policy_document_json = json.dumps(
assume_role_policy_document
)
role_policy_document = json.dumps(role_policy)
# Create IAM Role for the Lambda function
try:
agentcore_iam_role = iam_client.create_role(
RoleName=agentcore_gateway_role_name,
AssumeRolePolicyDocument=assume_role_policy_document_json
)
# Pause to make sure role is created
time.sleep(10)
except iam_client.exceptions.EntityAlreadyExistsException:
print("Role already exists -- deleting and creating it again")
policies = iam_client.list_role_policies(
RoleName=agentcore_gateway_role_name,
MaxItems=100
)
print("policies:", policies)
for policy_name in policies['PolicyNames']:
iam_client.delete_role_policy(
RoleName=agentcore_gateway_role_name,
PolicyName=policy_name
)
print(f"deleting {agentcore_gateway_role_name}")
iam_client.delete_role(
RoleName=agentcore_gateway_role_name
)
print(f"recreating {agentcore_gateway_role_name}")
agentcore_iam_role = iam_client.create_role(
RoleName=agentcore_gateway_role_name,
AssumeRolePolicyDocument=assume_role_policy_document_json
)
# Attach the AWSLambdaBasicExecutionRole policy
print(f"attaching role policy {agentcore_gateway_role_name}")
try:
iam_client.put_role_policy(
PolicyDocument=role_policy_document,
PolicyName="AgentCorePolicy",
RoleName=agentcore_gateway_role_name
)
except Exception as e:
print(e)
return agentcore_iam_role