【问题标题】:c# How to identify accountId and Region in a AWS Lambda call?c# 如何在 AWS Lambda 调用中识别 accountId 和 Region?
【发布时间】:2019-05-20 02:49:07
【问题描述】:

我需要确定触发 AWS Lambda 函数的 AWS 账户的 accountId 和区域。我没有在 ILambdaContext 中找到这些信息。 我怎样才能访问这些信息? 它是由 SNS 触发的 我正在使用 .Net Core 2.1

【问题讨论】:

    标签: c# amazon-web-services .net-core aws-lambda amazon-sns


    【解决方案1】:

    如果您有权访问您的 Lambda 执行上下文,则可以直接从上下文对象中的 InvokedLambdaFunctionArn 中提取 AWS 区域和 AWS 账户 ID。 当您调用 Lambda 函数时,根据集成类型,处理程序接受一个事件对象或两个参数 - 请求对象和执行上下文。

    【讨论】:

      【解决方案2】:

      识别帐户 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;
      

      【讨论】:

        【解决方案3】:

        您可以将 AccountNumber 添加到环境变量并阅读:

        var accountNumber = System.Environment.GetEnvironmentVariable("AccountNumber")
        

        【讨论】:

        • 其实我在考虑超过1个账号可以触发lambda函数,我需要触发该账号的SNS上发布消息的accountId。这样,它应该是动态的
        猜你喜欢
        • 2018-12-13
        • 2020-10-31
        • 2018-01-24
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-06
        相关资源
        最近更新 更多