page_type | languages | name | description | products | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|---|
sample |
|
Using GitHub Actions OpenID Connect (OIDC) with Azure for Terraform Deployments |
A sample showing how to configure GitHub OpenID Connect (OIDC) connection to Azure with Terraform and then use that configuration to deploy resources with Terraform. |
|
github-terraform-oidc-ci-cd |
This is a two part sample. The first part demonstrates how to configure Azure and GitHub for OIDC ready for Terraform deployments. The second part demonstrates an end to end Continuous Delivery Pipeline for Terraform.
File/folder | Description |
---|---|
terraform-example-deploy |
Some Terraform with Azure Resources for the demo to deploy. |
terraform-oidc-config |
The Terraform to configure Azure and GitHub ready for OIDC authenticaton. |
.gitignore |
Define what to ignore at commit time. |
CHANGELOG.md |
List of changes to the sample. |
CONTRIBUTING.md |
Guidelines for contributing to the sample. |
README.md |
This README file. |
LICENSE.md |
The license for the sample. |
This sample includes the following features:
- Option 1: Setup 3 Azure User Assigned Managed Identities with Federation ready for GitHub OIDC.
- Option 2: Setup 3 Azure App Registrations (Service Principals) with Federation ready for GitHub OIDC.
- Setup an Azure Storage Account for State file management.
- Setup GitHub repository and environments ready to deploy Terraform with OIDC.
- Run a Continuous Delivery pipeline for Terraform using OIDC auth for state and deploying resources to Azure.
- Run a Pull Request workflow with some basic static analysis.
There are two approaches shown in the code for federating GitHub and Azure. The preferred method is to use a User Assigned Managed Identity since this does not require elevated permissions in Azure Active Directory and has a longer token timeout. However the code also shows the Service Principal approach for those that prefer that method. If you choose the Service Principal approach then the account creating the infrastructure will need permission to create Applications in Azure Active Directory.
- HashiCorp Terraform CLI: Download
- Azure CLI: Download
- An Azure Subscription: Free Account
- A GitHub Organization: Free Organization
- Clone the repository locally and then follow the Demo / Lab.
The instructions for this sample are in the form of a Lab. Follow along with them to get up and running.
- Navigate to github.com.
- Login and select the account icon in the top right and then
Settings
. - Click
Developer settings
. - Click
Personal access tokens
and selectTokens (classic)
. - Click
Generate new token
and select theclassic
option. - Type
Demo_OIDC
into theNote
field. - Check these scopes:
repo
delete_repo
- Click
Generate token
-
IMPORTANT: Copy the token and save it somewhere.
- Clone this repository to your local machine.
- Open the repo in Visual Studio Code. (Hint: In a terminal you can open Visual Studio Code by navigating to the folder and running
code .
). - Navigate to the
terraform-oidc-config
folder and create a new file calledterraform.tfvars
. - In the config file add the following:
prefix = "<your_initials>-<date_as_YYYYMMDD>"
github_organisation_target = "<your_github_organisation_name>"
e.g.
prefix = "JFH-20221208"
github_organisation_target = "my-organization"
NOTE if you wish to use the Azure Active Directory Service Principal approach rather than a User Assigned Managed Identity, then also add this setting to
terraform.tfvars
:
use_managed_identity = false
- Open the Visual Studio Code Terminal and navigate the
terraform-oidc-config
folder. - Run
az login
and follow the prompts to login to Azure with your Global Administrator account. - Run
terraform apply
. - You'll be prompted for the variable
var.github_token
. Paste in the PAT you generated earlier and hit enter. - The plan will complete. Review the plan and see what is going to be created.
- Type
yes
and hit enter once you have reviewed the plan. - Wait for the apply to complete.
NOTE: If you are a Microsoft employee you may get a 403 error here due to the way GitHub accesses the template. If so, you need to grant your PAT SSO access to any Microsoft organisation you are a member of (e.g. Azure, Microsoft, Azure-Samples, etc). This does not affect non-Microsoft users.
When deploying the example you will have selected to use the default Managed Identity approach or the Service Principal approach choose the relevant option below.
- Login to the Azure Portal with your Global Administrator account.
- Navigate to your Subscription and select
Resource groups
. - Click the resource group post-fixed
identity
(e.g.JFH-20221208-dev
). - Look for a
Managed Identity
resource post-fixed withdev
and click it. - Click on
Federated Credentials
. - There should only be one credential in the list, select that and take a look at the configuration.
- Examine the
Subject identifier
and ensure you understand how it is built up.
- Login to the Azure Portal with your Global Administrator account.
- Navigate to
Azure Active Directory
and selectApp registrations
. - Select
All applications
, then find the one you just created post-fixed withdev
(e.g.JFH-20221208-dev
). - Select
Certificate & secrets
, thenFederated credentials
. - There should only be one credential in the list, select that and take a look at the configuration.
- Examine the
Subject identifier
and ensure you understand how it is built up.
- Navigate to your Subscription and select
Resource groups
. - You should see four newly created resource groups.
- Click the resource group post-fixed
dev
(e.g.JFH-20221208-dev
). - Select
Access control (IAM)
and selectRole assignments
. - Under the
Contributor
role, you should see that yourdev
Service Principal has been granted access directly to the resource group.
- Navigate to your Subscription and select
Resource groups
. - Click the resource group post-fixed
state
(e.g.JFH-20221208-dev
). - You should see a single storage account in there, click on it.
- Select
Containers
. You should see adev
,test
andprod
container. - Select the
dev
container. - Click
Access Control (IAM)
and selectRole assignments
. - Scroll down to
Storage Blob Data Contributor
. You should see yourdev
Service Principal has been assigned that role.
- Open github.com (login if you need to).
- Navigate to your organisation and select
Repositories
. - You should see a newly created repository in there (e.g.
JFH-20221208-wild-dog
). Click on it. - You should see some files under source control.
- Navigate to
Settings
, then selectEnvironments
. - You should see 3 environments called
dev
,test
andprod
. - Click on the
dev
environment. - You should see that the environment has 7 Environment secrets. These secrets are all used in the Action for deploying Terraform.
- Navigate to
Code
. - Select
.github
,workflows
and open themain.yml
file. - Examine the file and ensure you understand all the steps in there.
- Select
Actions
, then click on theRun Terraform with OpenID Connect
action in the left menu. - Click the
Run workflow
drop-down and hit theRun workflow
button. - Wait for the run to appear or refresh the screen, then click on the run to see the details.
- You will see each environment being deployed one after the other. In a real world scenarios you may want to have a manual intervention on the environment for an approval to promote to the next stage.
- You will also note that the
Analyse the Terraform
step was skipped. - Drill into the log for one of the environments and look at the
Terraform Apply
step. You should see the output of the plan and apply. - Run the workflow again and take a look at the log to compare what happens on the Day 2 run.
- Clone your new repository and open it in Visual Studio Code.
- Create a new branch, call it whatever you want.
- Open the
terraform-example-deploy/virtual-machine.tf
file. - Rename the virtual machine to
example-machine-pr
. - Commit and push the change.
- Raise a pull request.
- You'll see the GitHub Action running in the pull request.
- The
Terraform Fmt
step will fail formain.tf
. Fix it, commit and push your change. - Wait for the Action to run again.
- Look for the Pull Request comment that was added for the plan. Expand the
Show Plan
section and review. - Merge the Pull Request.
- Navigate to
Actions
and watch the run.