/terraform-with-jenkins-samples

Terraform plans & commands to provision Azure VMSS and VM from a VM image on demand or from a Jenkins pipeline.

Primary LanguageHCLMIT LicenseMIT

Provision & Deprovision Azure VMSS using Terraform and a Jenkins pipeline

Terraform enables you to safely and predictably provision, change, and improve infrastructure using plans and commands. This repo contains Terraform plans & commands for provisioning an Azure VMSS from an image using a Jenkins pipeline.

image

Requirements

The Jenkins pipelines in this repo leverage Kubernetes features such as pods and secrets while providing an elastic slave pool (each build runs in new pods).

To use Terraform it is required to have Azure Service Principal.
Create an Azure Service Principal through Azure CLI or Azure portal.

  1. Install Docker on your computer.
  2. Install Kubernetes
  3. Install Jenkins in Kubernetes with Helm and configure Tiller RBAC
  4. Provision a container registry such as Azure Container Registry or create an account with DockerHub.

Contents

  1. Build Docker image with Azure CLI and Terraform. This image will be used later in Jenkins pipeline deployed in Kubernetes
  2. Deployment
    • Provision Azure VM from Azure Marketplace plan.
    • Provision a VM image plan.
    • Provision VM or VMSS from a VM image.

1. Create Docker image with Azure CLI and Terraform. Dockerfile

  • Build

    docker build -t <containerRegistry>/terraform-az terraform-az
    
  • Push the Docker image to Azure Container Registry

    docker push <containerRegistry>/terraform-az
    

2. Deployment - Jenkins Pipeline in Kubernetes

  • Provision VMSS from a VM image.

    1. Create Kubernetes secret with Azure Service principal. This is required for the following pipelines.

    2. Setup Azure Storage in Jenkins and modify the pipelines accordingly with the relevant id, credentials and container name.

    3. Jenkins Pipeline (Provision) Jenkinsfile

    4. Jenkins Pipeline (Deprovision) Jenkinsfile

    The VMSS plan and tfstate are saved in Azure Blob Storage under plans and tfstate storage containers to support better automation

You can follow the detailed installation guide here

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.