【发布时间】:2020-09-05 13:30:29
【问题描述】:
我有一个使用 AWS SAM 创建的 C# AWS Lambda 函数。该函数尝试使用以下代码从系统管理中获取参数:
public async Task<string> GetConfiguration(string parameterName)
{
var request = new GetParameterRequest
{
Name = $"/project-name/{parameterName}",
WithDecryption = true
};
using (var client = new AmazonSimpleSystemsManagementClient(RegionEndpoint.EUWest1))
{
var response = await client.GetParameterAsync(request);
return response.Parameter.Value;
}
}
SAM 模板指定函数以具有ssm:GetParameter* 权限的 IAM 用户身份运行。当我将该函数部署到 AWS 时,此代码完全按预期工作。如果我在 Visual Studio 中运行,API 也可以访问参数。
但是,当我在本地运行代码时:
sam local start-api
我得到了这个例外:
[错误] Amazon.Lambda.AspNetCoreServer.AbstractAspNetCoreFunction: 响应请求的未知错误: AmazonSimpleSystemsManagementException: Amazon.SimpleSystemsManagement.AmazonSimpleSystemsManagementException: 请求中包含的安全令牌无效 ---> Amazon.Runtime.Internal.HttpErrorResponseException:引发了 Amazon.Runtime.Internal.HttpErrorResponseException 类型的异常。在 Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken 取消令牌)在 Amazon.Runtime.Internal.HttpHandler1.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext 执行上下文) --- 内部异常堆栈跟踪结束 --- 在 Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext executionContext, HttpErrorResponseException 异常)在 Amazon.Runtime.Internal.ExceptionHandler1.Handle(IExecutionContext executionContext,异常异常)在 Amazon.Runtime.Internal.ErrorHandler.ProcessException(IExecutionContext executionContext,异常异常)在 Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext 执行上下文)在 Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext 执行上下文)
我正在努力弄清楚为什么或如何让它发挥作用。
【问题讨论】:
-
我了解到您使用 AWS SAM CLI 在本地部署您的 Lambda 函数。您在运行本地 Lambda 时使用哪个 SSM 端点?您是启动 SSM 服务的本地模拟还是连接到云中的端点?如果后者为真,您如何为您的 Lambda 指定权限?你能粘贴你的模板吗?
-
start-api 也需要
--profile TEXT。也许您必须指定要使用的正确配置文件。似乎是一些权限问题:“请求中包含的安全令牌无效” -
@Marcin 我已经尝试指定
--profile=default这是我本地配置文件的名称,但这没有什么区别。有没有办法查看函数作为哪个 IAM 角色运行?
标签: c# amazon-web-services aws-lambda aws-sam aws-sam-cli