I’m a beginner and I keep getting this error message when testing a Lambda function. I’m trying to do a simple write file to an S3 directory bucket.
Here is the PHP code:
import json
import boto3
import os
def lambda_handler(event, context):
s3 = boto3.client('s3')
bucket_name = 'pollytts--use1-az6--x-s3'
object_key = 'test-file.txt'
file_content = 'Hello, this is a test file.'
try:
response = s3.put_object(
Bucket=bucket_name,
Key=object_key,
Body=file_content
)
return {
'statusCode': 200,
'body': json.dumps('File uploaded successfully!')
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps(f'Error uploading file to S3: {str(e)}')
}
Here is the error:
Response
{
"statusCode": 500,
"body": ""Error uploading file to S3: An error occurred (AccessDenied) when calling the CreateSession operation: Access Denied""
}
Function Logs
START RequestId: 29a9828b-32ce-40ef-a247-2b7fcdfd5f45 Version: $LATEST
END RequestId: 29a9828b-32ce-40ef-a247-2b7fcdfd5f45
REPORT RequestId: 29a9828b-32ce-40ef-a247-2b7fcdfd5f45 Duration: 610.16 ms Billed Duration: 611 ms Memory Size: 128 MB Max Memory Used: 83 MB
Request ID
29a9828b-32ce-40ef-a247-2b7fcdfd5f45
I added a user and added permission policy with all access to S3 actions, and the execution role of the lambda function also has the permissions.
New contributor
Talib Kareem is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.