AWS - 从EC2

时间:2018-05-08 02:33:40

标签: java spring amazon-web-services amazon-ec2 spring-cloud

我使用以下代码获取AWS凭据以连接到SQS。这是我的个人凭证,当我在qa或prod env中部署此代码时,我不应该使用我的凭据连接到SQS。此代码将在AWS中的EC2容器中运行。有什么选择可以连接到SQS而无需像我在下面那样明确提供用户ID /密码?

@Bean
public AWSCredentialsProvider awsCredentialsProvider() {

    ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(credentialsProvider);
    try {
        profileCredentialsProvider.getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException(
                "Cannot load the credentials from the credential profiles file. " +
                        "Please make sure that your credentials file is at the correct " +
                        "location (~/.aws/credentials), and is in valid format.",
                e);
    }
    return profileCredentialsProvider;
}

1 个答案:

答案 0 :(得分:2)

  • 创建一个具有足够权限的IAM角色,以支持您的应用提出的API请求
  • 将IAM角色分配给EC2实例

AWS开发工具包将自动从实例元数据中检索凭据。没有必要提供任何凭证。

请参阅:Working with AWS Credentials - AWS SDK for Java