All Projects → elmundio87 → Terraform_validate

elmundio87 / Terraform_validate

Licence: gpl-3.0
Assists in the enforcement of user-defined standards in Terraform

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Terraform validate

Terraform Aws Elastic Beanstalk Environment
Terraform module to provision an AWS Elastic Beanstalk Environment
Stars: ✭ 211 (-13.88%)
Mutual labels:  terraform, hcl
Terraform Aws Tfstate Backend
Terraform module that provision an S3 bucket to store the `terraform.tfstate` file and a DynamoDB table to lock the state file to prevent concurrent modifications and state corruption.
Stars: ✭ 229 (-6.53%)
Mutual labels:  terraform, hcl
K8s Scw Baremetal
Kubernetes installer for Scaleway bare-metal AMD64 and ARMv7
Stars: ✭ 176 (-28.16%)
Mutual labels:  terraform, hcl
Terraform Aws Components
Opinionated, self-contained Terraform root modules that each solve one, specific problem
Stars: ✭ 168 (-31.43%)
Mutual labels:  terraform, hcl
Terragrunt Reference Architecture
Terragrunt Reference Architecture (upd: May 2020)
Stars: ✭ 204 (-16.73%)
Mutual labels:  terraform, hcl
Tfk8s
A tool for converting Kubernetes YAML manifests to Terraform HCL
Stars: ✭ 167 (-31.84%)
Mutual labels:  terraform, hcl
Terraform Website S3 Cloudfront Route53
Terraform scripts to setup an S3 based static website, with a CloudFront distribution and the required Route53 entries.
Stars: ✭ 210 (-14.29%)
Mutual labels:  terraform, hcl
Terraform Aws Kubernetes
Terraform module for Kubernetes setup on AWS
Stars: ✭ 159 (-35.1%)
Mutual labels:  terraform, hcl
Terraform Aws Ecs Container Definition
Terraform module to generate well-formed JSON documents (container definitions) that are passed to the aws_ecs_task_definition Terraform resource
Stars: ✭ 217 (-11.43%)
Mutual labels:  terraform, hcl
Terraform Aws Jenkins
Terraform module to build Docker image with Jenkins, save it to an ECR repo, and deploy to Elastic Beanstalk running Docker stack
Stars: ✭ 197 (-19.59%)
Mutual labels:  terraform, hcl
Terraform Aws Cloudtrail Cloudwatch Alarms
Terraform module for creating alarms for tracking important changes and occurrences from cloudtrail.
Stars: ✭ 170 (-30.61%)
Mutual labels:  terraform, hcl
Intellij Hcl
HCL language support for IntelliJ platform based IDEs
Stars: ✭ 207 (-15.51%)
Mutual labels:  terraform, hcl
Terraform Aws Cloudfront S3 Cdn
Terraform module to easily provision CloudFront CDN backed by an S3 origin
Stars: ✭ 162 (-33.88%)
Mutual labels:  terraform, hcl
Azure arc
Automated Azure Arc environments
Stars: ✭ 224 (-8.57%)
Mutual labels:  terraform, hcl
Terraform Kubernetes Installer
Terraform Installer for Kubernetes on Oracle Cloud Infrastructure
Stars: ✭ 162 (-33.88%)
Mutual labels:  terraform, hcl
Tf aws bastion s3 keys
A Terraform module for creating bastion host on AWS EC2 and populate its ~/.ssh/authorized_keys with public keys from bucket
Stars: ✭ 178 (-27.35%)
Mutual labels:  terraform, hcl
Terraform Kubernetes
Example of deploying a Kubernetes cluster to Google Cloud using Terraform
Stars: ✭ 152 (-37.96%)
Mutual labels:  terraform, hcl
Aws Labs
step by step guide for aws mini labs. Currently maintained on : https://github.com/Cloud-Yeti/aws-labs Youtube playlist for labs:
Stars: ✭ 153 (-37.55%)
Mutual labels:  terraform, hcl
Go Lambda Ping
Deploy a Lambda to Ping a Site in 20 Seconds!
Stars: ✭ 195 (-20.41%)
Mutual labels:  terraform, hcl
Terraform Fargate Example
Example repository to run an ECS cluster on Fargate
Stars: ✭ 206 (-15.92%)
Mutual labels:  terraform, hcl

Terraform Validate

Linux: Linux Build Status

Windows: Windows Build status

A python package that allows users to define Policy as Code for Terraform configurations.

By parsing a directory of .tf files using pyhcl, each defined resource can be tested using this module.

Example Usages

Check that all AWS EBS volumes are encrypted

import terraform_validate

