LoginSignup
3
1

More than 5 years have passed since last update.

terraform import した aws_iam_role の assume_role_policy の確認方法

Posted at

下記のように aws_iam_role をimportしてきた時、

$ terraform import aws_iam_role.foo foo

aws_iam_role リソースの assume_role_policy は必須パラメータで、
「これの正確な定義はどこで分かるんだ :thinking: 」となります。

resource "aws_iam_role" "foo" {
  name               = "instance_role"
  path               = "/system/"
  assume_role_policy = ""
}

解決策

そんな時は、リソースの実体をみればOK。
awscli では以下で取得できます。
(AssumeRolePolicyDocument がほしかった assume_policy_role)

$ aws iam get-role --role-name=foo | jq .Role.AssumeRolePolicyDocument
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "cognito-idp.amazonaws.com"
      },
      "Action": "sts:AssumeRole",
      "Condition": {
        "StringEquals": {
          "sts:ExternalId": "xxx"
        }
      }
    }
  ]
}

Appendix

jsonをコピペでも良いのですが、data リソースなんかを使ってあげると、きれいに構造が見えます。

data "aws_iam_policy_document" "foo" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["cognito-idp.amazonaws.com"]
    }

    condition {
      test     = "StringEquals"
      variable = "sts:ExternalId"

      values = [
        "xxx",
      ]
    }
  }
}

resource "aws_iam_role" "foo" {
  name               = "foo"
  path               = "/service-role/"
  assume_role_policy = "${data.aws_iam_policy_document.foo.json}"
}

References

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1