• Stars
    star
    144
  • Rank 255,590 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Command line utility and JavaScript API for parsing stdout from "terraform plan" and converting it to JSON.

Terraform Plan Parser

Greenkeeper badge Build Status Coverage Status npm version

This project provides a CLI and JavaScript API for parsing terraform plan output.

IMPORTANT: This tool does not parse the file produced by the -out=path argument to terraform plan which is a binary file. There is not a stable specification for this binary file format so, at this time, it is safer to parse the somewhat structured textual output that gets written to stdout.

Why should I use this?

This parser allows the textual log output from terraform plan to be converted to JSON which is more machine readable.

Here are some suggested use cases:

  • Send notification when certain types of changes are detected. For example, email security team if an IAM policy is modified.
  • Validate that certain changes are allowed for a given change management request before invoking terraform apply.
  • Kick-off a special workflow for certain types of changes to the infrastructure (possibly, before calling terraform apply).

If you wish to perform linting or enforcement of best practices then your better option might be to analyze the source terraform code instead of only looking at the changes that are described by the terraform plan output.

Usage

JavaScript API

NPM:

npm install terraform-plan-parser

Yarn Package Manager:

yarn add terraform-plan-parser

IMPORTANT:

This project requires Node v8.9.0 (LTS) or newer because the source code utilizes language features such as async / await. If you are using an unsupported version of Node then you will see SyntaxError: Unexpected token function. It's possible to use babel to transpile the code for older versions of the Node runtime. The babel-preset-env is a good package for supporting this.

Parse string that contains stdout logs from terraform plan

const fs = require('fs');
const parser = require('terraform-plan-parser');

const stdout = fs.readFileSync('terraform-plan.stdout', {encoding: 'utf8'});
const result = parser.parseStdout(stdout);

Command Line

NPM:

npm install -g terraform-plan-parser

Yarn Package Manager:

yarn add global terraform-plan-parser

Command help:

# Get help on using command
parse-terraform-plan --help
Options:
  --help        Show help                                              [boolean]
  --version     Show version number                                    [boolean]
  -i, --input   Input file (stdin is used if not provided)              [string]
  -o, --output  Output file (stdout is used if not provided)            [string]
  --pretty      Output JSON in pretty format          [boolean] [default: false]

Read from stdin and write to stdout:

# Pipe output from "terraform plan" to parser which will convert it to JSON
terraform plan | parse-terraform-plan --pretty

Read from file and write to file:

# Store "terraform plan" output in file
terraform plan > terraform-plan.stdout

# Read from "terraform plan" output file and write to JSON file
parse-terraform-plan --pretty -i terraform-plan.stdout -o terraform-plan.json

Output Schema

The output is an object with these top-level properties:

  • errors: An array of parsing errors
  • changedResources: An array of changed resources
  • changedDataSources: An array of changed data sources

Each changed resource has the following properties:

  • action: One of "create", "destroy", "replace", "update"
  • type: Type of resource (e.g. "aws_ecs_service")
  • name: Resource name (e.g. "my_service")
  • path: Full path to resource as printed in plan output (e.g. "module.module1.module.module2.aws_ecs_service.my_service")
  • module: Fully qualified module name (e.g. "module1.module2") or undefined if resource not within module.
  • changedAttributes: An object whose keys are an attribute name and value is an object
  • newResourceRequired: A flag to indicate if a new resource is required (only present if true)
  • tainted: A flag to indicate if resource is tainted (only present if true)

A changed attribute object has the following properties:

  • old: An object with type property and value property which describes the old state of the attribute. The type will be "computed" or "string". The value will be a string.
  • new: An object with type property and value property which describes the new state of the attribute. The type will be "computed" or "string". The value will be a string.

Each data source has the following properties:

  • action: The action will always be "read"
  • type: Type of resource (e.g. "external")
  • name: Data source name (e.g. "ecr_image_digests")
  • path: Full path to data source as printed in plan output (e.g. "module.module1.module.module2.data.external.ecr_image_digests")
  • module: Fully qualified module name (e.g. "module1.module2") or undefined if data source not within module.
  • changedAttributes: An object whose keys are an attribute name and value is an object

Example Output

