c# 如何在 AWS Lambda 调用中识别 accountId 和 Region?
c# How to identify accountId and Region in a AWS Lambda call?
我需要确定触发 AWS Lambda 函数的 AWS 账户的 accountId 和区域。我没有在 ILambdaContext 中找到这些信息。
我怎样才能访问这些信息?
它是由 SNS 触发的
我正在使用 .Net Core 2.1
您可以将AccountNumber添加到环境变量中读取:
var accountNumber = System.Environment.GetEnvironmentVariable("AccountNumber")
如果您有权访问 Lambda 执行上下文,则可以直接从上下文对象中的 InvokedLambdaFunctionArn 中提取 AWS 区域和 AWS 账户 ID。
当您调用 Lambda 函数时,根据集成类型,处理程序接受一个事件对象或两个参数 - 请求对象和执行上下文。
识别帐户 ID 的正确方法是使用安全令牌服务 (STS)。使用以下方法
https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SecurityToken/MISecurityTokenServiceGetCallerIdentityGetCallerIdentityRequest.html
IAmazonSecurityTokenService.GetCallerIdentity
添加 Nuget 包 AWSSDK.SecurityToken ,然后创建 sts 客户端
IAmazonSecurityTokenService stsClient = new AmazonSecurityTokenServiceClient();
string accountId=stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()).Result.Account;
我需要确定触发 AWS Lambda 函数的 AWS 账户的 accountId 和区域。我没有在 ILambdaContext 中找到这些信息。 我怎样才能访问这些信息? 它是由 SNS 触发的 我正在使用 .Net Core 2.1
您可以将AccountNumber添加到环境变量中读取:
var accountNumber = System.Environment.GetEnvironmentVariable("AccountNumber")
如果您有权访问 Lambda 执行上下文,则可以直接从上下文对象中的 InvokedLambdaFunctionArn 中提取 AWS 区域和 AWS 账户 ID。 当您调用 Lambda 函数时,根据集成类型,处理程序接受一个事件对象或两个参数 - 请求对象和执行上下文。
识别帐户 ID 的正确方法是使用安全令牌服务 (STS)。使用以下方法 https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SecurityToken/MISecurityTokenServiceGetCallerIdentityGetCallerIdentityRequest.html
IAmazonSecurityTokenService.GetCallerIdentity
添加 Nuget 包 AWSSDK.SecurityToken ,然后创建 sts 客户端
IAmazonSecurityTokenService stsClient = new AmazonSecurityTokenServiceClient();
string accountId=stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()).Result.Account;