使用Terraform创建角色并绑定自定义权限策略

更新时间:

本文介绍如何使用Terraform创建角色并绑定权限策略。

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 创建RAM角色。

    1. 创建terraform.tf文件,输入以下内容,并保存在当前的执行目录中。

      resource "alicloud_ram_role" "role" {
        name = "testRole"
        document = <<EOF
          {
            "Statement": [
              {
                "Action": "sts:AssumeRole",
                "Effect": "Allow",
                "Principal": {
                  "Service": [
                    "apigateway.aliyuncs.com",
                    "ecs.aliyuncs.com"
                  ]
                }
              }
            ],
            "Version": "1"
          }
      EOF
        description = "this is a role test."
        force       = true
      }
    2. 运行terraform apply开始创建。

    3. 运行terraform show查看创建的角色。

  2. 创建自定义权限策略。

    1. terraform.tf文件中增加以下内容。

      resource "alicloud_ram_policy" "policy" {
        policy_name     = "testPolicy"
        policy_document = <<EOF
          {
            "Statement": [
              {
                "Action": [
                  "oss:ListObjects",
                  "oss:GetObject"
                ],
                "Effect": "Deny",
                "Resource": [
                  "acs:oss:*:*:mybucket",
                  "acs:oss:*:*:mybucket/*"
                ]
              }
            ],
              "Version": "1"
          }
      EOF
        description     = "this is a policy test"
        force           = true
      }
    2. 运行terraform apply开始创建。

    3. 运行terraform show查看创建的自定义权限策略。

  3. 为角色绑定权限策略。

    1. terraform.tf文件中增加以下内容。

      resource "alicloud_ram_role_policy_attachment" "attach" {
        policy_name = alicloud_ram_policy.policy.policy_name
        role_name   = alicloud_ram_role.role.name
        policy_type = alicloud_ram_policy.policy.type
      }
    2. 运行terraform apply开始创建。

    3. 运行terraform show查看角色拥有的自定义权限。

操作样例

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

resource "alicloud_ram_role" "role" {
  name        = "testRole"
  document    = <<EOF
    {
      "Statement": [
        {
          "Action": "sts:AssumeRole",
          "Effect": "Allow",
          "Principal": {
            "Service": [
              "apigateway.aliyuncs.com",
              "ecs.aliyuncs.com"
            ]
          }
        }
      ],
      "Version": "1"
    }
EOF
  description = "this is a role test."
  force       = true
}

resource "alicloud_ram_policy" "policy" {
  policy_name     = "testPolicy"
  policy_document = <<EOF
    {
      "Statement": [
        {
          "Action": [
            "oss:ListObjects",
            "oss:GetObject"
          ],
          "Effect": "Deny",
          "Resource": [
            "acs:oss:*:*:mybucket",
            "acs:oss:*:*:mybucket/*"
          ]
        }
      ],
        "Version": "1"
    }
EOF
  description     = "this is a policy test"
  force           = true
}

resource "alicloud_ram_role_policy_attachment" "attach" {
  policy_name = alicloud_ram_policy.policy.policy_name
  role_name   = alicloud_ram_role.role.name
  policy_type = alicloud_ram_policy.policy.type
}