百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

terraform-generator

> 数据库
开源

使用 Node.js 生成 Terraform 配置。

100 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

使用 Node.js 生成 Terraform 配置。

terraform-generator

Use Node.js to generate Terraform configurations.

You do not need to have Terraform installed to use this module.

The end result of using this module is Terraform configurations in plain text, you will need to write the text into a file (terraform-generator does provide an utility function to write the file for you) and execute it yourself.

Currently support generating configurations for Terraform version >= 0.12.

API Documentation

Benefit

Make use of all Javascript programming features (some of which is not available in Terraform), e.g. functions, array, loops, if-else, map, etc. to generate a plain Terraform configurations.

You can easily maintain your infra in Javascript/Typescript.

You don't need to use Terraform variables, you can use your own Javascript/JSON variables or use dotenv.

You don't need to use Terraform modules for reusable resource creations, you can make use of Javascript functions.

You can incorporate other Node.js libraries to facilitate your work.

Limitation

The generated configurations are unformatted and its validity is not verified, use terraform fmt and terraform plan to format it and check its validity yourself.

Install via NPM

bash
npm install terraform-generator

Usage

Import

typescript
import { TerraformGenerator, Resource, map, fn } from 'terraform-generator';

or

typescript
const { TerraformGenerator, Resource, map, fn } = require('terraform-generator');

Initialize TerraformGenerator

typescript
const tfg = new TerraformGenerator({
  required_version: '>= 0.12'
});

Blocks

Some block's arguments are not typed, please refer to official Terraform documentation on what arguments can be supplied.

typescript
tfg.provider('aws', {
  region: 'ap-southeast-1',
  profile: 'example'
});

const vpc = tfg.resource('aws_vpc', 'vpc', {
  cidr_block: '172.88.0.0/16'
});

Convert resource to data source

typescript
import { vpc as vpcDS } from 'other-terraform-generator-configuration';

const vpc = tfg.dataFromResource(vpcDS, undefined, ['cidr_block', ['tags', 'tag']]);

Arguments

…

Attributes

typescript
block.attr('id')                                      // block id, string
block.id                                              // convenience getter function, same as above
block.attr('subnets')                                 // subnet objects, object list
block.attr('subnets.*.id')                            // subnet ids, string list
block.attr('subnets').attr('*').attr('id')            // same as above
block.attr('subnets.*.id[0]')                         // first subnet id, string
block.attr('subnets').attr('*').attr('id').element(0) // same as above

Variables

typescript
// You can directly add multiple variable values.
tfg.addVars({
  var1: 123,
  var2: 'abc'
});

// You can also add single variable value while adding variable block to your configuration.
tfg.variable('password', {
  type: 'string'
}, 'p@ssW0rd');

Merge multiple configurations

typescript
// You can split your configuration into multiple files and merge them before you generate the final outcome.
tfg.merge(tfg2, tfg3);

Generate Terraform configuration

typescript
// Generate Terraform configuration as string
const result = tfg.generate();
console.log(result.tf);
console.log(result.tfvars);

// Write Terraform configuration to a file
tfg.write({
  dir: 'outputDir',
  format: true
});

Example

…

Issues· 1 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptjavascriptterraformterraform-configurationsterraform-project

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库