Docker中的Asp.net core5.0 API.如何访问AWS参数存储?



我有一个Asp.NetCore5.0应用程序。
我构建了一个docker映像:

# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/aspnet:5.0
COPY bin/Release/netcoreapp3.1/publish/ App/
WORKDIR /App
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

应用程序应该使用AWS参数存储,如:

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var envName = hostingContext.HostingEnvironment.EnvironmentName.ToString().ToLower();
config.AddSystemsManager($"/{envName}", TimeSpan.FromMinutes(5));
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

在我的本地机器上运行(我在本地机器上安装了AWS Cli)没有任何问题。
但是它不能在Docker容器中工作
在我的Linux服务器上
(AWS Cli安装在服务器上[不在docker中]也经过了测试)。
服务器不是AWS云的一部分。

异常

Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> Amazon.Runtime.AmazonClientException: No RegionEndpoint or ServiceURL configured
at Amazon.Runtime.ClientConfig.Validate()
at Amazon.Runtime.AmazonServiceClient..ctor(AWSCredentials credentials, ClientConfig config)
at Amazon.SimpleSystemsManagement.AmazonSimpleSystemsManagementClient..ctor(AWSCredentials credentials, AmazonSimpleSystemsManagementConfig clientConfig)
--- End of inner exception stack trace ---

我已经尝试传递No RegionEndpoint或ServiceURL配置为ENV等,但没有成功。

docker run -d -p 12080:80 -e AWS_ACCESS_KEY_ID=thisIsMyId -e AWS_SECRET_ACCESS_KEY=secret --name nameoftheapi dockerhubuser/nameoftheapi:262
docker run -d -p 12080:80-v $HOME/.aws/credentials:/home/app/.aws/credentials:ro --name nameoftheapi dockerhubuser/nameoftheapi:262

相同的例外。

如何从Docker容器内部访问AWS Cli ?
我需要在Docker内部的AWS Cli吗?还有其他解决办法吗?

Thanks in advance

我的解决方案是将包含aws凭证的文件夹传递给docker容器。

docker run -d  -p 12080:80 -v /root/.aws:/root/.aws:ro  --name nameofthecontainer dockerhubuser/someimage:264

我还在docker-host-server上安装了aws cli。我想我现在可以卸载它了。我只需要凭证。

我创建了一个可以访问AWS上参数存储的用户。
创建了一些参数和访问策略。

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ssm:GetParametersByPath",
"ssm:GetParameters",
"ssm:GetParameter"
],
"Resource": "arn:aws:ssm:eu-central-1:12345678:parameter/abc/*"
}
]
}

最后一个问题是错误的参数路径

/abc/Release/def/ghi/jkl
instead of
/abc/Production/def/ghi/jkl

基于此代码中对环境的错误假设

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var envName = hostingContext.HostingEnvironment.EnvironmentName.ToString().ToLower();
config.AddSystemsManager($"/{envName}", TimeSpan.FromMinutes(5));
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

我现在很高兴。

最新更新