Objective
Automate the deployment of Azure Kubernetes Service (AKS) using Terraform Cloud Workspaces. This ensures Infrastructure as Code (IaC) is centrally managed, and all changes are traceable.
1. Set Up a Terraform Cloud Workspace Free Account
Step 1: Create a Terraform Cloud Account
- Navigate to https://developer.hashicorp.com/sign-up.
- Sign up or log in with a valid email ID.
Step 2: Create an Organization
- After logging in, create an organization (if one doesn’t already exist):
a. Go to Organizations > Create Organization.
b. Provide a name for the organization.
Step 3: Create a Workspace
- Navigate to Workspaces:
a. Go to your organization dashboard.
b. Click on Workspaces > Create a New Workspace.
2. Select a Workflow: Terraform Cloud offers three workflows:
a. CLI-Driven Workflow: Ideal for local Terraform runs but stores state in Terraform Cloud.
2. Download and Install Azure CLI and Terraform
Step 1: Install Azure CLI
- Visit https://learn.microsoft.com/en-us/cli/azure/install-azure-cli.
- Download and install the CLI on your system.
Step 2: Install Terraform
- Go to https://releases.hashicorp.com/terraform/.
- Download the Terraform version that matches your system configuration.
3. Integration/Login from Local to Terraform Cloud
Step 1: Set Up Terraform Manifest Files
- Create a working directory.
- Within it, set up the root folder for the main module and child modules with provider configurations.
Step 2: Authenticate with Terraform Cloud
- Open the terminal in VS Code and run: “terraform login”
- Approve the request by entering yes.
3. Generate and paste the API token:
a. Navigate to Account Settings > Tokens > Create API Token.
b. Copy the generated token (visible only once).
c. Paste the token into the terminal.
Step 3: Initialize Terraform
- Run the
terraform init
command to download Azure provider plugins. - While initializing, select the appropriate workspace name.
- Verify successful initialization with the message:
“Terraform has been successfully initialized.”
4. Add Variables to Terraform Cloud Workspace
- Select your workspace and navigate to the Variables section.
- Add the required Azure variables:
- Client ID
- Client Secret
- Subscription ID
- Tenant ID
5. Develop Terraform Code
Step 1: Resource Group, VNet, and Subnet
Write Terraform scripts for creating:
- Resource Group (RG).
- Virtual Network (VNet).
- Subnet.
Step 2: Azure Kubernetes Service (AKS)
Write Terraform scripts for provisioning AKS.
Step 3: Test and Validate
Ensure the Terraform code is correct by running terraform plan
.
6. Deploy Resources to Azure Cloud
Deployment Steps
- Run
terraform init
to initialize plugins and modules. and select the workspace name.
2. Run terraform plan
to preview the deployment.
3.Run terraform apply
to deploy the resources based on the plan output.
Post-Deployment
- Check the Run Logs in the Terraform Cloud Workspace for detailed insights.
- Verify the resources in the Azure Portal.
Source: Read MoreÂ