Recently I started working on securing access to my S3 bucket and I have two sources which I want to grant access to and deny access to any different source.
In this case, the soruces to get the access are, my local IP or VPC IP range for example and Lambda function.
I created the following S3 bucket Policy:
{
"Version": "2019-10-10",
"Statement": [
{
"Effect": "Deny",
"NotPrincipal": {
"AWS": [
"arn:aws:iam::480311425080:role/<lambda role name>",
"arn:aws:sts::480311425080:assumed-role/<lambda role name>/<Lambda function name>"
],
"Service": "lambda.amazonaws.com"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::<bucket name>/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"<ip adress>/32",
"<ip adress>/32"
]
}
}
}
]
}
So as you see, I'm using NotPrincipal
to exclude my corresponding role and Lambda from being denied and I use NotIpaddress
to exclude my valid IPs from being denied.
In this case I still can connect to S3 from my Lambda function, but also still to connect to it from "supposidly none authorized IPs". So the condition
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"<ip adress>/32",
"<ip adress>/32"
]
}
}
does not work as expected.
Maybe you would tell to use only the role ARN for NotPrincipal
but it does not work neither.
Putting the principal as the role only "without specifying the arn with lambda function"
"NotPrincipal": {
"AWS": "arn:aws:iam::880719415082:role/lambda_s3_access"
},
apply the condition of IP filtering but makes it not possible for Lambda to connect.
Any idea?
Kind regards,
Rshad