- Newest
- Most votes
- Most comments
The error message says that your access key isn't accepted. I don't know why that might be, but I can say that your Lambda function shouldn't have such keys at all. Instead, you should add the necessary permissions to the Lambda execution role used by your function and simply create the S3 client in your code without specifying credentials. That will cause S3 to be accessed with the temporary credentials of the IAM role used as the Lambda execution role.
If I understood wrong and this code isn't running in Lambda but in ECS or an EC2 instance, the same principle still applies: the ECS task execution role or the IAM role attached to the EC2 instance profile used by the EC2 instance would be granted the necessary permissions, and the custom code running on the platform would not specify credentials for accessing S3 or other AWS services.
The AWS SDK will automatically use the temporary credentials available from the platform when keys aren't explicitly defined, whether the platform is a Lambda function, ECS task, or EC2 instance.
The lines you quoted only set the variables in your code. Those lines don't matter. You should remove the parameters you're giving to S3Client that specify those variables. That is causing the static username/password combination that AWS_SECRET_KEY and AWS_ACCESS_KEY_ID effectively are to be used to authenticate to AWS to access the S3 bucket. When you don't specify any credentials, the AWS SDK that you're using will transparently and automatically obtain temporary credentials from the platform that you're running on, such as Lambda.
However, before you delete the credentials from your code, you will need to grant the permissions to S3 to the IAM role that your Lambda function is using as its execution role. You can see the role identifier in the configuration of your Lambda function. You can either add the permissions to the policies attached to the IAM role, or you can grant the permissions in the bucket policy of your S3 bucket.
If you aren't familiar with the terms I'm using, perhaps there's someone else you're working with who has set up the current Lambda execution role and the S3 bucket who would be able to configure what I'm explaining?
Relevant content
- asked 3 years ago

Can you please explain how this works? I'm new to this. Should I delete these variables:
AWS_SECRET_KEY = process.env.AWS_SECRET_KEY AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID
and will S3 v3 automatically connect?"