All Products
Search
Document Center

Container Service for Kubernetes:Use Terraform to activate ACK and assign the default roles to ACK

Last Updated:Jan 09, 2024

When you use Container Service for Kubernetes (ACK) for the first time, you must assign default roles to ACK with your Alibaba Cloud account. Only after you assign these roles to ACK, ACK can access resources in other cloud services, create clusters, or save logs. These cloud services include Elastic Compute Service (ECS), Object Storage Service (OSS), Apsara File Storage NAS (NAS), and Server Load Balancer (SLB). This topic describes how to use Terraform to assign default roles to ACK when you use ACK for the first time.

Table of contents

Prerequisites

  • Terraform is installed.

    Note

    You must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.

    • By default, Cloud Shell has preinstalled Terraform and configured your account information. You do not need to modify the configurations.

    • For more information about how to install Terraform by using a method other than Cloud Shell, see Install and configure Terraform in the local PC.

  • Your account information is configured.

    Run the following commands to create environment variables to store identity authentication information.

    • Linux environment

      export ALICLOUD_ACCESS_KEY="************"   # Replace the value with the AccessKey ID of your Alibaba Cloud account. 
      export ALICLOUD_SECRET_KEY="************"   # Replace the value with the AccessKey secret of your Alibaba Cloud account. 
      export ALICLOUD_REGION="cn-beijing"         # Replace the value with the ID of the region in which your cluster resides.
    • Windows environment

      set ALICLOUD_ACCESS_KEY="************"   # Replace the value with the AccessKey ID of your Alibaba Cloud account. 
      set ALICLOUD_SECRET_KEY="************"   # Replace the value with the AccessKey secret of your Alibaba Cloud account. 
      set ALICLOUD_REGION="cn-beijing"         # Replace the value with the ID of the region in which your cluster resides.
    Note

    To improve the flexibility and security of permission management, we recommend that you create a Resource Access Management (RAM) user named Terraform. Then, create an AccessKey pair for the RAM user and grant permissions to the RAM user. For more information, see Create a RAM user and Grant permissions to RAM users.

Step 1: Activate ACK

ACK is available for commercial use. You must activate ACK before you can create ACK clusters.

  1. Create a working directory and a file named main.tf in the directory.

  2. Copy the following code to the main.tf file:

    View the content of the main.tf file

    provider "alicloud" {   
    }
    
    // Activate ACK. 
    data "alicloud_ack_service" "open" {
        enable = "On"
        type   = "propayasgo"
    }
  3. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If the following information is returned, Terraform is initialized:

    Initializing the backend...
    Initializing provider plugins...
    ...
    Terraform has been successfully initialized!
    ...
  4. Run the following command to activate ACK:

    terraform apply

    When the following information is returned, input yes and press Enter to activate the service.

    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: 0 added, 0 changed, 0 destroyed.

Step 2: Assign default roles to ACK

