|
| 1 | +import boto3 |
| 2 | +import json |
| 3 | + |
| 4 | +from logger import logger |
| 5 | +from vars import AWS_ACCOUNT_ID, BEDROCK_KB_IAM_POLICY |
| 6 | + |
| 7 | +iam = boto3.client("iam") |
| 8 | + |
| 9 | +# Define the policy document |
| 10 | +policy_document = { |
| 11 | + "Version": "2012-10-17", |
| 12 | + "Statement": [ |
| 13 | + { |
| 14 | + "Sid": "RedshiftDataAPIStatementPermissions", |
| 15 | + "Effect": "Allow", |
| 16 | + "Action": [ |
| 17 | + "redshift-data:GetStatementResult", |
| 18 | + "redshift-data:DescribeStatement", |
| 19 | + "redshift-data:CancelStatement" |
| 20 | + ], |
| 21 | + "Resource": ["*"], |
| 22 | + "Condition": { |
| 23 | + "StringEquals": { |
| 24 | + "redshift-data:statement-owner-iam-userid": "${aws:userid}" |
| 25 | + } |
| 26 | + } |
| 27 | + }, |
| 28 | + { |
| 29 | + "Sid": "RedshiftDataAPIExecutePermissions", |
| 30 | + "Effect": "Allow", |
| 31 | + "Action": [ |
| 32 | + "redshift-data:ExecuteStatement" |
| 33 | + ], |
| 34 | + "Resource": [ |
| 35 | + f"arn:aws:redshift-serverless:us-east-1:{AWS_ACCOUNT_ID}:workgroup/*" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "Sid": "RedshiftServerlessGetCredentials", |
| 40 | + "Effect": "Allow", |
| 41 | + "Action": "redshift-serverless:GetCredentials", |
| 42 | + "Resource": [ |
| 43 | + f"arn:aws:redshift-serverless:us-east-1:{AWS_ACCOUNT_ID}:workgroup/*" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "Sid": "SqlWorkbenchAccess", |
| 48 | + "Effect": "Allow", |
| 49 | + "Action": [ |
| 50 | + "sqlworkbench:GetSqlRecommendations", |
| 51 | + "sqlworkbench:PutSqlGenerationContext", |
| 52 | + "sqlworkbench:GetSqlGenerationContext", |
| 53 | + "sqlworkbench:DeleteSqlGenerationContext" |
| 54 | + ], |
| 55 | + "Resource": "*" |
| 56 | + }, |
| 57 | + { |
| 58 | + "Sid": "KbAccess", |
| 59 | + "Effect": "Allow", |
| 60 | + "Action": [ |
| 61 | + "bedrock:GenerateQuery" |
| 62 | + ], |
| 63 | + "Resource": "*" |
| 64 | + } |
| 65 | + ] |
| 66 | +} |
| 67 | + |
| 68 | +try: |
| 69 | + # Create the policy |
| 70 | + response = iam.create_policy( |
| 71 | + PolicyName=BEDROCK_KB_IAM_POLICY, |
| 72 | + PolicyDocument=json.dumps(policy_document), |
| 73 | + Description="Permissions for Bedrock Structured KB to query Redshift Serverless." |
| 74 | + ) |
| 75 | + |
| 76 | + logger.info(f"Successfully created policy!") |
| 77 | + |
| 78 | +except iam.exceptions.EntityAlreadyExistsException: |
| 79 | + logger.info(f"Policy already exists.") |
| 80 | +except Exception as e: |
| 81 | + logger.error(f"An error occurred: {e}") |
0 commit comments