In this project, we'll learn how to use Packer to build image and use Terraform to provision Azure resources
Before you start the project make sure you clone the starter code at this repo
- Create an Azure account
- Install Azure command line interface
- Install Packer
- Install Terraform
Diving into the project here is how you can use Packer and Terraform.
- Packer:
az login
to login to your Azure account and get credentialaz ad sp create-for-rbac --role Contributor --scopes /subscriptions/your-subcription-id --query "{ client_id: appId, client_secret: password }"
to create Role for Packer- Paste your previous step's output to create environment variables
New-Item -Path Env:\ARM_CLIENT_ID -Value 'client-id'
New-Item -Path Env:\ARM_CLIENT_SECRET -Value 'client-secret'
New-Item -Path Env:\ARM_SUBSCRIPTION_ID -Value 'subcription-id'
- Edit server.json file.
- Run
packer build .\server.json
in the terminal to build the image you config in server.json
- Terraform:
- Please follow thistutorial to store remote state in Azure Storage Account and get temporary credential
- You need to import resource group first
terraform import azurerm_resource_group.demorg /subscriptions/<resource-group-id>/resourceGroups/<resource-group-name>
inorder to use the resource group you created from previous step - You can custom the resource just by changing the variable in
variables.tf
file - Running
terraform init
to init the module thenterraform plan -out solution.plan
to preview the change Terraform will make - Running
terraform apply
to apply the plan
Output
- The image we created by Packer
- The infrastructure provisioned by Terraform