Skip to content

Commit 8976674

Browse files
authored
Merge pull request #16 from thomast1906/updates-sept-2024
Updates sept 2024
2 parents fbfee28 + 3e8ba97 commit 8976674

File tree

106 files changed

+1193
-1169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1193
-1169
lines changed

.github/workflows/main.yml

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: Terrform-Deploy
22

33
on:
44
push:
@@ -10,14 +10,12 @@ on:
1010
workflow_dispatch:
1111

1212
jobs:
13-
terraform-fmt-check:
14-
if: github.event_name == 'pull_request'
13+
terraform:
14+
name: Terrform-Deploy
15+
runs-on: ubuntu-latest
1516
defaults:
1617
run:
17-
working-directory: "./Terraform-AZURE-Services-Creation/AKS"
18-
name: Terraform
19-
environment: production
20-
runs-on: ubuntu-latest
18+
working-directory: "./2-Terraform-AZURE-Services-Creation/4-aks"
2119
permissions:
2220
contents: write
2321
env:
@@ -29,58 +27,38 @@ jobs:
2927
tf_storage_account_name: "thomasthorntontfstate"
3028
tf_state_container: "devopsthehardwaygithub"
3129
tf_state_key: "terraform.tfstate"
30+
3231
steps:
3332
- name: Checkout Code
3433
uses: actions/checkout@v4
3534

3635
- name: Setup Terraform
3736
uses: hashicorp/setup-terraform@v3
3837
with:
39-
terraform_version: 1.7.4
38+
terraform_version: 1.9.6
4039
terraform_wrapper: true
4140

41+
4242
- name: Terraform Init
43-
id: init
4443
run: terraform init
45-
env:
46-
ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }}
47-
ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }}
48-
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
49-
ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }}
50-
working-directory: "./Terraform-AZURE-Services-Creation/AKS"
5144

52-
- name: Terraform Fmt
53-
id: fmt
45+
- name: Terraform Format
46+
if: github.event_name == 'pull_request'
5447
run: terraform fmt
55-
working-directory: "./Terraform-AZURE-Services-Creation/AKS"
5648

5749
- name: Auto Commit Changes
5850
uses: stefanzweifel/git-auto-commit-action@v5
51+
if: github.event_name == 'pull_request'
5952
with:
6053
commit_message: "Terraform fmt"
6154
file_pattern: "*.tf *.tfvars"
6255
commit_user_name: "github-actions[bot]"
6356

64-
- name: Terraform Plan
65-
id: plan
57+
- name: Terraform Plan
6658
run: terraform plan -no-color -input=false
6759
env:
68-
ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }}
69-
ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }}
70-
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
71-
ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }}
7260
DEPLOYMENT_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
73-
working-directory: "./Terraform-AZURE-Services-Creation/AKS"
74-
continue-on-error: false
7561

