代码之家  ›  专栏  ›  技术社区  ›  Amir

使用Lambda函数运行AWS Athenas查询

  •  0
  • Amir  · 技术社区  · 6 年前

    select * from mytestdb.test
    

    该表有三列, customer_Id, product_Id, price

    我尝试创建一个lambda函数,该函数使用boto3为我运行相同的查询:

    import time
    import boto3
    
    DATABASE = 'mytestdb'
    TABLE = 'test'
    
    output='s3://mybucketons3/'
    
    COLUMN = 'Customer_Id'
    
    def lambda_handler(event, context):
    
        keyword = 'xyz12345'
    
        query = "SELECT * FROM %s.%s where %s = '%s';" % (DATABASE, TABLE, COLUMN, keyword)
    
        client = boto3.client('athena')
    
        # Execution
        response = client.start_query_execution(
            QueryString=query,
            QueryExecutionContext={
                'Database': DATABASE
            },
            ResultConfiguration={
                'OutputLocation': output,
            }
        )
    
    
        return
    

    但是,我得到了以下错误:

    Response:
    {
      "errorMessage": "An error occurred (AccessDeniedException) when calling the StartQueryExecution operation: User: arn:aws:sts::076088932150:assumed-role/Test/QueryTest is not authorized to perform: athena:StartQueryExecution on resource: arn:aws:athena:us-west-2:076088932150:workgroup/primary",
      "errorType": "ClientError",
    

    这似乎是一种访问问题,但我不知道为什么,因为我有两个lambda和雅典娜数据库与同一帐户。

    1 回复  |  直到 6 年前
        1
  •  4
  •   A.Khan    6 年前

    正如我在评论中提到的,您的Lambda角色应该包含允许策略与Athena服务交互。我还为您的S3存储桶添加了完全权限。例子:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Stmt1547414166585",
          "Action": [
            "athena:StartQueryExecution"
          ],
          "Effect": "Allow",
          "Resource": "*"
        },
        {
          "Sid": "Stmt1547414166586",
          "Action": [
            "s3:*"
          ],
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::your-bucket-name/*"
        } 
      ]
    }
    
        2
  •  0
  •   Sam Osborne    5 年前

    A.Khan的想法对我有用。

    使用AWS控制台编辑Lambda的IAM角色,使其具有AmazonAthenaFullAccess和AmazonS3FullAccess策略。

    AWS Policies

        3
  •  0
  •   Akansha Singh    3 年前

    为服务提供/添加完全访问权限不是最佳做法。您可以尝试将访问权限限制为仅lambda需要执行的操作。 尝试使用特定权限重新部署IAM角色,并在成功部署后将其重新附加到lambda函数。 你的lambda肯定会有用的。如果在添加所需权限后仍然拒绝访问,则从您的帐户中提交aws支持票证。