Skip to content

Cannot upload via S3 API in production mode

0

Firstly. i created an IAM user with permission AmazonS3FullAccess, then created a key with use case Local Code. After that, i copied all access key and secret key to my net core api

// Program.cs
Environment.SetEnvironmentVariable("AWS_ACCESS_KEY_ID", configuration["AWS:AccessKey"]);
Environment.SetEnvironmentVariable("AWS_SECRET_ACCESS_KEY", configuration["AWS:SecretKey"]);
Environment.SetEnvironmentVariable("AWS_REGION", configuration["AWS:Region"]);
services.AddDefaultAWSOptions(configuration.GetAWSOptions());

// appsettings.cs
"AWS": {
  "AccessKey": "ACCESS_KEY",
  "SecretKey": "SECRET_KEY",
  "Region": "ap-southeast-2"
},

I ran server, tested api upload image via S3 API

var request = new PutObjectRequest {
    BucketName = EnvironmentConstant.BUCKET_S3,
    Key = userInfo.Avatar,
    InputStream = file.OpenReadStream(),
};
request.Metadata.Add(StaticConstant.CONTENT_TYPE, file.Con

Everything is ok, i saw that my image was uploaded sucessfully in S3 bucket Then, i created an AWS Lambda Function, an AWS RSD Postgre, an API Gateway to lambda function with HTTPAPI, maybe all as defaults but i can't remember. In my project folder, i hit 2 commands

dotnet tool install -g Amazon.Lambda.Tools
dotnet lambda package bin/back.zip -c Release

It would create a zip file for me, After that, i uploaded this file to lambda function, waited for server deploying and tested some apis GET to fetch items in database, some POSTs, PUTs, everything worked perfectly. But then when i tested api upload image, the server returned to me an error 500 without messages, so i created a GlobalExceptionHandler, i tested it ok at localhost, but when deployed again, this handler never fired, i couldn't see anything return in response body. Then, i add some packages to log messages to CloudWatch. but it never showed message again. Now i really don't know what happened, what i have to do, or what error. Please help me out.

Thanks in advance!

2 Answers
1

Hi Aka,

To confirm, your code to PUT into S3 works locally but not when the code is uploaded to Lambda? But other lambda functions work as long as they're not uploading to S3? And confirming that the credentials you used are only used when running locally?

It looks like Lambda doesn't have the required permissions. Lambda would need to have an associated role to access S3, this is separate from the IAM user you created.

The detailed instructions on how to create the role and associate it with your function is below: https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html

AWS

answered 2 years ago

AWS
EXPERT

reviewed 2 years ago

  • Hello Lance. I followed the docs and created a role with these permission: AmazonS3FullAccess, AmazonS3ObjectLambdaExecutionRolePolicy, AWSLambdaVPCAccessExecutionRole. Then i updated lambda's execution role to this role. I called the api upload image again but seems like it didn't work, same error 500 as before.

0

Hi,

You may want to follow this guidance about 500s in Lambda to troubleshoot your issue: https://repost.aws/knowledge-center/lambda-troubleshoot-invoke-error-502-500

Best,

Didier

EXPERT

answered 2 years ago

  • Hello, i don't know what is the problem in my case, but i think it related to this problem: KMSAccessDeniedException But in my server, i'm using Access Key and Secret Key created from IAM user. And i cannot access the S3 due to unknown error with this Access Key

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.