'How to configure CircleCI IaC (Terraform) pipeline to use multiple repositories?

I am trying to run Terraform code through a CircleCI IaC pipeline to provision an S3 bucket in AWS.

  • I have Terraform code to provision S3 bucket s3.tf inside a repo named terraform
  • I have runtime variables in an s3.tfvars file in a repo named tfvars

So I would like to do these steps in my IaC pipeline:

  1. Clone terraform repo
  2. Clone tfvars repo
  3. Run terraform init
  4. Run terraform plan
  5. Run terraform apply

I have a config.yaml that looks like this below. I am not sure how to clone 2 repos in CircleCI pipeline (terraform and tfvars). Any pointers on how to do this?

version: '2.1'
parameters:
  ENV:
    type: string
    default: ""
orbs:
  terraform: 'circleci/[email protected]'
workflows:
  deploy_infrastructure:
     jobs:
     - terraform/init:
          path: .
     -  terraform/validate:
          path: .
          checkout: true
          context: terraform
     -  terraform/plan:
           path: .
           checkout: true
           context: terraform
           persist-workspace: true
           requires:
            - terraform/validate
           workspace: parameters.ENV
     - terraform/apply:
          attach-workspace: true
          context: terraform
          filters:
            branches:
              only: 'circleci-project-setup'
          requires:
            - terraform/plan


Solution 1:[1]

This solved the issue:

version: '2.1'
orbs:
  terraform: 'circleci/[email protected]'
jobs:
  single-job-lifecycle:
    executor: terraform/default
    steps:
      - checkout
      - run:
          command: >-
                GIT_SSH_COMMAND='ssh -vv -i ~/.ssh/id_rsa'
                git clone https://<url>/Tfvars.git
          name: GIT Clone TFvars repository
      - terraform/init:
          path: .
      - terraform/validate:
          path: .
      - run:
          name: "terraform plan"
          command: terraform plan -var-file "./Tfvars/tfvars/dev/s3.tfvars"
      - run:
          name: "terraform apply"
          command: terraform apply -auto-approve -var-file "./Tfvars/tfvars/dev/s3.tfvars"
    working_directory: ~/src
workflows:
  single-job-lifecycle:
    jobs:
      - single-job-lifecycle````

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Biju