File size: 10,462 Bytes
b20d059 |
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
import boto3
import json
import time
import zipfile
from io import BytesIO
iam_client = boto3.client('iam')
sts_client = boto3.client('sts')
session = boto3.session.Session()
region = session.region_name
account_id = sts_client.get_caller_identity()["Account"]
dynamodb_client = boto3.client('dynamodb')
dynamodb_resource = boto3.resource('dynamodb')
lambda_client = boto3.client('lambda')
bedrock_agent_client = boto3.client('bedrock-agent')
def create_dynamodb(table_name):
table = dynamodb_resource.create_table(
TableName=table_name,
KeySchema=[
{
'AttributeName': 'booking_id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'booking_id',
'AttributeType': 'S'
}
],
BillingMode='PAY_PER_REQUEST' # Use on-demand capacity mode
)
# Wait for the table to be created
print(f'Creating table {table_name}...')
table.wait_until_exists()
print(f'Table {table_name} created successfully!')
return
def create_lambda(lambda_function_name, lambda_iam_role):
# add to function
# Package up the lambda function code
s = BytesIO()
z = zipfile.ZipFile(s, 'w')
z.write("lambda_function.py")
z.close()
zip_content = s.getvalue()
# Create Lambda Function
lambda_function = lambda_client.create_function(
FunctionName=lambda_function_name,
Runtime='python3.12',
Timeout=60,
Role=lambda_iam_role['Role']['Arn'],
Code={'ZipFile': zip_content},
Handler='lambda_function.lambda_handler'
)
return lambda_function
def create_lambda_role(agent_name, dynamodb_table_name):
lambda_function_role = f'{agent_name}-lambda-role'
dynamodb_access_policy_name = f'{agent_name}-dynamodb-policy'
# Create IAM Role for the Lambda function
try:
assume_role_policy_document = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
assume_role_policy_document_json = json.dumps(assume_role_policy_document)
lambda_iam_role = iam_client.create_role(
RoleName=lambda_function_role,
AssumeRolePolicyDocument=assume_role_policy_document_json
)
# Pause to make sure role is created
time.sleep(10)
except iam_client.exceptions.EntityAlreadyExistsException:
lambda_iam_role = iam_client.get_role(RoleName=lambda_function_role)
# Attach the AWSLambdaBasicExecutionRole policy
iam_client.attach_role_policy(
RoleName=lambda_function_role,
PolicyArn='arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
)
# Create a policy to grant access to the DynamoDB table
dynamodb_access_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
],
"Resource": "arn:aws:dynamodb:{}:{}:table/{}".format(
region, account_id, dynamodb_table_name
)
}
]
}
# Create the policy
dynamodb_access_policy_json = json.dumps(dynamodb_access_policy)
dynamodb_access_policy_response = iam_client.create_policy(
PolicyName=dynamodb_access_policy_name,
PolicyDocument=dynamodb_access_policy_json
)
# Attach the policy to the Lambda function's role
iam_client.attach_role_policy(
RoleName=lambda_function_role,
PolicyArn=dynamodb_access_policy_response['Policy']['Arn']
)
return lambda_iam_role
def create_agent_role_and_policies(agent_name, agent_foundation_model, kb_id=None):
agent_bedrock_allow_policy_name = f"{agent_name}-ba"
agent_role_name = f'AmazonBedrockExecutionRoleForAgents_{agent_name}'
# Create IAM policies for agent
statements = [
{
"Sid": "AmazonBedrockAgentBedrockFoundationModelPolicy",
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": [
f"arn:aws:bedrock:{region}::foundation-model/{agent_foundation_model}"
]
}
]
# add Knowledge Base retrieve and retrieve and generate permissions if agent has KB attached to it
if kb_id:
statements.append(
{
"Sid": "QueryKB",
"Effect": "Allow",
"Action": [
"bedrock:Retrieve",
"bedrock:RetrieveAndGenerate"
],
"Resource": [
f"arn:aws:bedrock:{region}:{account_id}:knowledge-base/{kb_id}"
]
}
)
bedrock_agent_bedrock_allow_policy_statement = {
"Version": "2012-10-17",
"Statement": statements
}
bedrock_policy_json = json.dumps(bedrock_agent_bedrock_allow_policy_statement)
agent_bedrock_policy = iam_client.create_policy(
PolicyName=agent_bedrock_allow_policy_name,
PolicyDocument=bedrock_policy_json
)
# Create IAM Role for the agent and attach IAM policies
assume_role_policy_document = {
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"Service": "bedrock.amazonaws.com"
},
"Action": "sts:AssumeRole"
}]
}
assume_role_policy_document_json = json.dumps(assume_role_policy_document)
agent_role = iam_client.create_role(
RoleName=agent_role_name,
AssumeRolePolicyDocument=assume_role_policy_document_json
)
# Pause to make sure role is created
time.sleep(10)
iam_client.attach_role_policy(
RoleName=agent_role_name,
PolicyArn=agent_bedrock_policy['Policy']['Arn']
)
return agent_role
def delete_agent_roles_and_policies(agent_name):
agent_bedrock_allow_policy_name = f"{agent_name}-ba"
agent_role_name = f'AmazonBedrockExecutionRoleForAgents_{agent_name}'
dynamodb_access_policy_name = f'{agent_name}-dynamodb-policy'
lambda_function_role = f'{agent_name}-lambda-role'
for policy in [agent_bedrock_allow_policy_name]:
try:
iam_client.detach_role_policy(
RoleName=agent_role_name,
PolicyArn=f'arn:aws:iam::{account_id}:policy/{policy}'
)
except Exception as e:
print(f"Could not detach {policy} from {agent_role_name}")
print(e)
for policy in [dynamodb_access_policy_name]:
try:
iam_client.detach_role_policy(
RoleName=lambda_function_role,
PolicyArn=f'arn:aws:iam::{account_id}:policy/{policy}'
)
except Exception as e:
print(f"Could not detach {policy} from {lambda_function_role}")
print(e)
try:
iam_client.detach_role_policy(
RoleName=lambda_function_role,
PolicyArn='arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'
)
except Exception as e:
print(f"Could not detach AWSLambdaBasicExecutionRole from {lambda_function_role}")
print(e)
for role_name in [agent_role_name, lambda_function_role]:
try:
iam_client.delete_role(
RoleName=role_name
)
except Exception as e:
print(f"Could not delete role {role_name}")
print(e)
for policy in [agent_bedrock_allow_policy_name, dynamodb_access_policy_name]:
try:
iam_client.delete_policy(
PolicyArn=f'arn:aws:iam::{account_id}:policy/{policy}'
)
except Exception as e:
print(f"Could not delete policy {policy}")
print(e)
def clean_up_resources(
table_name, lambda_function, lambda_function_name, agent_action_group_response, agent_functions,
agent_id, kb_id, alias_id
):
action_group_id = agent_action_group_response['agentActionGroup']['actionGroupId']
action_group_name = agent_action_group_response['agentActionGroup']['actionGroupName']
# Delete Agent Action Group, Agent Alias, and Agent
try:
bedrock_agent_client.update_agent_action_group(
agentId=agent_id,
agentVersion='DRAFT',
actionGroupId= action_group_id,
actionGroupName=action_group_name,
actionGroupExecutor={
'lambda': lambda_function['FunctionArn']
},
functionSchema={
'functions': agent_functions
},
actionGroupState='DISABLED',
)
bedrock_agent_client.disassociate_agent_knowledge_base(
agentId=agent_id,
agentVersion='DRAFT',
knowledgeBaseId=kb_id
)
bedrock_agent_client.delete_agent_action_group(
agentId=agent_id,
agentVersion='DRAFT',
actionGroupId=action_group_id
)
bedrock_agent_client.delete_agent_alias(
agentAliasId=alias_id,
agentId=agent_id
)
bedrock_agent_client.delete_agent(agentId=agent_id)
print(f"Agent {agent_id}, Agent Alias {alias_id}, and Action Group have been deleted.")
except Exception as e:
print(f"Error deleting Agent resources: {e}")
# Delete Lambda function
try:
lambda_client.delete_function(FunctionName=lambda_function_name)
print(f"Lambda function {lambda_function_name} has been deleted.")
except Exception as e:
print(f"Error deleting Lambda function {lambda_function_name}: {e}")
# Delete DynamoDB table
try:
dynamodb_client.delete_table(TableName=table_name)
print(f"Table {table_name} is being deleted...")
waiter = dynamodb_client.get_waiter('table_not_exists')
waiter.wait(TableName=table_name)
print(f"Table {table_name} has been deleted.")
except Exception as e:
print(f"Error deleting table {table_name}: {e}") |