如何在Terraform中获取AWS用户名(不是完整的ARN)?

时间:2017-05-20 18:54:50

标签: amazon-web-services aws-sdk terraform

如果我有凭据给我以下内容......

$ aws iam get-user
{
    "User": {
        "UserName": "fred-bob",
        "Path": "/",
        "CreateDate": "2017-04-01T12:34:56Z",
        "UserId": "ABCD1234...",
        "Arn": "arn:aws:iam::123456789012:user/fred-bob"
    }
}

...是否有一个Terraform变量只能给我"fred-bob"而不是完整的ARN?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用iam_user资源并通过name属性访问IAM用户的名称。

您可以轻松导入现有的" fred-bob"用户并观察名称" fred-bob"存储在名称中:

导入

terraform import aws_iam_user.myuser fred-bob

用法:

"${aws_iam_user.myuser.name}"

生成的tfstate看起来像这样(片段),用#34; fred-bob"可用作名称属性:

"aws_iam_user.myuser": {
    "type": "aws_iam_user",
    "depends_on": [],
    "primary": {
        "id": "fred-bob",
        "attributes": {
            "arn": "arn:aws:iam::AWSACCOUNTNUMBER:user/fred-bob",
            "id": "fred-bob",
            "name": "fred-bob",
            "path": "/",
            "unique_id": "UNIQUEID"
        },
        "meta": {},
        "tainted": false
    },
    "deposed": [],
    "provider": "aws"
}