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.
NoteYou must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.
By default, Cloud Shell is preinstalled with Terraform and configured with your account information. You do not need to modify the configurations.
If you do not use Cloud Shell, you can directly install Terraform. For more information, see Install and configure Terraform in the local PC.
Your account information is configured.
You can specify identity information in environment variables.
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.
NoteTo 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 an ACK cluster.
Run the following command to activate ACK:
provider "alicloud" { } // Activate ACK. data "alicloud_ack_service" "open" { enable = "On" type = "propayasgo" }
Run the following command to initialize the Terraform environment:
terraform init
Expected output:
Initializing the backend... Initializing provider plugins... ... Terraform has been successfully initialized! ...
The output shows that Terraform is initialized.
Run the following command to create an ACK cluster:
terraform apply
Expected output:
... 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 ... alicloud_cs_managed_kubernetes.flannel: Creation complete after 8m26s [id=************] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
The output shows that the services are activated.
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.
Run the following command to check whether default roles are assigned to ACK with your Alibaba Cloud account:
NoteTerraform cannot automatically detect whether default roles are assigned to ACK or automatically assign default roles to ACK. You need to manually check the roles that are assigned to ACK and manually assign the required roles to ACK.
provider "alicloud" { } // Check whether default roles are assigned to ACK. data "alicloud_ram_roles" "roles" { policy_type = "System" } // Check the output. output "exist_role" { value = data.alicloud_ram_roles.roles }
If some default roles are already assigned to ACK with your Alibaba Cloud account, the assigned roles are displayed in the output. You need to manually assign roles that are not assigned but are required by ACK.
If no roles are assigned to ACK, you must assign all of the preceding roles to ACK with your Alibaba Cloud account.
Run the following command to initialize the Terraform environment:
terraform init
Expected output:
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! ...
The output shows that Terraform is initialized.
Run the following command to assign default roles to ACK:
terraform apply
Expected output:
..... Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
Enter
yes
and press the Enter key to complete the authorization.Run the following command to query the roles that are assigned to ACK:
terraform show
Expected output:
data "alicloud_ram_roles" "roles" { ... "names" = [ "AliyunContainerRegistryDefaultRole", "AliyunECIContainerGroupRole", ... ] ... }
If all roles required by ACK are displayed in the output, it indicates that the roles are assigned to ACK.