New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
S3 PutObjectAsync fails when I published to AWS lambda through Visual Studio 2019 (.net core 3.1). But is working with Mock Lambda Test tool. #1681
Comments
Hi @rahul-JFL, Thanks for posting the guidance question. Please share logs and stack trace (if any) from CloudWatch. Also, it would be helpful if you could add logging statements (using ILambdaContext.Logger) to the above code to trace the execution. The problem if see in the above code is that await is used to execute Task.Run() which executes loop in the background thread and then use S3Client.PutObjectAsync() which is not awaited. It is quite possible that the background thread for S3Client.PutObjectAsync() are paused (since these are not awaited) when Lambda function returns. Lambda Test Tool is just a simulation of code execution, it is different from the actual Lambda execution environment. Thanks, |
This comment has been minimized.
This comment has been minimized.
Hi @rahul-JFL, GitHub is the effective way to communicate about issues. Please post the logs (after adding logging statements), code snippet (if different from above) and stack trace for any failures here on GitHub. Also try using await keyword before S3Client.PutObjectAsync(request) call like below and see if it helps: await S3Client.PutObjectAsync(request); Thanks, |
Thanks Ashis,
|
Hi @rahul-JFL, Thanks for your inputs. I made some minor changes to your code (notice the proper use of await keyword specifically) and was able to successfully upload file to S3 inside AWS Lambda: public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
string returnMessage = await UploadFileToS3Bucket(fileContent, fileName);
return returnMessage;
}
public async Task<string> UploadFileToS3Bucket(String input, String fileName)
{
using (AmazonS3Client s3Client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, awsSessionToken, region))
{
LambdaLogger.Log("RegionEndPoint:" + region);
LambdaLogger.Log("bucketName:" + bucketName);
LambdaLogger.Log("fileName:" + fileName);
LambdaLogger.Log("input:" + input);
return await Task.Run(async () =>
{
for (int i = 0; i < 6; i++)
{
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = fileName,
ContentBody = input,
ContentType = "text/csv",
StorageClass = S3StorageClass.Standard,
CannedACL = S3CannedACL.NoACL
};
await s3Client.PutObjectAsync(request);
LambdaLogger.Log("PutObjectAsync Request:" + request);
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}
LambdaLogger.Log("Put Object Completed");
return "Success";
}).ConfigureAwait(false);
}
} Logs:
I also had to add appropriate role to AWS Lambda so that it's able to upload file to S3. Since you are also executing set 1 and 3 before uploading the file, you may also try increasing timeout (refer article Configuring functions in the AWS Lambda console) if those steps are causing any delay. Please let me know if this helps. Thanks, |
Thanks a lot! Ashish.
Is ‘awsSessionToken’ is a mandatory parameter in function UploadFileToS3Bucket? Because without this, issue is same.
If yes, please help me to generate ‘SessionToken’ code in my Lambda Function.
For your information, I am not using ‘Amazon API Gateway’ in my case. My lambda function is getting triggered through ‘Kinesis Firehose’.
Regards,
Rahul Gupta
From: Ashish Dhingra [mailto:notifications@github.com]
Sent: Wednesday, August 19, 2020 11:29 PM
To: aws/aws-sdk-net
Cc: Rahul Gupta; Mention
Subject: Re: [aws/aws-sdk-net] S3 PutObjectAsync fails when I published to AWS lambda through Visual Studio 2019 (.net core 3.1). But is working with Mock Lambda Test tool. (#1681)
Hi @rahul-JFL<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frahul-JFL&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7854e564dc6e421a53e308d8446982a5%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637334567243490865&sdata=W8Q2OcqS3wQxKBfmNoiIDURgCK70aagfAODjqrX1fOw%3D&reserved=0>,
Thanks for your inputs. I made some minor changes to your code (notice the proper use of await keyword specifically) and was able to successfully upload file to S3 inside AWS Lambda:
public async Task<string> FunctionHandler(string input, ILambdaContext context)
{
string returnMessage = await UploadFileToS3Bucket(fileContent, fileName);
return returnMessage;
}
public async Task<string> UploadFileToS3Bucket(String input, String fileName)
{
using (AmazonS3Client s3Client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, awsSessionToken, region))
{
LambdaLogger.Log("RegionEndPoint:" + region);
LambdaLogger.Log("bucketName:" + bucketName);
LambdaLogger.Log("fileName:" + fileName);
LambdaLogger.Log("input:" + input);
return await Task.Run(async () =>
{
for (int i = 0; i < 6; i++)
{
PutObjectRequest request = new PutObjectRequest
{
BucketName = bucketName,
Key = fileName,
ContentBody = input,
ContentType = "text/csv",
StorageClass = S3StorageClass.Standard,
CannedACL = S3CannedACL.NoACL
};
await s3Client.PutObjectAsync(request);
LambdaLogger.Log("PutObjectAsync Request:" + request);
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}
LambdaLogger.Log("Put Object Completed");
return "Success";
}).ConfigureAwait(false);
}
}
Logs:
START RequestId: 64616574-6b98-48a3-875c-7e0a145be654 Version: $LATEST
RegionEndPoint:Asia Pacific (Mumbai) (ap-south-1)bucketName:tests3bucket-1681fileName:DPI66639_20200819120927.csvinput:1021,20200819,1000,D834,000000000010000492,C009,0.1,KGS,PutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPutObjectAsync Request:Amazon.S3.Model.PutObjectRequestPut Object CompletedEND RequestId: 64616574-6b98-48a3-875c-7e0a145be654
REPORT RequestId: 64616574-6b98-48a3-875c-7e0a145be654 Duration: 10242.09 ms Billed Duration: 10300 ms Memory Size: 256 MB Max Memory Used: 104 MB
I also had to add appropriate role to AWS Lambda so that it's able to upload file to S3.
Please let me know if this helps.
Thanks,
Ashish
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faws%2Faws-sdk-net%2Fissues%2F1681%23issuecomment-676574436&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7854e564dc6e421a53e308d8446982a5%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637334567243501222&sdata=H2sH7UBtqI7E%2FK9ogaas0ORFWbfKuuES2FiIpZyB2kk%3D&reserved=0>, or unsubscribe<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FALECZL5FV65TFCO7SHL75MTSBQHFBANCNFSM4QBRT3QQ&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7854e564dc6e421a53e308d8446982a5%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637334567243501222&sdata=7KKssfrNZ5xq0Dt%2BwzCuSy7uU0D21sfEQGNwkvtZeTo%3D&reserved=0>.
DISCLAIMER This email and any files transmitted with it are confidential and are solely for the use of the individual or entity to which it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you receive this transmission in error, please notify the sender by reply email and then destroy the message. Opinions, conclusions and other information in this message that do not relate to official business of the company shall be understood to be neither given nor endorsed by JUBILANT FOODWORKS LTD. Any information contained in this email, when addressed to Clients is subject to the terms and conditions in governing client contract
|
@rahul-JFL, |
Yes, I review and checked my permission and policy on my bucket and lambda functions have full rights.
Still I am receiving time-out exception
{"errorMessage":"2020-08-20T15:48:30.712Z 85291954-22b9-45fc-8099-e1bdd2616421 Task timed out after 60.02 seconds"}
FYI file content :”My test text”.
From: Ashish Dhingra [mailto:notifications@github.com]
Sent: Thursday, August 20, 2020 8:04 PM
To: aws/aws-sdk-net
Cc: Rahul Gupta; Mention
Subject: Re: [aws/aws-sdk-net] S3 PutObjectAsync fails when I published to AWS lambda through Visual Studio 2019 (.net core 3.1). But is working with Mock Lambda Test tool. (#1681)
@rahul-JFL<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frahul-JFL&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7f30faee1e824972484c08d8451622a9%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637335308658280444&sdata=3TRU7zG5B3ld3dVVZxjUm%2BnvrSc4hlNpma7YD6PCUv4%3D&reserved=0>,
AwsSessionToken is not a mandatory token. Please check the permissions and policies. Also modify u you our code to use the await keyword properly since any background threads would be suspended once Lambda function returns.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faws%2Faws-sdk-net%2Fissues%2F1681%23issuecomment-677703310&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7f30faee1e824972484c08d8451622a9%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637335308658290434&sdata=828%2FP9dHfvar%2BLoCga60ZSsqR1ms%2B6Ox%2BcOlsvGR2%2Fg%3D&reserved=0>, or unsubscribe<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FALECZL4SAFEYH4MFPBSDRE3SBUX6ZANCNFSM4QBRT3QQ&data=02%7C01%7Crahul.gupta%40jublfood.com%7C7f30faee1e824972484c08d8451622a9%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637335308658290434&sdata=avl5mEz0f7BgMN3n%2BkDN7%2B1JJHALBmnq%2BKEUDZ00yQs%3D&reserved=0>.
DISCLAIMER This email and any files transmitted with it are confidential and are solely for the use of the individual or entity to which it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you receive this transmission in error, please notify the sender by reply email and then destroy the message. Opinions, conclusions and other information in this message that do not relate to official business of the company shall be understood to be neither given nor endorsed by JUBILANT FOODWORKS LTD. Any information contained in this email, when addressed to Clients is subject to the terms and conditions in governing client contract
|
Hi @rahul-JFL, Could you confirm if you modified your code to properly use "await" keyword? As mentioned earlier, background threads could be suspended if the Lambda returns the invocation call. In your code, the call s3Client.PutObjectAsync(request) needs to awaited or executed synchronously. await s3Client.PutObjectAsync(request); or var putItemResponse = s3Client.PutObjectAsync(request).Result; Using the modified code snippet shared with you, I'm unable to reproduce the issue. Thanks, |
Yes, I tried both Async as well as synchronous. Please find attached my both function class and exception screen shot. @ashishdhingra : I really appreciate your effort and support but for me, this is show stopper task . Please help me. |
Hi @rahul-JFL, In your code, could you please change the following: private string bucketName = "indentbucket";
...
string returnMessage = UploadFileToS3Bucket(input, "Post/MyText.csv"); Refer to Bucket restrictions and limitations and Object key and metadata for the naming guidelines. Thanks, |
I changed my bucket and removed the folder also, I am not able to put my object in the bucket. Please see my attached class code after your suggestion and log. sync_FunctionClass.txt Also please check my bucket permission Thanks |
Hi @ashishdhingra Regards, |
Hi @rahul-JFL, Could you check if your Lambda has proper role attached to access S3. Here are the steps I used to deploy Lambda: Notice the Role Name field. You may also try increasing timeout. Function executes successfully I'm using the same code as shared earlier and it works fine (just changed the Key for the uploaded file to prefix it with "Post/", since you wanted it to upload to sub folder), It appears be the environmental issue, may be VPC, security groups, etc. Thanks, |
Thanks @ashishdhingra But now another issue, if i will not use VPC in my lambda function so I am unable to connect to RDS service(MySQL).
Can we connect RDS-MySQL without VPC? Thanks & Regards, |
Hi @rahul-JFL, Good morning. As we figured out earlier, Lambda code is correct and working, so the only thing left is to figure out the proper service level configuration within VPC. I would recommend going through service specific documentation which could provide some reference to VPC specific scenarios. Following documentation links might be helpful for your scenario: For any other support related to VPC, please directly contact service specific support to get expertise guidance related to service. Please let me know if any more guidance for .NET SDK related Lambda code is required and if this issue could be closed. Thanks, |
Thanks @ashishdhingra
Let me check and close this issue soon.
I have another question , how can I pass a csv string to my lambda function as an input parameter.
Actually, my scenario is that
Triggered (if any .csv file pushed) a lambda function then a lambda function will read a csv file from S3- bucket and data saved into RDS.
Thanks
Rahul Gupta
From: Ashish Dhingra [mailto:notifications@github.com]
Sent: Monday, August 24, 2020 10:13 PM
To: aws/aws-sdk-net
Cc: Rahul Gupta; Mention
Subject: Re: [aws/aws-sdk-net] S3 PutObjectAsync fails when I published to AWS lambda through Visual Studio 2019 (.net core 3.1). But is working with Mock Lambda Test tool. (#1681)
Hi @rahul-JFL<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frahul-JFL&data=02%7C01%7Crahul.gupta%40jublfood.com%7Cc9d2a51697c14ff0c50908d8484cc384%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637338841818057522&sdata=5DENNJ2ei8ZI2xBorew5B2IAjBVPK65DJPBBmdCDKvE%3D&reserved=0>,
Good morning.
As we figured out earlier, Lambda code is correct and working, so the only thing left is to figure out the proper service level configuration within VPC. I would recommend going through service specific documentation which could provide some reference to VPC specific scenarios. Following documentation links might be helpful for your scenario:
· Example Bucket Policies for VPC Endpoints for Amazon S3<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.aws.amazon.com%2FAmazonS3%2Flatest%2Fdev%2Fexample-bucket-policies-vpc-endpoint.html&data=02%7C01%7Crahul.gupta%40jublfood.com%7Cc9d2a51697c14ff0c50908d8484cc384%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637338841818067516&sdata=mVm1cQKfja56CoC3lRoRhF6H%2BMw1QY9PDqcvIH83F98%3D&reserved=0>
· Configuring a Lambda function to access resources in a VPC<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdocs.aws.amazon.com%2Flambda%2Flatest%2Fdg%2Fconfiguration-vpc.html&data=02%7C01%7Crahul.gupta%40jublfood.com%7Cc9d2a51697c14ff0c50908d8484cc384%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637338841818067516&sdata=koFiBnKm5t5AmS9JFcSAwGaea4khcrN0cOj3tEHg76M%3D&reserved=0>
For any other support related to VPC, please directly contact service specific support to get expertise guidance related to service.
Please let me know if any more guidance for .NET SDK related Lambda code is required and if this issue could be closed.
Thanks,
Ashish
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faws%2Faws-sdk-net%2Fissues%2F1681%23issuecomment-679240038&data=02%7C01%7Crahul.gupta%40jublfood.com%7Cc9d2a51697c14ff0c50908d8484cc384%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637338841818077514&sdata=RH%2FJImMJmtUtSi52uriJpPPoXSQdzVpa7CDoOVBL1S8%3D&reserved=0>, or unsubscribe<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FALECZL3SW72X3KHWBKMLL53SCKKBFANCNFSM4QBRT3QQ&data=02%7C01%7Crahul.gupta%40jublfood.com%7Cc9d2a51697c14ff0c50908d8484cc384%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637338841818077514&sdata=KSKe9eX5BLDQ8Fhxkgnn73XiLEErNcBpaXM8ubu0Mr4%3D&reserved=0>.
DISCLAIMER This email and any files transmitted with it are confidential and are solely for the use of the individual or entity to which it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you receive this transmission in error, please notify the sender by reply email and then destroy the message. Opinions, conclusions and other information in this message that do not relate to official business of the company shall be understood to be neither given nor endorsed by JUBILANT FOODWORKS LTD. Any information contained in this email, when addressed to Clients is subject to the terms and conditions in governing client contract
|
Thanks @ashishdhingra Let me check and close this issue soon. Actually, my scenario is that Thanks |
Hi @rahul-JFL, There is one good article Tutorial: Importing data into Amazon Pinpoint from external sources which closely resembles you scenarios. It basically involves setting up events on S3 bucket whenever a CSV file is pushed to the bucket which later triggers lambda function. Lambda function would receive event data in a specified JSON format which you can use to fetch file from S3, parse CSV content and do further processing. There are many such code samples online if you look out for it in a search engine. Kindly note that the above article is just provided as a guidance source and you should develop and fully test your code before production implementation. Let me know if this helps. Thanks, |
Hi Ashish, Lambda Function to be invoked or triggered by S3(csv file upload). My query is here now, how do i get csv file data in function, because i tried with 'String input' parameter.but it is not working. Thanks & Regards; |
Hi @rahul-JFL, I notice that the new guidance issue aws/aws-lambda-dotnet#723 was created for your question on Lambda triggered via CSV upload in S3 bucket. Hence I will close this issue now since the original issue mentioned here was resolved. We can continue discussion on CSV guidance under new issue. Thanks, |
Yes Sure, Thanks Ashish.
From: Ashish Dhingra [mailto:notifications@github.com]
Sent: Tuesday, August 25, 2020 10:00 PM
To: aws/aws-sdk-net
Cc: Rahul Gupta; Mention
Subject: Re: [aws/aws-sdk-net] S3 PutObjectAsync fails when I published to AWS lambda through Visual Studio 2019 (.net core 3.1). But is working with Mock Lambda Test tool. (#1681)
Hi @rahul-JFL<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Frahul-JFL&data=02%7C01%7Crahul.gupta%40jublfood.com%7C3a857f7a1bc3466a540708d849142d91%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637339698295653935&sdata=YPXv1kX3wTLdo%2BZ8ZwiLUqWNuXq0DMKCc%2FOxQ55IB7A%3D&reserved=0>,
I notice that the new guidance issue aws/aws-lambda-dotnet#723<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faws%2Faws-lambda-dotnet%2Fissues%2F723&data=02%7C01%7Crahul.gupta%40jublfood.com%7C3a857f7a1bc3466a540708d849142d91%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637339698295663932&sdata=WwXN0mGE0%2BYQjjH%2BhgI9umEgLI2VozNnSUkDkf%2FtrPg%3D&reserved=0> was created for your question on Lambda triggered via CSV upload in S3 bucket. Hence I will close this issue for now since the original issue is resolved and can continue on CSV guidance under new issue.
Thanks,
Ashish
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faws%2Faws-sdk-net%2Fissues%2F1681%23issuecomment-680133402&data=02%7C01%7Crahul.gupta%40jublfood.com%7C3a857f7a1bc3466a540708d849142d91%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637339698295663932&sdata=3uHAEvF%2FALfJaekMptKWMCml66Y6OTqdwwPvrykklJc%3D&reserved=0>, or unsubscribe<https://ind01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FALECZL5YQF7Y6E5ZN24275TSCPRKFANCNFSM4QBRT3QQ&data=02%7C01%7Crahul.gupta%40jublfood.com%7C3a857f7a1bc3466a540708d849142d91%7Cc9b81ecc188c41d998a221545facf34f%7C0%7C0%7C637339698295673927&sdata=PrJyKkYZUpp%2FO0zd0feU8%2B8bimrqcbZ85Vihm0L7Rgk%3D&reserved=0>.
DISCLAIMER This email and any files transmitted with it are confidential and are solely for the use of the individual or entity to which it is addressed. Any use, distribution, copying or disclosure by any other person is strictly prohibited. If you receive this transmission in error, please notify the sender by reply email and then destroy the message. Opinions, conclusions and other information in this message that do not relate to official business of the company shall be understood to be neither given nor endorsed by JUBILANT FOODWORKS LTD. Any information contained in this email, when addressed to Clients is subject to the terms and conditions in governing client contract
|
The Question
I am unable to upload a plain text file to my S3 bucket with 'putobjectAsync' method in one of my lambda functions .
My lambda is working in below three step
Above all steps are working with the Mock Lambda test tool. but it is not working,when i am uploading my publish zip file into Lambda function.
--> My UploadFile function code is here
Environment
This is a ❓ general question
The text was updated successfully, but these errors were encountered: