github.com/hashicorp/packer@v1.14.3/website/content/guides/packer-on-cicd/trigger-tfe.mdx (about)

     1  ---
     2  page_title: Trigger Terraform Enterprise runs
     3  description: >-
     4    Learn how to Terraform Enterprise from your CI/CD pipeline to provision infrastructure.
     5  ---
     6  
     7  # Create Terraform Enterprise Runs
     8  
     9  Once an image is built and uploaded to an artifact store, the next step is to
    10  use this new image. In some cases the image will be downloaded by the dev team
    11  and used locally in development, like is often done with VirtualBox images with
    12  Vagrant. In most other cases, the new image will be used to provision new
    13  infrastructure.
    14  
    15  You can use [Terraform](/terraform/cli) to provision new infrastructure with images generated by Packer. When working with teams, you can use [HCP Terraform](/terraform/cloud-docs) or [Terraform
    16  Enterprise](/terraform/enterprise) to automate Terraform runs. 
    17  
    18  ## Create a Terraform Configuration and Workspace
    19  
    20  The following is a sample Terraform configuration which provisions a new AWS
    21  EC2 instance. The `aws_ami_id` is a variable which will be provided when
    22  running `terraform plan` and `terraform apply`. This variable references the
    23  latest AMI generated with the Packer build in CI/CD.
    24  
    25  ```hcl
    26  variable "aws_ami_id" { }
    27  
    28  provider "aws" {
    29    region = "us-west-2"
    30  }
    31  
    32  resource "aws_instance" "web" {
    33    ami           = "${var.aws_ami_id}"
    34    instance_type = "t2.micro"
    35  }
    36  ```
    37  
    38  Terraform Enterprise should have a workspace with this terraform configuration
    39  and a placeholder variable `aws_ami_id`.
    40  
    41  ## Include Terraform Enterprise in Your CI Builds
    42  
    43  Follow these steps to create a new run from CI/CD after a Packer build is
    44  complete and uploaded.
    45  
    46  1. Add a new step to the CI/CD pipeline.
    47  2. In the new step add a `curl` call to update the variables in the workspace
    48     using the [update variables
    49     API](/terraform/docs/enterprise/api/variables#update-variables),
    50     so that Terraform has a reference to the latest image. For the sample
    51     configuration above, the `aws_ami_id` variable should be updated to the AMI
    52     ID of the latest image.
    53  3. In that same step, add another `curl` call to [create a new run via the
    54     API](/terraform/docs/enterprise/api/run#create-a-run).
    55     A run performs a plan and apply on the last configuration version created,
    56     using the variables set in the workspace. In the previous step we update the
    57     variables, so the new run can be created using the previous configuration
    58     version.