skip to Main Content

I have this GitHub repo:

_
 |_ .github/workflows/myworkflow.yaml
 |_ terraform/terraformInFolder.tf
 |_ terraformInRootDir.tf

my workflow runs terraform bash scripts like terraform apply, vaidate, etc…
it can only run the .tf files in the root directory. I couldn’t find a way to run the files in the folder.
I tried to use with: path: but with cannot be used with run.

Here is my workflow:

name: deployment-workflow

on: push
jobs:
  terraform_apply:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1


    - name: Verify Terraform version
      run: terraform --version

    - name: Terraform init
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: terraform init -input=false

    - name: Terraform validation
      run: terraform validate

    - name: Terraform apply
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      run: terraform apply -auto-approve -input=false 

How can i include the path to my .tf files in the folder?

2

Answers


  1. You could use the parameter

    -chdir=< folder location >

    to redirect the execution of terraform. I update your code to this:

    name: deployment-workflow
    
    on: push
    jobs:
      terraform_apply:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v1
    
    
        - name: Verify Terraform version
          run: terraform --version
    
        - name: Terraform init
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          run: terraform -chdir="./terraform" init -input=false
    
        - name: Terraform validation
          run: terraform -chdir="./terraform" validate -no-color
    
        - name: Terraform apply
          env:
            AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
            AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          run: terraform -chdir="./terraform" apply -auto-approve -input=false 
    
    Login or Signup to reply.
  2. Terraform doesn’t expect you to break up your modules using directories like this. All files in a module are loose in the top level of the module’s folder. In the tree you provided, your root module contains only a single file: terraformInRootDir.tf. The subdir is ignored.

    If you want to add structure, you can do so by creating submodules. The files of a submodule are in a subdirectory, but you have to declare the submodule in code by specifying the path and passing the input variables.

    You can learn more about how Hashicorp recommends you structure your repo in the docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search