Wednesday, September 2, 2020

genesis device

GenesisDevice

A tiny Javascript library to kickstart Terraform projects.
GenesisDevice lets you generate Terraform configurations from Javascript data structures. This has the following benefits:
  • no need to memorize Terraform's DSL;
  • programatically generate multiple Terraform configurations;
  • write sanity-checking tests for Terraform files in Javascript.
The configurations generated by GenesisDevice are human-readable with optional comments.

Install

npm install genesis-device

Usage

The Javascript code below:
const GenesisDevice = require('genesis-device');
const genesis = new GenesisDevice();

genesis.addResource('aws_vpc', 'regional_vpc', {
  cidr_block: '10.0.0.0/16',
  instance_tenancy: 'default',
  enable_dns_hostnames: true,
  enable_dns_support: true,
}, [
  'AWS VPC',
  'Create a master VPC for all AZs in the region.',
]);

console.log(genesis.toString());
when run using node will write the following Terraform-syntax configuration to the console:
#
# AWS VPC
# Create a master VPC for all AZs in the region.
#
resource "aws_vpc" "regional_vpc" {
  cidr_block           = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support   = true
  instance_tenancy     = "default"
}
More complicated, nested Terraform constructions are also supported. For example, the code below (using the special $inlines key):
genesis.addResource('aws_security_group', 'db_security_group', {
  description: 'Only allow private IP traffic.',
  name: 'db_security_group',
  vpc_id: '${aws_vpc.regional_vpc.id}',
  $inlines: [
    ['ingress', {
      from_port: 0,
      to_port: 0,
      protocol: '-1',
      cidr_blocks: ['10.0.0.0/8'],
    }],
    ['egress', {
      from_port: 0,
      to_port: 0,
      protocol: '-1',
      cidr_blocks: ['10.0.0.0/8'],
    }],
  ],
}, [
  'AWS Security Group for RDS instances.',
]);
will generate the Terraform fragment below:
#
# AWS Security Group for RDS instances.
#
resource "aws_security_group" "db_security_group" {
  description = "Only allow private IP traffic."
  name        = "db_security_group"
  vpc_id      = "\${aws_vpc.regional_vpc.id}"

  egress {
    cidr_blocks = [
      "10.0.0.0/8"
    ]
    from_port   = 0
    protocol    = "-1"
    to_port     = 0
  }

  ingress {
    cidr_blocks = [
      "10.0.0.0/8"
    ]
    from_port   = 0
    protocol    = "-1"
    to_port     = 0
  }
}

No comments:

Post a Comment

God is dead