All Products
Search
Document Center

Terraform:Buat peran RAM dan berikan izin

Last Updated:Jun 04, 2026

Gunakan Terraform untuk membuat peran RAM dan menyambungkan kebijakan izin kustom.

Catatan

Jalankan kode contoh secara langsung di Terraform Explorer.

Prasyarat

  • Untuk keamanan, gunakan pengguna RAM dengan izin minimum yang diperlukan. Buat pengguna RAM dan Berikan izin kepada pengguna RAM. Izin yang diperlukan:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "ram:GetRole",
            "ram:ListPoliciesForRole",
            "ram:ListRoles",
            "ram:CreateRole",
            "ram:DeleteRole",
            "ram:DetachPolicyFromRole",
            "ram:UpdateRole",
            "ram:GetPolicy",
            "ram:GetPolicyVersion",
            "ram:AttachPolicyToRole",
            "ram:CreatePolicy",
            "ram:CreatePolicyVersion",
            "ram:ListEntitiesForPolicy",
            "ram:ListPolicyVersions",
            "ram:DeletePolicy",
            "ram:DeletePolicyVersion",
            "ram:ListPoliciesForGroup",
            "ram:ListPolicies",
            "ram:ListPolicyAttachments"
          ],
          "Resource": "*"
        }
      ]
    }
  • Persiapkan lingkungan Terraform. Anda dapat menggunakan salah satu metode berikut untuk menjalankan Terraform.

    • Gunakan Terraform di Terraform Explorer: Alibaba Cloud menyediakan lingkungan online untuk menjalankan Terraform. Anda tidak perlu menginstal Terraform. Anda dapat masuk untuk menggunakan dan mencoba Terraform secara online. Metode ini cocok untuk skenario ketika Anda ingin mencoba dan men-debug Terraform dengan cepat dan nyaman tanpa biaya.

    • Cloud Shell: Alibaba Cloud Cloud Shell telah memiliki komponen Terraform yang pra-instal dan kredensial identitas yang dikonfigurasi. Anda dapat menjalankan perintah Terraform langsung di Cloud Shell. Metode ini cocok untuk skenario ketika Anda ingin mengakses dan menggunakan Terraform dengan cepat dan nyaman dengan biaya rendah.

    • Instal dan konfigurasi Terraform di mesin lokal Anda: Metode ini cocok untuk skenario dengan koneksi jaringan yang buruk atau ketika Anda memerlukan lingkungan pengembangan kustom.

Resource yang digunakan

Langkah 1: Buat kebijakan

  1. Buat direktori kerja dengan file main.tf, lalu salin kode berikut ke dalam main.tf. Kode ini membuat kebijakan kustom menggunakan Bahasa Kebijakan.

    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    
    # Create a policy.
    resource "alicloud_ram_policy" "policy" {
      policy_name     = "policy-name-${random_integer.default.result}"
      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. Inisialisasi lingkungan runtime Terraform.

    terraform init

    Output yang diharapkan jika berhasil:

    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. Terapkan konfigurasi.

    terraform apply

    Masukkan yes saat diminta, lalu tekan Enter. Output yang diharapkan:

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. Verifikasi hasilnya.

    Terraform show

    Jalankan perintah berikut untuk melihat resource yang dibuat:

    terraform show
    shell@Alicloud:~/ram2$ terraform show
    # alicloud_ram_policy.policy:
    resource "alicloud_ram_policy" "policy" {
        attachment_count = 0
        default_version  = "v1"
        description      = "this is a policy test"
        document         = <<-EOT
            {
    
                "Statement": [
                    {
                        "Action": [
                            "oss:ListObjects",
                            "oss:GetObject"
                        ],
                        "Effect": "Deny",
                        "Resource": [
                            "acs:oss:*:*:mybucket",
                            "acs:oss:*:*:mybucket/*"
                        ]
                    }
                ],
                "Version": "1"
            }
    
        EOT
        force            = true
        id               = "policy-name-xxx"
        name             = "policy-name-xxx"
        policy_document  = <<-EOT
            {
    
                "Statement": [
                    {
                        "Action": [
                            "oss:ListObjects",
                            "oss:GetObject"
                        ],
                        "Effect": "Deny",
                        "Resource": [
                            "acs:oss:*:*:mybucket",
                            "acs:oss:*:*:mybucket/*"
                        ]
                    }
                ],
                "Version": "1"
            }
    
        EOT
        policy_name      = "policy-name-xxx"
        type             = "Custom"
    }

    Konsol

    Login ke Konsol RAM. Pilih Permissions > Policies untuk melihat kebijakan tersebut.

    Kebijakan tersebut terdaftar sebagai kebijakan kustom dengan deskripsi this is a policy test dan jumlah referensi 0.

Langkah 2: Buat peran RAM dan berikan izin

  1. Tambahkan kode berikut ke file main.tf Anda.

    # Create a RAM role.
    resource "alicloud_ram_role" "role" {
      name        = "role-name-${random_integer.default.result}"
      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
    }
    
    # Attach the policy to the RAM role.
    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. Buat rencana eksekusi dan pratinjau perubahan.

    terraform plan
  3. Terapkan konfigurasi.

    terraform apply

    Masukkan yes saat diminta, lalu tekan Enter. Output yang diharapkan:

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. Verifikasi hasilnya.

    Terraform show

    Jalankan perintah berikut untuk melihat resource yang dibuat:

    terraform show
    # alicloud_ram_role.role:
    resource "alicloud_ram_role" "role" {
        arn                  = "acs:ram::xxx:role/role-name-xxx"
        description          = "this is a role test."
        document             = jsonencode(
            {
                Statement = [
                    {
                        Action    = "sts:AssumeRole"
                        Effect    = "Allow"
                        Principal = {
                            Service = [
                                "apigateway.aliyuncs.com",
                                "ecs.aliyuncs.com",
                            ]
                        }
                    },
                ]
                Version   = "1"
            }
        )
        force                = true
        id                   = "role-name-xxx"
        max_session_duration = 3600
        name                 = "role-name-xxx"
        ram_users            = []
        role_id              = "xxx"
        services             = [
            "apigateway.aliyuncs.com",
            "ecs.aliyuncs.com",
        ]
        version              = "1"
    }
    
    # alicloud_ram_role_policy_attachment.attach:
    resource "alicloud_ram_role_policy_attachment" "attach" {
        id          = "role:policy-name-xxx:Custom:role-name-xxx"
        policy_name = "policy-name-xxx"
        policy_type = "Custom"
        role_name   = "role-name-xxx"
    }

    Konsol

    1. Login ke Konsol RAM. Pilih Identities > Roles untuk melihat peran RAM.

    2. Klik Role Name untuk melihat izinnya.

      Pada halaman detail peran, di tab Permissions, verifikasi bahwa kebijakan telah disambungkan ke peran tersebut. Tabel kebijakan menampilkan informasi seperti nama kebijakan, deskripsi, cakupan resource, dan waktu penyambungan.

Melepaskan sumber daya

Jalankan terraform destroy untuk melepas resource yang tidak lagi Anda perlukan. Perintah umum.

terraform destroy

Contoh lengkap

Catatan

Jalankan kode contoh secara langsung di Terraform Explorer.

Kode contoh

resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# Create a policy.
resource "alicloud_ram_policy" "policy" {
  policy_name     = "policy-name-${random_integer.default.result}"
  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
}

# Create a RAM role.
resource "alicloud_ram_role" "role" {
  name        = "role-name-${random_integer.default.result}"
  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
}

# Attach the policy to the RAM role.
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
}

Jelajahi folder spesifik produk di repositori More Complete Examples.