从 ec2 实例提交 curl 命令不会发送附加安全配置文件的凭据

submitting a curl command from an ec2 instance does not send credentials of the attached security profile

我有一个附有实例配置文件的 ec2 实例。此实例配置文件有权将消息发布到 sns 主题。当我远程进入 ec2 实例并发出类似

的命令时
aws sns publish --topic-arn topic_arn --message hello

这有效。

现在我尝试用一​​个简单的 curl 命令做同样的事情,这就是我在远程访问 ec2 实例后使用的命令

curl "https://sns.us-west-2.amazonaws.com/?Message=hello&Action=Publish&TargetArn=topic_arn" 

我收到以下错误

<Code>MissingAuthenticationToken</Code>
<Message>Request is missing Authentication Token</Message>

我希望 curl 在发送请求时附加实例配置文件详细信息(例如使用 aws cli 时),但它似乎没有这样做。有谁知道我该如何克服这个问题?

当你这样做时:

curl "https://sns.us-west-2.amazonaws.com/?Message=hello&Action=Publish&TargetArn=topic_arn" 

您正在直接向 SNS 端点发出请求。这意味着你必须 sign your request with AWS credentials from your instance profile. If you don't want to use AWS CLI or any AWS SDK for accessing the SNS, you have to program the entire signature procedure yourself as described in the docs.

这就是为什么当您使用 AWS CLI 时

aws sns publish --topic-arn topic_arn --message hello

一切正常,因为 AWS CLI 代表您向 SNS 端点发出签名请求