🐶
Terraform

Terraform: Grant AKS Access to ACR

By Ondřej Dolanský on 12/31/2024

Learn how to securely grant access permissions for your Azure Kubernetes Service (AKS) cluster to interact with Azure Container Registry (ACR) using Terraform for seamless deployments.

Terraform: Grant AKS Access to ACR

Table of Contents

Introduction

When working with Azure Kubernetes Service (AKS) and Azure Container Registry (ACR), you need to establish proper authentication and authorization for your AKS cluster to access and pull images from your ACR. This typically involves granting the 'AcrPull' role to your AKS cluster's managed identity within the scope of your ACR. Terraform provides a streamlined way to manage this configuration.

Step-by-Step Guide

To grant your Azure Kubernetes Service (AKS) cluster access to pull images from your Azure Container Registry (ACR), you'll need to assign the 'AcrPull' role to the AKS cluster's managed identity within the ACR's scope. Here's how you can achieve this using Terraform:

  1. Retrieve the AKS Cluster's Identity:

    data "azurerm_kubernetes_cluster" "aks" {
      name                = "your-aks-cluster-name"
      resource_group_name = "your-resource-group-name"
    }
  2. Define the Role Assignment:

    resource "azurerm_role_assignment" "acr_pull" {
      scope                = data.azurerm_container_registry.acr.id
      role_definition_name = "AcrPull"
      principal_id         = data.azurerm_kubernetes_cluster.aks.identity[0].principal_id
    }

In this code:

  • data.azurerm_kubernetes_cluster.aks.identity[0].principal_id fetches the principal ID of your AKS cluster, which is used to authenticate access to other Azure resources.
  • data.azurerm_container_registry.acr.id (not shown in the example) should be replaced with the actual resource ID of your Azure Container Registry.
  • "AcrPull" is the built-in role that grants read-only access to the container registry.

This Terraform configuration ensures that your AKS cluster has the necessary permissions to pull images from your ACR, allowing you to deploy and manage your containerized applications seamlessly.

Code Example

This Terraform code configures access for an Azure Kubernetes Cluster (AKS) to pull images from an Azure Container Registry (ACR). It defines the Azure provider, retrieves information about the existing ACR and AKS cluster, and then assigns the "AcrPull" role to the AKS cluster's managed identity. This role assignment grants the AKS cluster read-only access to the specified ACR, allowing it to pull images for deployments.

# Configure Azure Provider
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "~> 3.0" # Use the latest version compatible with your environment
    }
  }
}

# Configure Azure Provider
provider "azurerm" {
  features {}
}

# Data Source for Azure Container Registry (ACR)
data "azurerm_container_registry" "acr" {
  name                = "your-acr-name"
  resource_group_name = "your-resource-group-name"
}

# Data Source for Azure Kubernetes Cluster (AKS)
data "azurerm_kubernetes_cluster" "aks" {
  name                = "your-aks-cluster-name"
  resource_group_name = "your-resource-group-name"
}

# Role Assignment for ACR Pull Access
resource "azurerm_role_assignment" "acr_pull" {
  scope                = data.azurerm_container_registry.acr.id
  role_definition_name = "AcrPull"
  principal_id         = data.azurerm_kubernetes_cluster.aks.identity[0].principal_id
}

Explanation:

  1. Provider Configuration: The code starts by configuring the Azure provider, specifying the source and version.
  2. Data Source for ACR: The data "azurerm_container_registry" "acr" block retrieves information about your existing Azure Container Registry. Replace "your-acr-name" and "your-resource-group-name" with the actual values.
  3. Data Source for AKS: The data "azurerm_kubernetes_cluster" "aks" block fetches details about your Azure Kubernetes Cluster. Update "your-aks-cluster-name" and "your-resource-group-name" accordingly.
  4. Role Assignment: The resource "azurerm_role_assignment" "acr_pull" block defines the role assignment:
    • scope: Specifies the resource (ACR) where the role is assigned.
    • role_definition_name: Sets the role to "AcrPull," granting read-only access to the ACR.
    • principal_id: Uses the principal ID of your AKS cluster's managed identity, retrieved from the data.azurerm_kubernetes_cluster.aks data source.

How to Use:

  1. Replace Placeholders: Update the placeholders for ACR name, AKS cluster name, and resource group name with your actual values.
  2. Terraform Init and Apply: Run terraform init to initialize Terraform, then terraform apply to create the role assignment.

This configuration ensures that your AKS cluster can authenticate with Azure Container Registry and pull images, enabling you to deploy your containerized applications seamlessly.

Additional Notes

Security and Best Practices:

  • Principle of Least Privilege: Instead of granting access to the entire ACR, consider creating dedicated repositories within your ACR for different applications or environments. Assign the AcrPull role at the repository level to limit the scope of access for each AKS cluster.
  • Managed Identities: Always prefer using managed identities for authentication between Azure services like AKS and ACR. This eliminates the need to manage secrets and provides a more secure approach.
  • Role-Based Access Control (RBAC): Leverage Azure RBAC to control access to your Azure resources. Define clear roles and responsibilities for your team members and grant permissions accordingly.
  • Regular Audits: Regularly audit your role assignments and resource access to ensure security and compliance.

Troubleshooting:

  • Permissions Errors: If your AKS cluster encounters permission errors when pulling images, double-check the role assignment, the principal ID used, and the scope of the role (ACR or specific repository).
  • Terraform State: Ensure your Terraform state is up-to-date. Run terraform plan and terraform apply to reconcile any configuration drift.
  • Azure CLI/Portal Verification: You can verify the role assignment using the Azure CLI or the Azure portal. Look for the AcrPull role assigned to your AKS cluster's managed identity within the ACR's Access Control (IAM) settings.

Additional Considerations:

  • Network Connectivity: Ensure that your AKS cluster has network connectivity to your ACR. This might involve configuring virtual networks, peering, or private endpoints.
  • Image Pull Secrets: While this example focuses on role-based access control, you can also use image pull secrets to authenticate with your ACR. This approach is common when using individual user accounts or service principals for pulling images.
  • Alternative Authentication Methods: Azure offers various authentication methods for ACR, including service principals and managed identities. Choose the method that best suits your security and management requirements.

Further Learning:

Summary

This article provides a concise Terraform configuration to enable an Azure Kubernetes Service (AKS) cluster to pull images from an Azure Container Registry (ACR).

Key Steps:

  1. Retrieve AKS Cluster Identity: The provided Terraform code snippet fetches the principal ID of the AKS cluster, which is crucial for authentication with other Azure resources.

  2. Assign "AcrPull" Role: The code defines a role assignment, granting the AKS cluster's managed identity the "AcrPull" role within the ACR's scope. This role grants read-only access to the container registry, allowing the AKS cluster to pull images.

Important Notes:

  • Replace placeholders like "your-aks-cluster-name", "your-resource-group-name", and data.azurerm_container_registry.acr.id with your actual resource names and IDs.
  • The "AcrPull" role is a built-in role specifically designed for granting image pulling permissions.

By implementing this Terraform configuration, you ensure secure and seamless image pulling from your ACR, simplifying containerized application deployment and management within your AKS cluster.

Conclusion

By leveraging Terraform to manage the integration between AKS and ACR, you can streamline the deployment process and enhance the security of your containerized applications. The provided code snippet and explanations offer a clear path to set up the necessary permissions, ensuring your AKS cluster can seamlessly pull images from your ACR. Remember to replace the placeholders with your specific resource details and consider the security best practices and additional considerations discussed to tailor the configuration to your environment. By following these guidelines, you can establish a robust and secure workflow for building, deploying, and managing your containerized applications on Azure.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait