0.0.2 • Published 4 years ago

terraform-get-plan v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

Terraform Get Plan

This simple library allows you to execute a Terraform plan from your Node.js application and retrieve the resulting plan as JSON or a native object.

Usage

Require the module:

const {getPlan} = require('terraform-get-plan');

You can run the plan on the current working directory:

const plan = await getPlan();

You can run the plan on a different directory:

const plan = await getPlan({workingDirectory: '/Users/tf'});

You can define a terraform file inline:

const plan = await getPlan({
    terraform: `
    provider "aws" {
        region                      = "us-east-1"
        skip_credentials_validation = true
        skip_requesting_account_id  = true
        skip_metadata_api_check     = true
        s3_force_path_style         = true
        access_key                  = "mock_access_key"
        secret_key                  = "mock_secret_key"
    
    }
    
    resource "aws_ssm_parameter" "foo" {
        name  = "foo"
        type  = "String"
        value = "bar"
    }
    `
});

Use Cases

My primary use case for writing this is so I can write jest tests on Terraform modules I am designing. These are not a replacement for a full integration suite, but having a fast static feedback loop is really important when writing code. This helps enable that. I am sure there are other use cases, too.

Here's an example:

const {getPlan} = require('terraform-get-plan');

jest.setTimeout(120000); // plan can be slow...

test('Expect the module to work...', async () => {
    const plan = await getPlan({terraform: 
    `
    provider "aws" {
        alias                       = "mock"
        region                      = "us-east-1"
        skip_credentials_validation = true
        skip_requesting_account_id  = true
        skip_metadata_api_check     = true
        s3_force_path_style         = true
        access_key                  = "mock_access_key"
        secret_key                  = "mock_secret_key"
    }

    module "test" {
        source = "./terraform"
        value  = "hello"
        providers = {
            aws = aws.mock
        }
    }
    `
    });
    expect(plan.planned_values.root_module.child_modules[0].resources[0].type).toBe('aws_ssm_parameter');
    expect(plan.planned_values.root_module.child_modules[0].resources[0].name).toBe('my_param');
    expect(plan.planned_values.root_module.child_modules[0].resources[0].values.value).toBe('hello');
});