![](/img/trans.png)
[英]AWS SDK for Java v2 is unable to load credentials from EC2 instance
[英]Unable to load AWS credentials on EC2 Instance
应用程序
Simple REST API registration service in Spring, after sending proper POST request new user is created in database and Amazon SES sends an email with registration link to verify.
问题
在我的操作系统(Windows)应用程序中设置局部变量( AWS_ACCESS_KEY_ID
、 AWS_SECRET_ACCESS_KEY
和AWS_DEFAULT_REGION
)后在本地工作得很好,但问题在部署后就开始了。 我在 AWS 上有一个带有 Amazon Linux AMI 的 EC2 实例:
{
"timestamp": "2020-04-26T15:44:44.010+0000",
"message": "Unable to load AWS credentials from any provider in the chain: [EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)), SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey), WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path., com.amazonaws.auth.profile.ProfileCredentialsProvider@23fac1a3: profile file cannot be null, com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@68aa5a98: The requested metadata is not found at http://169.254.169.254/latest/meta-data/iam/security-credentials/]"
}
我再次检查了我的 EC2 实例上的本地环境变量,它看起来很好,但为了确保我使用“aws configure”命令重新配置了它
异常不断显示,不知何故应用程序无法获取环境变量,我现在已经为此奋斗了 5 个多小时,所以希望有人能来这里救我......
一段代码(在本地工作正常):
AmazonSimpleEmailService client =
AmazonSimpleEmailServiceClientBuilder
.standard()
.withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
.withRegion(Regions.EU_CENTRAL_1)
.build();
我是 Linux 菜鸟,对简单命令有问题,所以请对需要一些控制台命令的解决方案保持温和。
如果您在 EC2 上运行应用程序,请不要使用 IAM 用户。
而是创建具有相同权限的 IAM角色并将该角色分配给实例。 如果应用程序使用 AWS SDK,它将能够毫无问题地获取凭证。
In your case problem is probably app's environment being different from yours, if you export credentials in your bash session it will not pass to app if it's loaded under different user or bash session.
DefaultAWSCredentialsProvider 有多个地方会查找凭证。 您可以设置凭据配置文件,而不是将凭据设置为环境变量。 请参阅此文档: 使用 AWS 凭证。
确保您已安装 AWS CLI ,然后您可以运行以下命令来配置您的配置文件: aws configure
单击此处获取有关 aws configure 命令的文档。
如果您已经配置了您的 aws 配置文件但它仍然无法工作,那么您很可能为错误的 linux 用户配置了配置文件。 例如,如果名为 tomcat8 的 linux 用户是运行 tomcat 实例的用户,那么您需要在 /home/tomcat8/.aws/credentials/ 中设置凭据配置文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.