I get an "Access Denied" error when I use an AWS Lambda function to upload files to an Amazon Simple Storage Service (Amazon S3) bucket. The Amazon S3 bucket is in another AWS account.
Short description
If the permissions between a Lambda function and an Amazon S3 bucket are incomplete or incorrect, then Lambda returns an "Access Denied" error.
The following solution requires a Lambda function in account A and an S3 bucket in account B.
To set up permissions between a Lambda function in account A and an S3 bucket in another account account B, complete the following steps:
- Create a Lambda execution role in account A that allows the Lambda function to upload objects to Amazon S3.
- Update the S3 bucket policy in account B to allow the Lambda function to upload objects to the bucket.
Resolution
Create a Lambda execution role in account A that allows the Lambda function to upload objects to Amazon S3
Example Python version 3.12 code for a Lambda function that uploads files to an S3 bucket
import json
import boto3
s3 = boto3.client('s3')
def lambda_handler(event, context):
bucket = 'EXAMPLE-DESTINATION-BUCKET'
transactionToUpload = {}
transactionToUpload['transactionId'] = '12345'
transactionToUpload['type'] = 'PURCHASE'
transactionToUpload['amount'] = 20
transactionToUpload['customerId'] = 'CID-1111'
filename = 'CID-1111' + '.json'
uploadByteStream = bytes(json.dumps(transactionToUpload).encode('UTF-8'))
s3.put_object(Bucket=bucket, Key=filename, Body=uploadByteStream)
print("Put Complete")
Complete the following steps:
-
Create an AWS Identity and Access Management (IAM) execution role in account A for your Lambda function:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
-
Attach a policy to the IAM role that grants permission to upload objects to the bucket in Account B.
Example IAM policy that grants an IAM role s3:PutObject permission
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::AccountBBucketName/*"
}
]
}
Note: Replace AccountBBucketName with the S3 bucket name for account B.
-
Change your Lambda function execution role to the IAM role that you created in the preceding step. For more information, see Configuring Lambda functions.
Modify the S3 bucket's policy in account B to allow the Lambda function to upload objects to the bucket
Update the bucket policy so that it specifies the Lambda execution role ARN as a Principal that has access to the action s3:PutObject.
Example S3 bucket policy that allows a Lambda function to upload objects to the bucket
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::AccountA:role/AccountARole"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::AccountBBucketName/*"
}
]
}
Note: Replace AccountBBucketName with the S3 bucket name for account B.
Check the AWS KMS encryption configuration
If an IAM entity has full permissions to an object but still receives an "Access Denied" error, then check your configuration requirements. Check whether the object has AWS Key Management Service (AWS KMS) encryption (SSE-KMS). Use the Amazon S3 console to view the object's properties and check the object's SSE-KMS information.
If the destination bucket has default encryption with a customer managed key, then check your configuration requirements. The KMS key policy must allow your Lambda execution role to perform the kms:GenerateDataKey or kms:Decrypt action. For more information, see Allows access to the AWS account and activates IAM policies.
If the IAM entity belongs to a different account than the AWS KMS key, then edit the IAM policy to grant kms:GenerateDataKey permission. For example, to upload the SSE-KMS objects, you must specify kms:GenerateDataKey permission in both the key policy and the IAM policy. For more information about cross-account access between the IAM user and KMS key, see Allowing users in other accounts to use a KMS key.
Related information
How do I troubleshoot 403 Access Denied errors from Amazon S3?
How do I allow my Lambda function access to my Amazon S3 bucket?
Why am I getting an Access Denied error message when I upload files to my Amazon S3 bucket that has AWS KMS default encryption?
How do I troubleshoot Access Denied errors when IAM users from another AWS account try to access my Amazon S3 bucket?