Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting S3 bucket access to an AWS Lambda function

I have a file in an S3 bucket for which I would like to restrict access, so that it can only be accessed from within a specific Lambda function. I tried writing a Bucket policy (subbing in my info for region, account, etc.) to accomplish this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1457474835965",
      "Action": "s3:*",
      "Principal": "*",
      "Effect": "Deny",
      "Resource": "arn:aws:s3:::my-bucket/file.txt",
      "Condition": {
        "ArnNotEquals": {
          "aws:SourceArn": "arn:aws:lambda:region:account:function:FunctionName"
        }
      }
    }
  ]
}

However access to the file was still denied to the Lambda function when it was invoked. How can I accomplish what I am trying to do?

like image 635
nnnm Avatar asked Mar 25 '26 22:03

nnnm


1 Answers

Your lambda function will be running with a specific role. Create a policy that grants access to the s3 resource and add it to the role.

Example:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:*",
      "Resource": "arn:aws:s3:::my-bucket/file.txt"
    }
  ]
}
like image 87
ataylor Avatar answered Mar 27 '26 22:03

ataylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!