Miserlou / Zappa

Serverless Python
https://blog.zappa.io/
MIT License
11.89k stars 1.2k forks source link

Incorrect IAM permissions for DynamoDB #1662

Open tk421 opened 6 years ago

tk421 commented 6 years ago

Dynamo DB Incorrect permissions

When deploying a zappa application based in this post, with the following zappa_settings.json

{
    "dev": {
        "app_function": "blog.app", 
        "aws_region": "ap-southeast-2", 
        "profile_name": "default", 
        "project_name": "serverless-blog", 
        "runtime": "python2.7", 
        "s3_bucket": "taromba-sb"
    }
}

and make zappa deploy, it starts the deployment but eventually fails with the following error:

Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 502 response code.

Zappa creates a IAM policy called _zappapermissions that contains the following code for DynamoDB

        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": "arn:aws:dynamodb:*:*:*"
        },

And those permissions does not allow to execute the action ListTables which is needed in the deployment process.

Python 2.7

Expected Behavior

After running zappa deploy, the deployment should be successful.

Actual Behavior

% zappa update
(python-slugify 1.2.6 (/home/tk421/code/serverless.blog/env/lib/python2.7/site-packages), Requirement.parse('python-slugify==1.2.4'), set([u'zappa']))
Calling update for stage dev..
Downloading and installing dependencies..
Packaging project as zip.
Uploading serverless-blog-dev-1539819658.zip (8.6MiB)..
100%|| 9.02M/9.02M [01:16<00:00, 117KB/s]
Updating Lambda function code..
Updating Lambda function configuration..
Uploading serverless-blog-dev-template-1539819740.json (1.6KiB)..
100%|█| 1.66K/1.66K [00:00<00:00, 14.1KB/s]
Deploying API Gateway..
Scheduling..
Unscheduled serverless-blog-dev-zappa-keep-warm-handler.keep_warm_callback.
Scheduled serverless-blog-dev-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 502 response code.

zappa tail

[1539819427320] An error occurred (AccessDeniedException) when calling the ListTables operation: User: arn:aws:sts::808777168163:assumed-role/serverless-blog-dev-ZappaLambdaExecutionRole/serverless-blog-dev is not authorized to perform: dynamodb:ListTables on resource: *: ClientError
Traceback (most recent call last):
  File "/var/task/handler.py", line 580, in lambda_handler
  return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 245, in lambda_handler
  handler = cls()
  File "/var/task/handler.py", line 139, in __init__
  self.app_module = importlib.import_module(self.settings.APP_MODULE)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
  __import__(name)
  File "/var/task/blog.py", line 12, in <module>
  dyn_storage = DynamoDBStorage(region_name='us-east-1')
  File "/var/task/flask_blogging/dynamodbstorage.py", line 22, in __init__
  self._create_all_tables()
  File "/var/task/flask_blogging/dynamodbstorage.py", line 195, in _create_all_tables
  response = self._client.list_tables()
  File "/var/runtime/botocore/client.py", line 314, in _api_call
  return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 612, in _make_api_call
  raise error_class(parsed_response, operation_name)
ClientError: An error occurred (AccessDeniedException) when calling the ListTables operation: User: arn:aws:sts::808777168163:assumed-role/serverless-blog-dev-ZappaLambdaExecutionRole/serverless-blog-dev is not authorized to perform: dynamodb:ListTables on resource: *

Possible Fix

Make sure that zappa-permissions creates the correct values. More broader permissions works, but this gets override by zappa all the time - it would be best to tailor those permissions to what is actually needed.

        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": "*"
        },

Steps to Reproduce

  1. Configure AWS CLI and confirm that you can interact with AWS
  2. Zappa version 0.47.0
  3. git clone https://bitbucket.org/manageacloud/serverless-test.git
  4. virtualenv env (tested with python 2.7)
  5. source env/bin/activate
  6. pip install -r requirements.txt
  7. zappa deploy dev

Your Environment

thesunlover commented 6 years ago

did you run aws configure?

tk421 commented 6 years ago

did you run aws configure?

@thesunlover yes. I can interact with aws over the cli without any problem.

progerjkd commented 5 years ago

I am having the same problem. Did you fixed it?

tk421 commented 5 years ago

@progerjkd I found a workaround.

File zappa_settings.json

    "dev": {
        "app_function": "blog.app", 
        "aws_region": "ap-southeast-2", 
        "profile_name": "default", 
        "project_name": "serverless-blog", 
        "runtime": "python2.7", 
        "s3_bucket": "taromba-sb",
        "manage_roles": false,
        "role_name": "MyLambdaRole", 
        "role_arn": "arn:aws:iam::800000000:role/my-role-name-dev-ZappaLambdaExecutionRole"
    }
}

You just need to create the role in AWS IAM to get the the correct value for role_arn . Those are the permissions that I am currently using for testing purposes.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "ec2:CreateNetworkInterface",
                "ec2:DescribeInstances",
                "ec2:DetachNetworkInterface",
                "xray:PutTelemetryRecords",
                "ec2:DescribeNetworkInterfaces",
                "lambda:InvokeFunction",
                "ec2:ResetNetworkInterfaceAttribute",
                "ec2:ModifyNetworkInterfaceAttribute",
                "ec2:DeleteNetworkInterface",
                "route53:*",
                "ec2:AttachNetworkInterface",
                "xray:PutTraceSegments"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "logs:*",
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::*"
        },
        {
            "Sid": "VisualEditor3",
            "Effect": "Allow",
            "Action": "kinesis:*",
            "Resource": "arn:aws:kinesis:*:*:*"
        },
        {
            "Sid": "VisualEditor4",
            "Effect": "Allow",
            "Action": "sns:*",
            "Resource": "arn:aws:sns:*:*:*"
        },
        {
            "Sid": "VisualEditor5",
            "Effect": "Allow",
            "Action": "sqs:*",
            "Resource": "arn:aws:sqs:*:*:*"
        },
        {
            "Sid": "VisualEditor6",
            "Effect": "Allow",
            "Action": "dynamodb:*",
            "Resource": "*"
        }
    ]
}