-
Describe the bugI am trying to implement lambda which will assume role and then will send a file to S3 bucket through Multi-region access point. Lambda throws Internal Server Error exception -> See lambda details in reproduction Steps. Expected BehaviorFile should appear in S3 bucket under the Multi-Region Access Point. Current BehaviorBelow exception is thrown from line "var result = s3Client.PutObjectAsync(putObjectRequest).Result;": System.AggregateException: One or more errors occurred. (We encountered an internal error. Please try again.) ---> Amazon.S3.AmazonS3Exception: We encountered an internal error. Please try again. ---> Amazon.Runtime.Internal.HttpErrorResponseException: Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown. at Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken cancellationToken) at Amazon.Runtime.Internal.HttpHandler Reproduction Steps
And with below code:
Possible SolutionNo response Additional Information/ContextThe same situation occurs when I try to execute the Lambda Function Code from Console Application on Windows 10 machine while I have aws credentials configured which allows to assume role created in point 2 of reproduction steps. Also worth to mention that below powershell + AWS CLI code works correctly. This code is doing a similar job as lambda while in this situation the operation ends successfully:
Version of AWS CLI: AWS .NET SDK and/or Package version usedAmazon.Lambda.Core 2.1.0 Targeted .NET Platform.NET 6 Operating System and versionLambda |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
Hi @kpok, Good afternoon. I'm unsure how you have configured Lambda and roles, here are the steps that I used to reproduce the issue:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Principal": {
"AWS": [
"arn:aws:iam::<<account-id>>:role/testmraplambda"
]
},
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3::<<account-id>>:accesspoint/<<mrap-alias.mrap>>/object/*"
]
}
]
}
using Amazon;
using Amazon.Lambda.Core;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.SecurityToken;
using Amazon.SecurityToken.Model;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace TestMRAPLambda;
public class Function
{
public void FunctionHandler(ILambdaContext context)
{
AWSConfigs.LoggingConfig.LogTo = LoggingOptions.Console;
AWSConfigs.LoggingConfig.LogResponses = ResponseLoggingOption.Always;
AWSConfigs.LoggingConfig.LogMetrics = true;
var s3Endpoint = "arn:aws:s3::<<account-id>>:accesspoint/<<mrapalias.mrap>>";
Console.WriteLine($"S3Endpoint: {s3Endpoint}");
var s3Client = new AmazonS3Client();
var stream = GenerateStreamFromString("test1");
var putObjectRequest = new PutObjectRequest()
{
BucketName = s3Endpoint,
Key = "testFromLambda.txt",
InputStream = stream
};
var result = s3Client.PutObjectAsync(putObjectRequest).Result;
Console.WriteLine(result);
Console.WriteLine(result.HttpStatusCode);
}
public static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
} Also ensured to add reference to <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate ready to run images during publishing to improve cold start time. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.1" />
<PackageReference Include="AWSSDK.Extensions.CrtIntegration" Version="3.7.1.8" />
<PackageReference Include="AWSSDK.S3" Version="3.7.200.1" />
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.200.1" />
</ItemGroup>
</Project> Reference to
I'm unsure why you need to assume a role when you would attach role to Lambda during deployment. Please let me know if I'm missing something from the reproduction steps. Are you using a different role attached to Lambda function? NOTE: I have hard-coded ARNs for reproduction. This should be avoided in production. Also, we should dispose of Amazon S3 client after use (mat be wrap in Thanks, |
Beta Was this translation helpful? Give feedback.
-
Hi @ashishdhingra For the bug reproduction I am trying to assume role from another lambda role on the same AWS account but the same error occurs also when I try to do with different accounts. Regards, |
Beta Was this translation helpful? Give feedback.
-
One common "sharp edge" with MRAPs and assumed roles is mentioned on https://docs.aws.amazon.com/AmazonS3/latest/userguide/MultiRegionAccessPointRestrictions.html
V3 of the .NET SDK still defaults to the global STS endpoint. Can you check which endpoint the STS request is going to? And if it's still the global endpoint, configure it to go to a regional endpoint instead?
|
Beta Was this translation helpful? Give feedback.
-
Hi @ashovlin I replaced below line: with lines: var config = new AmazonSecurityTokenServiceConfig() { StsRegionalEndpoints = StsRegionalEndpointsValue.Regional }; and the solution started to work. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Glad it worked! Yeah, the defaulting to |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
One common "sharp edge" with MRAPs and assumed roles is mentioned on https://docs.aws.amazon.com/AmazonS3/latest/userguide/MultiRegionAccessPointRestrictions.html