{
  "errors": [],
  "changedResources": [
    {
      "action": "update",
      "type": "aws_ecs_service",
      "name": "sample_app",
      "path": "aws_ecs_service.sample_app",
      "changedAttributes": {
        "task_definition": {
          "old": {
            "type": "string",
            "value": "arn:aws:ecs:us-east-1:123123123123:task-definition/sample-app:186"
          },
          "new": {
            "type": "string",
            "value": "${ aws_ecs_task_definition.sample_app.arn }"
          }
        }
      }
    }
  ],
  "changedDataSources": [
    {
      "action": "read",
      "type": "external",
      "name": "ecr_image_digests",
      "path": "data.external.ecr_image_digests",
      "changedAttributes": {
        "id": {
          "new": {
            "type": "computed"
          }
        },
        "program.#": {
          "new": {
            "type": "string",
            "value": "1"
          }
        },
        "program.0": {
          "new": {
            "type": "string",
            "value": "extract-image-digests"
          }
        },
        "result.%": {
          "new": {
            "type": "computed"
          }
        }
      }
    }
  ]
}

More Repositories

1

sparkflow

Easy to use library to bring Tensorflow on Apache Spark
Python
300
star
2

attempt

A JavaScript library that makes it easier to retry functions that return a promise
TypeScript
180
star
3

axios-fetch

A WebAPI Fetch implementation backed by an Axios client
TypeScript
170
star
4

json-schema-to-graphql-types

A tool to convert JSON Schemas into GraphQL Types
JavaScript
100
star
5

graphql-resolvers-xray-tracing

A GraphQL middleware to enable X-Ray tracing subsegments for GraphQL resolvers
TypeScript
66
star
6

lambda-typescript-webpack-babel-starter

This project demonstrates how to write, test, and package AWS Lambda functions using TypeScript, webpack, and babel
TypeScript
52
star
7

chroma-react

Open source design system from LifeOmic, built with React
TypeScript
48
star
8

react-vis-network

[Deprecated] Declarative vis.js network for react
JavaScript
33
star
9

lambda-tools

Common utilities for Lambda testing and development
JavaScript
23
star
10

cli

A CLI app that provides functionality around the LifeOmic platform.
JavaScript
20
star
11

alpha

Unified client for HTTP services
TypeScript
20
star
12

spark-vcf

Spark VCF data source implementation for Dataframes
Scala
14
star
13

dynamodb-dataloader

A dataloader for DyanmoDB
TypeScript
12
star
14

bitrise

Bitrise API client
JavaScript
11
star
15

rate-limiter-py

Rate-limiter module which leverages DynamoDB to enforce resource limits
Python
11
star
16

fhirlib-scala

Scala
9
star
17

blamda

Bundle lambdas really fast!
TypeScript
7
star
18

twilio-webhook-validator-koa

Koa middleware for Twilio webhook request validation
TypeScript
5
star
19

terraform-provider-appstore

Terraform provider for managing app store resources
Go
4
star
20

react-native-prim

Prim is a customizable TypeScript atomic styles framework for React Native.
TypeScript
4
star
21

chromicons

Handcrafted open source icons from LifeOmic
JavaScript
4
star
22

abac

Attribute Based Access Control for Lifeomic products
TypeScript
3
star
23

alpha-cli

Curl-like CLI for the Alpha client
TypeScript
3
star
24

gradual-feature-toggle-tools

Provides a set of helper functions to make gradual, non-binary, feature toggle use easier
TypeScript
3
star
25

chromicons.com

Web app for displaying LifeOmic Chromicons
JavaScript
2
star
26

terraform-provider-marketplace

Go
2
star
27

eslint-plugin-frontend

A repository containing custom ESLint rules for frontend LifeOmic projects
JavaScript
2
star
28

csv-to-json

CSV to JSON Mapping Tool
Python
2
star
29

eslint-plugin-node

Shared ESLint configuration and rules for LifeOmic projects
JavaScript
2
star
30

TaskDeepLearning

Deep learning tools for task service
Python
2
star
31

eslint-plugin-i18next

An ESLint plugin for validating i18next usage.
TypeScript
2
star
32

react-native-sdk

React-native components for building a mobile app against the LifeOmic Platform.
TypeScript
2
star
33

one-schema

Build end-to-end typesafe REST APIs using TypeScript.
TypeScript
1
star
34

one-query

This package helps you use react-query with a REST API, and get type safety everywhere.
TypeScript
1
star
35

device-samples

Sample code and projects for med devices integrating with the LifeOmic Platform
1
star
36

dynamost

Interact with a DynamoDB table, and keep type safety end-to-end.
TypeScript
1
star
37

delta

Tools for working with data streams in AWS Lambda
TypeScript
1
star
38

phc-sdk-py

The phc-sdk-py is a developer kit for interfacing with the PHC API on Python 3.8 and above.
Python
1
star