When you use ACK for the first time, you must assign default roles to ACK with your Alibaba Cloud account.

  1. Add the following code to the main.tf file and run the terraform apply command to check whether the roles are already assigned.

    Note

    Due to the limits of Terraform, Terraform cannot automatically check whether all required roles are assigned and automatically assign the missing roles. Therefore, you need to manually query the role information and assign the roles to ACK.

    // Check whether the roles are already assigned. 
    data "alicloud_ram_roles" "roles" {
        policy_type = "System"
    }
    
    // List the roles that are assigned to ACK. 
    output "exist_role" {
      value = data.alicloud_ram_roles.roles
    }

    The following information is returned:

    No changes. Your infrastructure matches the configuration.
    
    Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    
    Outputs:
    ...
    exist_role = {
      "id" = "1788****59"
      "ids" = tolist([
        "3009617019****1438",
        "3023233020****0278",
        "3302003419****4675",
        "3178548808****5924",
        "3371411011****5177",
        "3475619590****3519",
      ])
      "name_regex" = tostring(null)
      "names" = tolist([
        "AliyunCASDefaultRole",
        "AliyunContainerRegistryDefaultRole",
        "AliyunCSDefaultRole",
        "AliyunCSKubernetesAuditRole",
        "AliyunCSManagedArmsRole",
        "AliyunCSManagedCmsRole",
        "AliyunCSManagedCsiRole",
        "AliyunCSManagedKubernetesRole",
        "AliyunCSManagedLogRole",
        "AliyunCSManagedNetworkRole",
        "AliyunCSManagedVKRole",
        "AliyunCSServerlessKubernetesRole",
        "AliyunServiceRoleForCSB",
        "AliyunServiceRoleForECI",
        "AliyunServiceRoleForGws",
        "AliyunServiceRoleForResourceDirectory",
        "AliyunServiceRoleForServiceMesh",
      ])
      "output_file" = tostring(null)
      "policy_name" = tostring(null)
      "policy_type" = "System"
      "roles" = tolist([
        {
          "arn" = "acs:ram::1848450434088535:role/aliyuncasdefaultrole"
          "assume_role_policy_document" = <<-EOT
          {
              "Statement": [{
                      "Action": "sts:AssumeRole",
                      "Effect": "Allow",
                      "Principal": {"Service": ["cas.aliyuncs.com"]}}],
              "Version": "1"}
          EOT
          "create_date" = "2023-07-17T03:27:28Z"
          "description" = "Certificate Management Service assumes this role to access your resources in other Alibaba Cloud services by default."
          "document" = <<-EOT
          {
              "Statement": [{
                      "Action": "sts:AssumeRole",
                      "Effect": "Allow",
                      "Principal": {"Service": ["cas.aliyuncs.com"]}}],
              "Version": "1"}
          EOT
          "id" = "300961701980****"
          "name" = "AliyunCASDefaultRole"
          "update_date" = "2023-07-17T03:27:28Z"
        },
        {
          "arn" = "acs:ram::1848450434****:role/aliyuncontainerregistrydefaultrole"
          "assume_role_policy_document" = <<-EOT
          {
              "Statement": [{
                      "Action": "sts:AssumeRole",
                      "Effect": "Allow",
                      "Principal": {"Service": ["cr.aliyuncs.com"]}}],
              "Version": "1"}
          "id" = "3502335964487******"
          "name" = "AliyunServiceRoleForServiceMesh"
          "update_date" = "2022-09-27T10:26:50Z"
        },
      ])
    }

    If only some roles are assigned or no role is assigned, refer to the following role information and assign the roles to ACK:

    View role information

    provider "alicloud" {
    }
    
    // Create a role. 
    resource "alicloud_ram_role" "role" {
      for_each    = { for r in var.roles : r.name => r }
      name        = each.value.name
      document    = each.value.policy_document
      description = each.value.description
      force       = true
    }
    
    // Attach a RAM policy to the role. 
    resource "alicloud_ram_role_policy_attachment" "attach" {
      for_each    = { for r in var.roles : r.name => r }
      policy_name = each.value.policy_name
      policy_type = "System"
      role_name   = each.value.name
      depends_on  = [alicloud_ram_role.role]
    }
    
    // View the roles required by ACK. 
    variable "roles" {
      type = list(object({
        name            = string
        policy_document = string
        description     = string
        policy_name     = string
      }))
      default = [
        {
          name            = "AliyunCSManagedLogRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The logging component of ACK clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedLogRolePolicy"
        },
        {
          name            = "AliyunCSManagedCmsRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The CMS component of ACK clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedCmsRolePolicy"
        },
        {
          name            = "AliyunCSManagedCsiRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The volume plug-in of ACK clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedCsiRolePolicy"
        },
        {
          name            = "AliyunCSManagedVKRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The VK component of ACK Serverless clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedVKRolePolicy"
        },
        {
          name            = "AliyunCSClusterRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "ACK assumes this role to access your resources in other Alibaba Cloud services in order to run applications."
          policy_name     = "AliyunCSClusterRolePolicy"
        },
        {
          name            = "AliyunCSServerlessKubernetesRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "ACK Serverless assumes this role to access your resources in other Alibaba Cloud services by default."
          policy_name     = "AliyunCSServerlessKubernetesRolePolicy"
        },
        {
          name            = "AliyunCSKubernetesAuditRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The auditing feature of ACK assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSKubernetesAuditRolePolicy"
        },
        {
          name            = "AliyunCSManagedNetworkRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The network plug-in of ACK clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedNetworkRolePolicy"
        },
        {
          name            = "AliyunCSDefaultRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "ACK assumes this role to access your resources in other Alibaba Cloud services when managing ACK clusters by default."
          policy_name     = "AliyunCSDefaultRolePolicy"
        },
        {
          name            = "AliyunCSManagedKubernetesRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "ACK Managed assumes this role to access your resources in other Alibaba Cloud services by default."
          policy_name     = "AliyunCSManagedKubernetesRolePolicy"
        },
        {
          name            = "AliyunCSManagedArmsRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "The ARMS plug-in of ACK clusters assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCSManagedArmsRolePolicy"
        },
        {
          name            = "AliyunCISDefaultRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "Container Intelligence Service assumes this role to access your resources in other Alibaba Cloud services."
          policy_name     = "AliyunCISDefaultRolePolicy"
        },
        {
          name            = "AliyunOOSLifecycleHook4CSRole"
          policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"oos.aliyuncs.com\"]}}],\"Version\":\"1\"}"
          description     = "Operation Orchestration Service (OOS) assumes this role to access your resources in other Alibaba Cloud services. ACK relies on OOS to scale node pools."
          policy_name     = "AliyunOOSLifecycleHook4CSRolePolicy"
        }
      ]
    }
  2. Run the following command to initialize the Terraform runtime environment:

    terraform init

    If the following information is returned, Terraform is initialized.

    Initializing the backend...
    Initializing provider plugins...
    ...
    Terraform has created a lock file .terraform.lock.hcl to record the providerselections it made above. Include this file in your version control repositoryso that Terraform can guarantee to make the same selections by default whenyou run "terraform init" in the future.
    Terraform has been successfully initialized!
    ...
  3. Run the terraform apply command to assign the roles to ACK:

    When the following information is returned, input yes and press Enter to complete the authorization.

    .....
    Do you want to perform these actions?  
     Terraform will perform the actions described above.  
     Only 'yes' will be accepted to approve.  Enter a value:
  4. Run the following command to query the assigned roles:

    terraform show

    The following output indicates that all required roles are assigned:

    data "alicloud_ram_roles" "roles" {
      ...
      "names"       = [
        "AliyunCISDefaultRole",
        "AliyunCSClusterRole",
        "AliyunCSDefaultRole",
        ...
      ]
      ...
    }