76-
- name: Terraform Apply
77-
id: apply
78-
run: terraform apply -auto-approve -input=false
79-
if: github.ref == 'refs/heads/main'
80-
env:
81-
ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }}
82-
ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }}
83-
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
84-
ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }}
85-
working-directory: "./Terraform-AZURE-Services-Creation/AKS"
86-
continue-on-error: false
62+
- name: Terraform Apply
63+
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
64+
run: terraform apply -auto-approve -input=false
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Configure Storage Account for Terraform State File
2+
3+
## 🎯 Purpose
4+
In this lab, you'll create a secure location to store the remote Terraform State file. This is crucial for maintaining consistency and collaboration in your infrastructure-as-code projects.
5+
6+
## 🛠️ Create Blob Storage for Terraform State File
7+
8+
### Prerequisites
9+
- [ ] Azure CLI installed and configured
10+
- [ ] Basic understanding of Azure Storage concepts
11+
12+
### Steps
13+
14+
1. **Customise Variables**
15+
- Open the [create-terraform-storage.sh](https://github.com/thomast1906/DevOps-The-Hard-Way-Azure/blob/main/1-Azure/scripts/create-terraform-storage.sh) script.
16+
- Locate the following lines:
17+
18+
```bash
19+
RESOURCE_GROUP_NAME="devopshardway-rg"
20+
STORAGE_ACCOUNT_NAME="devopshardwaysa"
21+
```
22+
23+
- Replace the placeholders with your desired names.
24+
25+
2. **Run the Script**
26+
27+
- Run the following command in your terminal:
28+
29+
```bash
30+
./scripts/create-terraform-storage.sh
31+
```
32+
33+
3. **What's Happening Behind the Scenes?**
34+
The script performs these actions:
35+
- [ ] Creates an Azure Resource Group
36+
- [ ] Sets up an Azure Storage Account
37+
- [ ] Establishes an Azure Blob storage container
38+
39+
## 🔍 Verification
40+
To ensure everything was set up correctly:
41+
42+
1. Log into the [Azure Portal](https://portal.azure.com).
43+
2. Navigate to your newly created Resource Group.
44+
3. Verify the presence of the Storage Account.
45+
4. Within the Storage Account, check for the Blob container.
46+
5. It should look similar to this:
47+
48+
![](images/storage-account.png)
49+
50+
## 🧠 Knowledge Check
51+
After running the script, try to answer these questions:
52+
1. Why is it important to use remote state storage for Terraform?
53+
2. What are the benefits of using Azure Blob Storage for this purpose?
54+
3. How would you access this state file in your Terraform configurations?
55+
56+
## 💡 Pro Tip
57+
Consider setting up access policies and encryption for your storage account to enhance security. Azure provides several options for this, including Azure AD authentication and Azure Key Vault integration.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Create Azure AD Group for AKS Admins
2+
3+
## 🎯 Purpose
4+
In this lab, you'll create an Azure AD Group for AKS Admins. These "admins" will be the designated users who can access the AKS cluster using kubectl.
5+
6+
## 🛠️ Create Azure AD AKS Admin Group
7+
8+
### Prerequisites
9+
- [ ] Sufficient permissions to create Azure AD groups
10+
11+
12+
### Steps
13+
14+
1. **Run the Script**
15+
Execute the following command in your terminal:
16+
```bash
17+
./scripts/create-azure-ad-group.sh
18+
```
19+
2. What the Script Does
20+
21+
The script performs these actions:
22+
- [ ] Creates an Azure AD Group named `devopsthehardway-aks-group`
23+
- [ ] Adds the current user (logged into Az CLI) to the `devopsthehardway-aks-group`
24+
- [ ] Outputs the Azure AD Group ID
25+
26+
**Important Note**
27+
Make sure to note down the Azure AD Group ID displayed at the end of the script execution. You'll need this for AKS Terraform configurations later.
28+
29+
## 🔍 Verification
30+
To ensure the group was created successfully:
31+
1. Log into the [Azure Portal](https://portal.azure.com)
32+
2. Navigate to **Azure Active Directory > Groups**
33+
3. Search for `devopsthehardway-aks-group`
34+
4. Verify that your user account is listed as a member:
35+
36+
![](images/azure-ad-group.png)
37+
38+
## 🧠 Knowledge Check
39+
After running the script, consider these questions:
40+
1. Why is it beneficial to use Azure AD groups for AKS admin access?
41+
2. How does this group-based access improve security compared to individual user access?
42+
3. In what ways might you further modify the AD group for different levels of access?
43+
44+
## 💡 Pro Tip
45+
Consider setting up multiple AD groups with different levels of access (e.g., read-only, developer, admin) to implement a more granular access control strategy for your AKS clusters.

1-Azure/images/azure-ad-group.png

64.8 KB
Loading

1-Azure/images/storage-account.png

44 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
AZURE_AD_GROUP_NAME="devopsthehardway-aks-group"
4+
CURRENT_USER_OBJECTID=$(az ad signed-in-user show --query id -o tsv)
5+
6+
# Check if Azure AD Group exists
7+
GROUP_EXISTS=$(az ad group list --filter "displayName eq '$AZURE_AD_GROUP_NAME'" --query "[].displayName" -o tsv)
8+
9+
if [ "$GROUP_EXISTS" = "$AZURE_AD_GROUP_NAME" ]; then
10+
echo "Azure AD group $AZURE_AD_GROUP_NAME already exists."
11+
else
12+
# Create Azure AD Group
13+
az ad group create --display-name $AZURE_AD_GROUP_NAME --mail-nickname $AZURE_AD_GROUP_NAME
14+
fi
15+
16+
# Check if Current User is already a member of the Azure AD Group
17+
USER_IN_GROUP=$(az ad group member check --group $AZURE_AD_GROUP_NAME --member-id $CURRENT_USER_OBJECTID --query value -o tsv)
18+
19+
if [ "$USER_IN_GROUP" = "true" ]; then
20+
echo "Current user is already a member of the Azure AD group $AZURE_AD_GROUP_NAME."
21+
else
22+
# Add Current az login user to Azure AD Group
23+
az ad group member add --group $AZURE_AD_GROUP_NAME --member-id $CURRENT_USER_OBJECTID
24+
fi
25+
26+
AZURE_GROUP_ID=$(az ad group show --group $AZURE_AD_GROUP_NAME --query id -o tsv)
27+
28+
echo "AZURE AD GROUP ID IS: $AZURE_GROUP_ID"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/sh
2+
3+
RESOURCE_GROUP_NAME="devopshardway-rg"
4+
STORAGE_ACCOUNT_NAME="devopshardwaysa"
5+
6+
# Check if Resource Group exists
7+
RESOURCE_GROUP_EXISTS=$(az group exists --name $RESOURCE_GROUP_NAME)
8+
9+
if [ "$RESOURCE_GROUP_EXISTS" = "true" ]; then
10+
echo "Resource group $RESOURCE_GROUP_NAME already exists."
11+
else
12+
# Create Resource Group
13+
az group create -l uksouth -n $RESOURCE_GROUP_NAME
14+
fi
15+
16+
# Check if Storage Account exists
17+
STORAGE_ACCOUNT_EXISTS=$(az storage account check-name --name $STORAGE_ACCOUNT_NAME --query 'nameAvailable' --output tsv)
18+
19+
if [ "$STORAGE_ACCOUNT_EXISTS" = "false" ]; then
20+
echo "Storage account $STORAGE_ACCOUNT_NAME is already created in resource group $RESOURCE_GROUP_NAME."
21+
else
22+
# Create Storage Account
23+
az storage account create -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP_NAME -l uksouth --sku Standard_LRS
24+
25+
# Create Storage Account blob
26+
az storage container create --name tfstate --account-name $STORAGE_ACCOUNT_NAME
27+
fi
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Create an Azure Container Registry Repository
2+
3+
## 🎯 Purpose
4+
In this lab, you'll create a repository in Azure Container Registry (ACR) to store the Docker image for the thomasthornton.cloud app.
5+
6+
## 🛠️ Create the ACR Terraform Configuration
7+
8+
### Prerequisites
9+
- [ ] Terraform installed
10+
- [ ] Basic understanding of Terraform and ACR concepts
11+
12+
## Steps
13+
14+
1. **Review and Change Terraform .tfvars**
15+
- Open the [terraform.tfvars](https://github.com/thomast1906/DevOps-The-Hard-Way-Azure/tree/main/2-Terraform-AZURE-Services-Creation/1-acr/terraform.tfvars) file.
16+
- Ensure all values are accurate for your environment and unique.
17+
18+
2. **Understand the Terraform Configuration**
19+
Review the [ACR Terraform configuration](https://github.com/thomast1906/DevOps-The-Hard-Way-Azure/tree/main/2-Terraform-AZURE-Services-Creation/1-acr). The `acr.tf` file will:
20+
- [ ] Use a Terraform backend to store the `.tfstate` in an Azure Storage Account
21+
- [ ] Use the `uksouth` region (can change if desired)
22+
- [ ] Create a new Resource Group using `azurerm_resource_group`
23+
- [ ] Create a new ACR using `azurerm_container_registry`
24+
25+
3. **Create the ACR**
26+
Run the following commands in your terminal:
27+
```bash
28+
terraform init
29+
terraform plan
30+
terraform apply
31+
32+
## 🔍 Verification
33+
To ensure the ACR was created successfully:
34+
1. Log into the [Azure Portal](https://portal.azure.com)
35+
2. Navigate to ACR in the [Azure Portal](https://portal.azure.com/#browse/Microsoft.ContainerRegistry%2Fregistries)
36+
3. Look for your newly created ACR
37+
4. Verify its properties match your Terraform configuration
38+
39+
Example screenshot of the Terraform apply command:
40+
41+
![](images/acr.png)
42+
43+
## 🧠 Knowledge Check
44+
After creating the ACR, consider these questions:
45+
46+
1. Why is it beneficial to use Terraform for creating cloud resources like ACR?
47+
2. How does storing the Terraform state in Azure Storage Account help in team environments?
48+
3. What are the advantages of using ACR over other container registry options?
49+
50+
## 💡 Pro Tip
51+
Consider setting up replication for your ACR to improve pull performance in different regions. You can add this to your Terraform configuration for automated setup.

Terraform-AZURE-Services-Creation/ACR/acr.tf renamed to 2-Terraform-AZURE-Services-Creation/1-acr/acr.tf

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
terraform {
2-
required_version = ">= 1.5.7"
3-
backend "azurerm" {
4-
resource_group_name = "devopshardway-rg"
5-
storage_account_name = "devopshardwaysa"
6-
container_name = "tfstate"
7-
key = "acr-terraform.tfstate"
8-
}
9-
}
10-
11-
provider "azurerm" {
12-
features {}
13-
}
14-
151
resource "azurerm_resource_group" "acr_resource_group" {
162
name = "${var.name}-rg"
173
location = var.location
@@ -21,7 +7,7 @@ resource "azurerm_resource_group" "acr_resource_group" {
217
}
228

239
resource "azurerm_container_registry" "acr" {
24-
name = "${var.name}tamopsacracr"
10+
name = "${var.name}azurecr"
2511
resource_group_name = azurerm_resource_group.acr_resource_group.name
2612
location = azurerm_resource_group.acr_resource_group.location
2713
sku = "Standard"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
terraform {
2+
required_version = ">= 1.9.6"
3+
backend "azurerm" {
4+
resource_group_name = "devopshardway-rg"
5+
storage_account_name = "devopshardwaysa"
6+
container_name = "tfstate"
7+
key = "acr-terraform.tfstate"
8+
}
9+
10+
required_providers {
11+
azurerm = {
12+
source = "hashicorp/azurerm"
13+
version = ">= 4.3.0"
14+
}
15+
}
16+
}
17+
18+
provider "azurerm" {
19+
features {}
20+
subscription_id = "04109105-f3ca-44ac-a3a7-66b4936112c3"
21+
22+
}

0 commit comments

Comments
 (0)