class TestEncryptionAtRest(unittest.TestCase):

    def setUp(self):
        # Tell the module where to find your terraform configuration folder
        self.path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../terraform")
        self.v = terraform_validate.Validator(self.path)

    def test_aws_ebs_volume(self):
        # Assert that all resources of type 'aws_ebs_volume' are encrypted
        self.v.error_if_property_missing() # Fail any tests if the property does not exist on a resource
        self.v.resources('aws_ebs_volume').property('encrypted').should_equal(True)

    def test_instance_ebs_block_device(self):
        # Assert that all resources of type 'ebs_block_device' that are inside a 'aws_instance' are encrypted
        self.v.error_if_property_missing()
        self.v.resources('aws_instance').property('ebs_block_device').property('encrypted').should_equal(True)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestEncryptionAtRest)
    unittest.TextTestRunner(verbosity=0).run(suite)

resource "aws_instance" "foo" {
  # This would fail the test
  ebs_block_device{
    encrypted = false
  }
}

resource "aws_ebs_volume" "bar" {
  # This would fail the test
  encrypted = false
}

Check that AWS resources are tagged correctly

import terraform_validate

class TestEncryptionAtRest(unittest.TestCase):

    def setUp(self):
        # Tell the module where to find your terraform configuration folder
        self.path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"../terraform")
        self.v = terraform_validate.Validator(self.path)

    def test_aws_ebs_volume(self):
        # Assert that all resources of type 'aws_instance' and 'aws_ebs_volume' have the correct tags
        tagged_resources = ["aws_instance","aws_ebs_volume"]
        required_tags = ["name","version","owner"]
        self.v.resources(tagged_resources).property('tags').should_have_properties(required_tags)

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestEncryptionAtRest)
    unittest.TextTestRunner(verbosity=0).run(suite)

Behaviour functions

These affect the results of the Validation functions in a way that may be required for your tests.

Validator.error_if_property_missing()

By default, no errors will be raised if a property value is missing on a resource. This changes the behavior of .property() calls to raise an error if a property is not found on a resource.

Validator.enable_variable_expansion()

By default, variables in property values will not be calculated against their default values. This changes the behaviour of all Validation functions, to work out the value of a string when the variables have default values.

eg. string = "${var.foo}" will be read as string = "1" by the validator if the default value of foo is 1.

Search functions

These are used to gather property values together so that they can be validated.

Validator.resources([resource_types])

Searches for all resources of the required types and outputs a TerraformResourceList.

Can be chained with a .property() function.

If passed a string as an argument, search through all resource types and list the ones that match the string as a regex. If passed a list as an argument, only use the types that are inside the list.

Outputs: TerraformResourceList

TerraformResourceList.property(property_name)

Collects all top-level properties in a TerraformResourceList and exposes methods that can be used to validate the property values.

Can be chained with another .property() call to fetch nested properties.

eg. .resource('aws_instance').property('name')

TerraformResourceList.find_property(regex)

Similar to TerraformResourceList.property(), except that it will attempt to use a regex string to search for the property.

eg. .resource('aws_instance').find_property('tag[a-z]')

TerraformPropertyList.property(property_name)

Collects all nested properties in TerraformPropertyList and exposes methods that can be used to validate the property values.

eg. .resource('aws_instance').property('tags').property('name')

TerraformPropertyList.find_property(regex)

Similar to TerraformPropertyList.property(), except that it will attempt to use a regex string to search for the property.

eg. .resource('aws_instance').find_property('tag[a-z]')

Validation functions

If there are any errors, these functions will print the error and raise an AssertionError. The purpose of these functions is to validate the property values of different resources.

TerraformResourceList.should_have_properties([required_properties])

Will raise an AssertionError if any of the properties in required_properties are missing from a TerraformResourceList.

TerraformPropertyList.should_have_properties([required_properties])

Will raise an AssertionError if any of the properties in required_properties are missing from a TerraformPropertyList.

TerraformResourceList.should_not_have_properties([excluded_properties])

Will raise an AssertionError if any of the properties in required_properties are missing from a TerraformResourceList.

TerraformPropertyList.should_not_have_properties([excluded_properties])

Will raise an AssertionError if any of the properties in required_properties are missing from a TerraformPropertyList.

TerraformResourceList.name_should_match_regex(regex)

Will raise an AssertionError if the Terraform resource name does not match the value of regex

TerraformPropertyList.should_equal(expected_value)

Will raise an AssertionError if the value of the property does not equal expected_value

TerraformPropertyList.should_not_equal(unexpected_value)

Will raise an AssertionError if the value of the property equals unexpected_value

TerraformPropertyList.should_match_regex(regex)

Will raise an AssertionError if the value of the property does not match the value of regex

TerraformPropertyList.list_should_contain([value])

Will raise an AssertionError if the list value does not contain any of the [value]

TerraformPropertyList.list_should_not_contain([value])

Will raise an AssertionError if the list value does contain any of the [value]

Run with Docker

Build the terraform_validate daemon using:

docker build -t terraform_validate .

Then, on a different location, place your tests on your tests.py.

To run:

docker run -v `pwd`:/terraform_validate terraform_validate

Example output (All tests passing):

$ docker run -v `pwd`:/terraform_validate terraform_validate
----------------------------------------------------------------------
Ran 3 tests in 1.607s

OK
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].