Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check nested terraform blocks #217

Open
adamwallred opened this issue Oct 16, 2020 · 1 comment
Open

How to check nested terraform blocks #217

adamwallred opened this issue Oct 16, 2020 · 1 comment

Comments

@adamwallred
Copy link

Hello,

Assuming I have the following resource:

resource "google_service_account" "sa" {
  account_id   = "sa"
  display_name = "sa"

  lifecycle {
    prevent_destroy = true
  }
}

What would the rule look like that could assert that prevent_destroy is present and true? I assumed:

---
version: 1
description: Google-specific Terraform Rules
type: Terraform
files:
  - "*.tf"
rules:
  - id: PREVENT_DESTROY_IN_SERVICE_ACCOUNT
    message: "Service Account definitions must have prevent_destroy set"
    category: resource
    resources:
      - google_service_account
    assertions:
      - key: lifecycle.prevent_destroy
        op: eq
        value: true

would work, but it fails:

  {
    "AssertionMessage": "lifecycle.prevent_destroy(null) should be equal to true",
    "Category": "resource",
    "CreatedAt": "2020-10-16T14:59:05Z",
    "Filename": "test/sa.tf",
    "LineNumber": 1,
    "ResourceID": "sa",
    "ResourceType": "google_service_account",
    "RuleID": "PREVENT_DESTROY_IN_SERVICE_ACCOUNT",
    "RuleMessage": "Service Account definitions must have prevent_destroy set",
    "Status": "FAILURE"
  }
@adamwallred
Copy link
Author

Figured it out. lifecycle is an array (though I don't understand why).

---
version: 1
description: Google-specific Terraform Rules
type: Terraform
files:
  - "*.tf"
rules:
  - id: PREVENT_DESTROY_IN_SERVICE_ACCOUNT
    message: "Service Account definitions must have prevent_destroy set"
    category: resource
    resources:
      - google_service_account
    assertions:
      - exactly-one:
          key: "lifecycle[]"
          expressions:
            - key: prevent_destroy
              op: is-true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant