~/.aws/credentials
~/.aws/config
文件:
[default]
region = us-west-2
output = json
[profile myRole]
role_arn = arn:aws:iam::XXXXXXXXX:role/my-role
region = us-west-2
source_profile = default
通过所有这些设置,我仍在启动时有以下传奇:
Ignoring queue with name 'TEST-QUEUE.fifo': The queue does not exist.; nested exception is com.amazonaws.services.sqs.model.QueueDoesNotExistException: The specified queue does not exist for this wsdl version. (Service: AmazonSQS; Status Code: 400; Error Code: AWS.SimpleQueueService.NonExistentQueue; Request ID: XXXXX-25a7-56e8-8b90-c15ba6a9d160; Proxy: null)
我还确保可以通过以下命令从AWS CLI访问SQS:
aws sqs get-queue-attributes --queue-url https://sqs.us-west-2.amazonaws.com/XXXXXX/TEST-QUEUE.fifo --profile=myRole --attribute-names=All
,也可以通过在单独的python脚本中使用boto3库将消息发送到队列
问题是,虽然AWS CLI和BOTO3自动使用AWS配置文件自动处理角色假设,但Spring Cloud AWS并未开箱即用。结果,您的Java应用程序实际上并未假设您的AWS配置中定义了IAM角色,并且最终使用无法访问指定SQS队列的凭据,因此“队列不存在”,
要解决这个问题,您需要明确配置您的AWS凭据提供商担任角色。一种方法是创建使用stsassumerolesessessessencredentialsprovider的bean。例如:@Bean
public AWSCredentialsProvider awsCredentialsProvider() {
// Use the default profile as the source of credentials
AWSCredentialsProvider basicProvider = new ProfileCredentialsProvider("default");
// Create an STS client using the basic provider
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion("us-west-2")
.withCredentials(basicProvider)
.build();
// Return a credentials provider that assumes the specified role
return new STSAssumeRoleSessionCredentialsProvider.Builder("arn:aws:iam::XXXXXXXXX:role/my-role", "mySession")
.withStsClient(stsClient)
.build();
}
然后配置您的SQS客户端(或Spring Cloud AWS上下文)使用此凭据提供商。这种明确的配置可确保正确假定该角色,并且您的应用程序将拥有正确的权限以访问队列。
关注我,因此您可以随时向我发消息以后的问题。如果有帮助,请接受答案并也投票。