【问题标题】:【AWS SDK PHP】Unable to access SecretsManager from ECS by IAM role【AWS SDK PHP】无法通过IAM角色从ECS访问SecretsManager
【发布时间】:2021-09-02 00:34:47
【问题描述】:

我有一个在 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 角色访问 SecretsManager。

我的容器的“任务角色”(不是任务执行角色)具有“SecretsManagerReadWrite”策略。

但是,由于此错误,我无法访问 SecretsManager。

Error: [Aws\Exception\CredentialsException] 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 任务角色的策略更改为“PowerUserAccess”。错误信息更改为 404。

2021-06-18 01:52:12 Error: [Aws\Exception\CredentialsException] 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:

【问题讨论】:

    标签: amazon-iam aws-secrets-manager aws-php-sdk


    【解决方案1】:

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

    • EC2
    http://169.254.169.254/latest/meta-data/iam/security-credentials/
    
    • ECS
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-01
      • 2022-06-30
      • 2022-01-13
      • 2019-11-23
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      相关资源
      最近更新 更多