简体   繁体   中英

I want to create a devops pipeline to create my resources in Azure using Terraform

I have watched many tutorials online....and all of them do it in the release pipeline......I don't have a do.netcore app to build and artifact....I want to provision my resources without any application build.....

Can I do it in the build pipeline? Is there any tutorial out there which shows the solution to the above problem?

I want to know how to provision resources without any demo generated pipeline...I have the.tf file and just want to create the pipeline which automates creation of the resources.

What did you try and what were you expecting? Describe what you tried, what you expected to happen, and what actually resulted. Minimum 20 characters.

Indeed, it is possible to provision resources with Terraform from an Azure DevOps build pipeline. However, there are a few things to consider:

  1. You need to create a service connection for Azure Pipelines . This is required to authorize Azure Pipelines to connect to your cloud provider and manage resources in your subscription. There are service connection providers available for Azure Resource Manager (of course), but also for AWS and GCP. There is even a generic provider for general use.

  2. Install the Terraform extension for Azure Pipelines . Click on the shopping bag icon in the upper right corner of Azure DevOps and select Browse marketplace to find and install the extension.

  3. Terraform always needs a backend where it can store the state of the managed infrastructure. If you do not explicitly specify a backend in your Terraform config, it will use the local backend by default and store the state in the terraform.tfstate file. This however will not work when Terraform is executed within a pipeline as every run of the pipeline will be executed on a fresh virtual machine and the state in the local backend will not persist. To overcome this you need to define a remote backend, eg an Azure Storage Account, an AWS S3 bucket, or an Google Cloud Storage bucket.

Once these prerequisites are met, you can either create your pipeline from scratch in the YAML pipeline editor with the help of the task assistant or you start with the snippet below.

Assuming your terraform files are located in the terraform/ folder of your repository, the following steps of the pipeline will install Terraform on the build agent, initialize the work directory, and plan and apply the execution plan to provision the resources:

stages:
- stage: Infra
  displayName: Deploy Infrastructure
  jobs:  
  - job: Infra
    displayName: Deploy Infrastructure
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - task: TerraformInstaller@0
      displayName: 'Install Terraform'
      inputs:
        terraformVersion: '0.14.4'
    - task: TerraformTaskV1@0
      inputs:
        provider: 'azurerm'
        command: 'init'
        workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
        backendServiceArm: '<SERVICE_CONNECTION_NAME>'
        backendAzureRmResourceGroupName: '<RESOURCEGROUP_NAME>'
        backendAzureRmStorageAccountName: '<STORAGE_ACCOUNT_NAME>'
        backendAzureRmContainerName: '<STORAGE_CONTAINER_NAME>'
        backendAzureRmKey: '<NAME_OF_TERRAFORM_STATE_FILE>'

    - task: TerraformTaskV1@0
      displayName: 'Terraform Plan'
      inputs:
        provider: 'azurerm'
        command: 'plan'
        workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
        environmentServiceNameAzureRM: '<SERVICE_CONNECTION_NAME>'

    - task: TerraformTaskV1@0
      displayName: 'Terraform Apply'
      inputs:
        provider: 'azurerm'
        command: 'apply'
        workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'
        environmentServiceNameAzureRM: '<SERVICE_CONNECTION_NAME>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM