Giter VIP home page Giter VIP logo

Comments (6)

dflook avatar dflook commented on June 1, 2024

Hello @mcassaniti, can you share the workflow yaml you are using and the workflow log output? What is it you expect to happen that is not working?

The validate command is only doing static checking of the module, I wouldn't expect the terraform.workspace to be evaluated or the provider actually initialised. The snippet you provided seems to validate for me.

from terraform-github-actions.

mcassaniti avatar mcassaniti commented on June 1, 2024

Thank you for getting back to me so soon.

Workflow code.

---
  name: agent-monitoring
  on:
    pull_request:
      paths:
        - agent-monitoring/**

  jobs:
    plan:
      name: Test agent-monitoring
      runs-on: ubuntu-latest

      steps:
        - name: Clone repository
          uses: actions/checkout@v2

        - name: Create Hashicorp vault password file
          run: echo ${{ secrets.HASHICORP_VAULT_ANSIBLE_PASSWORD  }} > .vault_password

        - name: Configure AWS Credentials
          uses: aws-actions/configure-aws-credentials@v1
          with:
            aws-access-key-id: ${{ secrets.TERRAFORM_AWS_ACCESS_KEY }}
            aws-secret-access-key: ${{ secrets.TERRAFORM_AWS_SECRET_KEY }}
            aws-region: ap-southeast-2

        - name: Create Terraform workspace
          uses: dflook/terraform-new-workspace@v1
          with:
            path: agent-monitoring
            workspace: prod

        - name: Validate Terraform
          uses: dflook/terraform-validate@v1
          with:
            path: agent-monitoring

        - name: Plan Terraform
          uses: dflook/terraform-plan@v1
          with:
            path: agent-monitoring
            workspace: prod
            add_github_comment: false

From the workflow run using the YAML above, create terraform workspace step.

Initializing modules...
  - lambda_python_package in ../_common-modules/lambda-python-archive
  
  Initializing the backend...
  
  Successfully configured the backend "s3"! Terraform will automatically
  use this backend unless the backend configuration changes.
  
  Initializing provider plugins...
  - Reusing previous version of hashicorp/aws from the dependency lock file
  - Reusing previous version of hashicorp/vault from the dependency lock file
  - Reusing previous version of hashicorp/external from the dependency lock file
  - Reusing previous version of hashicorp/random from the dependency lock file
  - Installing hashicorp/aws v3.66.0...
  - Installed hashicorp/aws v3.66.0 (signed by HashiCorp)
  - Installing hashicorp/vault v2.24.1...
  - Installed hashicorp/vault v2.24.1 (signed by HashiCorp)
  - Installing hashicorp/external v2.1.0...
  - Installed hashicorp/external v2.1.0 (signed by HashiCorp)
  - Installing hashicorp/random v3.1.0...
  - Installed hashicorp/random v3.1.0 (signed by HashiCorp)
  
  Terraform has been successfully initialized!
Workspace appears to exist, selecting it
Switched to workspace "prod".

From the workflow run using the YAML above, validate terraform step.

 Initializing modules...
  - lambda_python_package in ../_common-modules/lambda-python-archive
  
  Initializing provider plugins...
  - Reusing previous version of hashicorp/aws from the dependency lock file
  - Reusing previous version of hashicorp/vault from the dependency lock file
  - Reusing previous version of hashicorp/external from the dependency lock file
  - Reusing previous version of hashicorp/random from the dependency lock file
  - Using hashicorp/random v3.1.0 from the shared cache directory
  - Using hashicorp/aws v3.66.0 from the shared cache directory
  - Using hashicorp/vault v2.24.1 from the shared cache directory
  - Using hashicorp/external v2.1.0 from the shared cache directory
  
  Terraform has been successfully initialized!
Error: Invalid index
Error: Invalid index
╷
│ Error: Invalid index
│ 
│   on providers.tf line 41, in provider "aws":
│   41:   region              = local.aws_provider_config[terraform.workspace].region
│     ├────────────────
│     │ local.aws_provider_config is object with 2 attributes
│     │ terraform.workspace is "default"
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on providers.tf line 42, in provider "aws":
│   42:   allowed_account_ids = [local.aws_provider_config[terraform.workspace].account_id]
│     ├────────────────
│     │ local.aws_provider_config is object with 2 attributes
│     │ terraform.workspace is "default"
│ 
│ The given key does not identify an element in this collection value.
╵

Output when run in a fresh directory after a clone and setup of credentials.

mcassaniti@wks-mcass-linux:~/test-this/agent-monitoring$ terraform init
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/external from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Reusing previous version of hashicorp/vault from the dependency lock file
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
- Installing hashicorp/external v2.1.0...
- Installed hashicorp/external v2.1.0 (signed by HashiCorp)
- Installing hashicorp/aws v3.66.0...
- Installed hashicorp/aws v3.66.0 (signed by HashiCorp)
- Installing hashicorp/vault v2.24.1...
- Installed hashicorp/vault v2.24.1 (signed by HashiCorp)

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
mcassaniti@wks-mcass-linux:~/test-this/agent-monitoring$ terraform validate
╷
│ Error: Invalid index
│ 
│   on providers.tf line 41, in provider "aws":
│   41:   region              = local.aws_provider_config[terraform.workspace].region
│     ├────────────────
│     │ local.aws_provider_config is object with 2 attributes
│     │ terraform.workspace is "default"
│ 
│ The given key does not identify an element in this collection value.
╵
╷
│ Error: Invalid index
│ 
│   on providers.tf line 42, in provider "aws":
│   42:   allowed_account_ids = [local.aws_provider_config[terraform.workspace].account_id]
│     ├────────────────
│     │ local.aws_provider_config is object with 2 attributes
│     │ terraform.workspace is "default"
│ 
│ The given key does not identify an element in this collection value.
╵
mcassaniti@wks-mcass-linux:~/test-this/agent-monitoring$ 

from terraform-github-actions.

dflook avatar dflook commented on June 1, 2024

What version of terraform are you using?

from terraform-github-actions.

mcassaniti avatar mcassaniti commented on June 1, 2024

Local version:

mcassaniti@wks-mcass-linux:/mnt/data/GIT/test-this/agent-monitoring$ terraform --version
Terraform v1.0.11
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.66.0
+ provider registry.terraform.io/hashicorp/external v2.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0
+ provider registry.terraform.io/hashicorp/vault v2.24.1
mcassaniti@wks-mcass-linux:/mnt/data/GIT/test-this/agent-monitoring$

Workflow version:

Installing Terraform
  Reading latest terraform version
  Switched terraform to version "1.0.11"

from terraform-github-actions.

dflook avatar dflook commented on June 1, 2024

Hi @mcassaniti, there is a new version v1.21.0 that has a new workspace input for the dflook/terraform-validate action.

I was mistaken in that terraform.workspace is evaluated, but terraform doesn't initialise it properly when doing a validate.
Using the new workspace input you can set the full name of the workspace to use while validating.

For the S3 backend this should match the workspace name you normally use.

from terraform-github-actions.

mcassaniti avatar mcassaniti commented on June 1, 2024

Thank you so much and thanks for the quick turnaround.

from terraform-github-actions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.