AWS - 从EC2连接到SQS

问题描述 投票:-1回答:1

我使用以下代码获取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;
}
java spring amazon-web-services amazon-ec2 spring-cloud
1个回答
2
投票
  • 创建具有足够权限的IAM角色,以获取您的应用将发出的API请求
  • 将IAM角色分配给EC2实例

AWS开发工具包将自动从实例元数据中检索凭据。无需提供任何凭据。

见:Working with AWS Credentials - AWS SDK for Java

© www.soinside.com 2019 - 2024. All rights reserved.