I want to create an aws lambda code, which provides a public API for only read from an aws rds db instance. When I want to create a lambda function, it asks me about permission roles. Because I’m afraid, I want to give a very strict permission to the code to allow only the reading from the db instance.
I have found this site, it lists a few managed policies. I could find this inside that:
"AmazonRDSReadOnlyAccess": {
"Arn": "arn:aws:iam::aws:policy/AmazonRDSReadOnlyAccess",
"AttachmentCount": 0,
"CreateDate": "2015-02-06T18:40:53+00:00",
"DefaultVersionId": "v1",
"Document": {
"Statement": [
{
"Action": [
"rds:Describe*",
"rds:ListTagsForResource",
"ec2:DescribeAccountAttributes",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeSecurityGroups",
"ec2:DescribeVpcs"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"cloudwatch:GetMetricStatistics"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
},
"IsAttachable": true,
"IsDefaultVersion": true,
"Path": "/",
"PolicyId": "ANPAJKTTTYV2IIHKLZ346",
"PolicyName": "AmazonRDSReadOnlyAccess",
"UpdateDate": "2015-02-06T18:40:53+00:00",
"VersionId": "v1"
},
I can see the default policy document, when I want to create a new custom role. And I can see that is basically contains {“Statement”, “Version” and “Resource”}:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
And this perfectly fits to the AmazonRDSReadOnlyAccess’s “Document” block, so I think that needs to be copy-pasted to there to achieve the rds read-only permission. So what I need to put into the custom role’s policy document is:
{
"Statement": [
{
"Action": [
"rds:Describe*",
"rds:ListTagsForResource",
"ec2:DescribeAccountAttributes",
"ec2:DescribeAvailabilityZones",
"ec2:DescribeSecurityGroups",
"ec2:DescribeVpcs"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": [
"cloudwatch:GetMetricStatistics"
],
"Effect": "Allow",
"Resource": "*"
}
],
"Version": "2012-10-17"
}
This is the thing I need to do? Am I right?
Does it allow me for the lambda function to read only from a certain RDS db instance? Is there a more simple way to do this?
Because I saw the policy templates in the “create new role from template”, and I couldn’t find anything for this goal.
Solution:
The IAM policies, like the one you shown above, are grant/deny access for management of the RDS service only. That does not grant you authorization to access data. You can consider the following security approach for securing the DB again unauthorized access.
-
Secure the Lambda Execution Role – give the lambda service least privleged role for accessing the RDS management service.
-
Secure the RDS login user – Create a user dedicated for this function, and grant it the least privedge required to access the DB and perform the needed functions
-
Secure the Lambda via API. You can use the AWS API Gateway to expose the Lambda function. This API can further secured against unauthorized access. This is optional.