如果您使用的是AWS Glue Crawler,可能会遇到以下错误:
"User does not have permission to call IAM:GetRole" 或 "AccessDeniedException"
这是由于AWS Glue Crawler默认角色缺少必要的IAM权限。 这种问题可以通过添加具有必要权限的自定义角色来解决。
以下是一些示例代码,可以帮助您创建具有必要权限的自定义角色:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:Get*", "s3:List*", "s3:PutObject" ], "Resource": [ "*" ] } ] }
import boto3 from botocore.exceptions import ClientError
GLUE_ROLE_NAME = 'glue_custom_role'
glue = boto3.client('glue') iam = boto3.client('iam')
try: # create IAM role for Glue glue_role = iam.create_role( Path='/', RoleName=GLUE_ROLE_NAME, AssumeRolePolicyDocument='''{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "glue.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }''' )
# attach S3 policy to Glue role
policy_arn = 'arn:aws:iam::aws:policy/AmazonS3FullAccess'
iam.attach_role_policy(
RoleName=GLUE_ROLE_NAME,
PolicyArn=policy_arn
)
print("Role created: ", glue_role['Role']['Arn'])
except ClientError as e: if e.response['Error']['Code'] == 'Entity