【AWS SDK PHP】无法通过 IAM 角色从 ECS 访问密钥管理器



我有一个在ECS Fargate 1.14上运行的php应用程序。 AWS-SDK-PHP 版本是 3。

出于安全原因,我希望按 IAM 角色(而不是 AWS 访问密钥/ID)获取存储在 AWS SecretsManager 中的密钥值。 我像这样设置了SecretsManagerClient。

$config = [
'version' => '2017-10-17',
'region' => 'ap-northeast-1',
];
return new SecretsManagerClient($config);

我没有添加"凭证",因为我想通过 IAM 角色访问密钥管理器。

我的容器的"任务角色"(不是任务执行角色)具有策略"机密管理器读写"。

但是,我无法访问出现此错误的机密管理器。

Error: [AwsExceptionCredentialsException] Error retrieving credentials from the instance profile metadata service. (cURL error 7: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)) in /share/swell/cgi-bin/vendor/aws/aws-sdk-php/src/Credentials/InstanceProfileProvider.php on line 240

此消息是什么意思?

我知道 AWSClient 按以下顺序查找凭证:

1 Load credentials from environment variables.
2 Load credentials from a credentials .ini file.
3 Load credentials from an IAM role.

这种情况不适用于3号吗?

还是我附加了错误的策略?

在 2021/06/18 上添加信息

我将附加到 ECS 任务角色的策略更改为"高级用户访问"。错误消息更改为 404。

2021-06-18 01:52:12 Error: [AwsExceptionCredentialsException] Error retrieving credentials from the instance profile metadata service. (Client error: `GET http://169.254.169.254/latest/meta-data/iam/security-credentials/` resulted in a `404 Not Found` response:

这是因为 EC2 和 ECS 具有不同的位置来获取凭证信息。

  • EC2
http://169.254.169.254/latest/meta-data/iam/security-credentials/
  • 弹性云服务器
http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI

我必须将环境变量"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"设置为我的 Web 应用程序容器。 我把这一行添加到我的Dockerfile中,现在我可以访问SecretsManager。

RUN echo 'export $(strings /proc/1/environ | grep AWS_CONTAINER_CREDENTIALS_RELATIVE_URI)' >> /etc/bashrc

https://forums.aws.amazon.com/thread.jspa?threadID=273767

最新更新