Search code examples
c#amazon-web-services.net-coreaws-lambdaamazon-sns

c# How to identify accountId and Region in a AWS Lambda call?


I need to identify the accountId and the region of the AWS account that triggered an AWS Lambda function. I did not find these infos in the ILambdaContext. How can I have access to these infos? It's being triggered by an SNS I'm using .Net Core 2.1


Solution

  • The proper way to identify the account id is to use secure token service (STS). Use the following method https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/SecurityToken/MISecurityTokenServiceGetCallerIdentityGetCallerIdentityRequest.html

    IAmazonSecurityTokenService.GetCallerIdentity

    Add the Nuget package AWSSDK.SecurityToken , then create sts client

    IAmazonSecurityTokenService stsClient = new AmazonSecurityTokenServiceClient();
    string accountId=stsClient.GetCallerIdentityAsync(new GetCallerIdentityRequest()).Result.Account;