AWS - 从 EC2 连接到 SQS

AWS - Connect to SQS from EC2

我正在使用以下代码获取 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;
}
  • 创建一个具有足够权限的 IAM 角色,以便您的应用程序将发出 API 请求
  • 将 IAM 角色分配给 EC2 实例

AWS SDK 将自动从实例元数据中检索凭证。无需提供任何凭据。

参见:Working with AWS Credentials - AWS SDK for Java