简体   繁体   English

我想创建一个 devops 管道以使用 Azure 创建我的资源 Terraform

[英]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.....我在网上看了很多教程....所有教程都是在发布管道中完成的......我没有要构建和工件的 do.netcore 应用程序......我想提供我的资源没有任何应用程序构建......

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.我想知道如何在没有任何演示生成管道的情况下配置资源...我有 .tf 文件,只想创建自动创建资源的管道。

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.最少 20 个字符。

Indeed, it is possible to provision resources with Terraform from an Azure DevOps build pipeline.实际上,可以从 Azure DevOps 构建管道使用 Terraform 提供资源。 However, there are a few things to consider:但是,有几点需要考虑:

  1. You need to create a service connection for Azure Pipelines .您需要为 Azure Pipelines 创建一个服务连接 This is required to authorize Azure Pipelines to connect to your cloud provider and manage resources in your subscription.这是授权 Azure 管道连接到您的云提供商并管理您订阅中的资源所必需的。 There are service connection providers available for Azure Resource Manager (of course), but also for AWS and GCP.有可用于 Azure 资源管理器(当然)的服务连接提供程序,也可用于 AWS 和 GCP。 There is even a generic provider for general use.甚至还有一个用于一般用途的通用提供程序。

  2. Install the Terraform extension for Azure Pipelines . 为 Azure 管道安装 Terraform 扩展 Click on the shopping bag icon in the upper right corner of Azure DevOps and select Browse marketplace to find and install the extension.点击 Azure DevOps 和 select Browse marketplace右上角的购物袋图标,找到并安装扩展。

  3. Terraform always needs a backend where it can store the state of the managed infrastructure. Terraform 始终需要一个后端,它可以存储托管基础架构的 state。 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.如果您没有在 Terraform 配置中明确指定后端,它将默认使用local后端并将 state 存储在terraform.tfstate文件中。 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.然而,当 Terraform 在管道中执行时,这将不起作用,因为管道的每次运行都将在新的虚拟机上执行,并且local后端中的 state 将不会持续存在。 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.为了克服这个问题,您需要定义一个远程后端,例如 Azure 存储帐户、AWS S3 存储桶或 Google 云存储存储桶。

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.满足这些先决条件后,您可以在任务助手的帮助下在YAML 管道编辑器中从头开始创建管道,或者从下面的代码片段开始。

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:假设您的 terraform 文件位于存储库的terraform/文件夹中,管道的以下步骤将在构建代理上安装 Terraform,初始化工作目录,并计划和应用执行计划来供应资源:

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>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Terraform 创建 Azure 突触管道 - Create Azure Synapse Pipeline using Terraform Terraform 按顺序创建资源 - Terraform Create Resources In Order 我想使用 terraform 在 GCP 中创建公共和私有 Su.net - I want to create Public and Private Subnet in GCP using terraform 如何使用 Terraform 自动创建服务主体或 MSI,以在 Azure 管道中使用以管理 AKS 资源? - How do I automatically create service principals or MSIs with Terraform for use in Azure Pipelines to manage AKS resources? 使用我的用户帐户使用 terraform 在 root 帐户中创建 sns 和 cloudwatch 资源 - Using my user account to create sns and cloudwatch resources in root account using terraform 使用 Terraform 创建 Azure 策略 - Create Azure policy with Terraform Terraform 使用 for_each 和 jsondecode 创建多个资源 - Terraform create multiple resources using for_each and jsondecode 如何使用 Terraform 创建 Azure Windows 虚拟机? - How to create Azure Windows VM using Terraform? Azure DevOps 管道无法找到可执行文件:“terraform” - Azure DevOps Pipeline unable to locate executable file: 'terraform' 尽管是订阅所有者,但无法使用 terraform 创建 azura 资源 - Unable to create azura resources using terraform despite being a subscription owner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM