- Newest
- Most votes
- Most comments
I see a few issues with your Lambda function and S3 bucket policy configuration that are causing the errors.
First, let's address the Lambda function code:
- You're incorrectly referencing the S3 bucket name. In your code, you have:
s3.Bucket(os.environ['***lab-bucket-jml-wednesday1***']).put_object(Key=s3_path, Body=encoded_string)
This is trying to access an environment variable with the literal name 'lab-bucket-jml-wednesday1', which doesn't exist. Instead, you should be using:
s3.Bucket('lab-bucket-jml-wednesday1').put_object(Key=s3_path, Body=encoded_string)
Or set an environment variable named S3_BUCKET_NAME with the value 'lab-bucket-jml-wednesday1' and use:
s3.Bucket(os.environ['S3_BUCKET_NAME']).put_object(Key=s3_path, Body=encoded_string)
For the S3 bucket policy:
-
Your policy is a "Deny" policy with a condition. This means it will deny all PutObject requests EXCEPT those from the specified IAM roles. This is correct, but make sure the role ARN for your Lambda function is correctly specified.
-
Ensure that the Lambda execution role (DIYPermission) has the necessary IAM permissions to perform s3:PutObject actions on your bucket. You need to attach a policy to the Lambda execution role that allows:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::lab-bucket-jml-wednesday1/*"
]
}
]
}
The error message indicates that the Lambda function doesn't have permission to perform the s3:PutObject action, which suggests either:
- The Lambda execution role doesn't have the necessary IAM permissions, or
- The bucket policy is incorrectly configured and is denying the request.
Double-check both configurations to ensure your Lambda function has the proper permissions to write to the S3 bucket.
Sources
Lambda Access to S3 When Public Access Is Blocked? | AWS re:Post
Securing Amazon S3 presigned URLs for serverless applications | AWS Compute Blog
Unable to perform upload to S3 bucket | AWS re:Post
Relevant content
asked 3 years ago
asked 3 years ago

Thanks Prakruthi R:
I stepped through the practice lab again. It directed me to creating a function, which I didn't do before, which had code for use of DynamoDB. I ultimately did have text file loaded as an object to S3, which was the point of the lab. However, the validation process of the DIY lab still had the prior error in my posting. The lab console worked but the validation didn't reflect that.
I attempted to alter functions that should've been left alone rather than pasting code into a created function. As I said, the instructions weren't very clear.
Thanks for you feedback.