简体   繁体   English

无法在 EC2 实例上加载 AWS 凭证

[英]Unable to load AWS credentials on EC2 Instance

The application应用程序

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. 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.

The problem问题

Locally after setting local variables ( AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION ) in my OS (Windows) app works just fine, but the problem starts after deploying it.在我的操作系统(Windows)应用程序中设置局部变量( AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_DEFAULT_REGION )后在本地工作得很好,但问题在部署后就开始了。 I have an EC2 Instance with Amazon Linux AMI on AWS:我在 AWS 上有一个带有 Amazon Linux AMI 的 EC2 实例:

  • created user at AWS Identity and Access Management (IAM), granted AmazonSESFullAccess role在 AWS Identity and Access Management (IAM) 创建用户,授予 AmazonSESFullAccess 角色
  • logging by CMD using shh with the private key file *.pem CMD 使用带有私钥文件 *.pem 的 shh 进行日志记录
  • Tomcat8 service started Tomcat8服务启动
  • MySQL service started MySQL 服务启动
  • application *.war file deployed已部署应用程序 *.war 文件
  • created environment variables using 'export' command and I checked 'printenv' just to be sure everything is fine使用“导出”命令创建了环境变量,我检查了“printenv”以确保一切正常
  • after sending POST request I got exception (below) which means that user has been created but Amazon SES didn't send an email confirmation because couldn't authenticate发送 POST 请求后出现异常(如下),这意味着用户已创建,但 Amazon SES 未发送 email 确认,因为无法进行身份验证
{
    "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/]"
}
  • I checked again local environment variables on my EC2 instance and it was looking fine but to be sure I re-configured it using 'aws configure' command我再次检查了我的 EC2 实例上的本地环境变量,它看起来很好,但为了确保我使用“aws configure”命令重新配置了它

  • exception keeps showing, somehow application cannot get environment variables, I'm fighting with that for over 5 hours now so hopefully someone will come here to rescue me...异常不断显示,不知何故应用程序无法获取环境变量,我现在已经为此奋斗了 5 个多小时,所以希望有人能来这里救我......

Piece of code (works fine locally):一段代码(在本地工作正常):

        AmazonSimpleEmailService client =
                AmazonSimpleEmailServiceClientBuilder
                        .standard()
                        .withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
                        .withRegion(Regions.EU_CENTRAL_1)
                        .build();

I am total Linux noob, having problems with simple commands so please be gentle with solutions requiring some console commands.我是 Linux 菜鸟,对简单命令有问题,所以请对需要一些控制台命令的解决方案保持温和。

If you're running app on EC2, don't use IAM user.如果您在 EC2 上运行应用程序,请不要使用 IAM 用户。

Instead create IAM role with same permissions and assign that role to the instance.而是创建具有相同权限的 IAM角色并将该角色分配给实例。 If app uses AWS SDK it will be able to pick up credentials without any problems.如果应用程序使用 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. 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.

The DefaultAWSCredentialsProvider has multiple places it will look for credentials. DefaultAWSCredentialsProvider 有多个地方会查找凭证。 Instead of setting up your credentials as an environment variable, you can set up a credentials profile.您可以设置凭据配置文件,而不是将凭据设置为环境变量。 See this documentation: Working with AWS Credentials .请参阅此文档: 使用 AWS 凭证

Make sure you have the AWS CLI installed , then you can run the following command to configure your profile: aws configure确保您已安装 AWS CLI ,然后您可以运行以下命令来配置您的配置文件: aws configure

Click here for the documentation on the aws configure command.单击此处获取有关 aws configure 命令的文档。

If you have already configured your aws profile and it still does not work, you have most likely configured the profile for the wrong linux user.如果您已经配置了您的 aws 配置文件但它仍然无法工作,那么您很可能为错误的 linux 用户配置了配置文件。 For example, if a linux user named tomcat8 is the user who is running your tomcat instance, then you need to set up a credentials profile at /home/tomcat8/.aws/credentials/例如,如果名为 tomcat8 的 linux 用户是运行 tomcat 实例的用户,那么您需要在 /home/tomcat8/.aws/credentials/ 中设置凭据配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM