Giter VIP home page Giter VIP logo

terrascript's Introduction

Build Status Gem Version

What is Terrascript

Terrascript is wrapper around Terraform which adds the ability to script repetitive tasks as inline ruby code.

Terrascript parses .tfrb files, executes ruby code and outputs .tf files.

Simply invoke tfsc with your usual Terraform arguments and enjoy the automation.

Usage

The overall structure is as shown below:

@inline
# ruby code
return
# plain text
@end

@inline directive marks the beginning of inline ruby code.

Code is evaluated until a return statement is found.

Text between return and @end statements is passed as an argument to the code. By default it's accessible inside the inline code under block variable, but you can override the name by specifying your own name in @inline directive.

For example @inline arg declaration will pass text in arg variable.

Anything the inline ruby code prints (puts) is written to the destination .tf file.

Examples

@inline
["one", "two"].each do |fn|
  puts block.gsub("<fn>", fn)
end
return
# <fn> alias
module "<fn>_alias" {
  source = "mewa/lambda-alias/aws"
  version = "1.0.0"

  alias = "${local.environment}"
  function_arn = "${aws_lambda_function.<fn>.arn}"
  function_name = "${aws_lambda_function.<fn>.function_name}"
  function_version = "${aws_lambda_function.<fn>.version}"
  invoke_arn = "${aws_lambda_function.<fn>.invoke_arn}"
}

# <fn> permission
resource "aws_lambda_permission" "<fn>_lambda_permission" {
  statement_id  = "AllowAPIGatewayInvoke"
  principal     = "apigateway.amazonaws.com"
  action        = "lambda:InvokeFunction"
  function_name = "${aws_lambda_function.<fn>.function_name}"
  qualifier = "${module.<fn>_alias.alias}"

  source_arn = "${aws_api_gateway_deployment.stage.execution_arn}/*/*"
}
@end

Will be rendered as

# one alias
module "one_alias" {
  source = "mewa/lambda-alias/aws"
  version = "1.0.0"

  alias = "${local.environment}"
  function_arn = "${aws_lambda_function.one.arn}"
  function_name = "${aws_lambda_function.one.function_name}"
  function_version = "${aws_lambda_function.one.version}"
  invoke_arn = "${aws_lambda_function.one.invoke_arn}"
}

# one permission
resource "aws_lambda_permission" "one_lambda_permission" {
  statement_id  = "AllowAPIGatewayInvoke"
  principal     = "apigateway.amazonaws.com"
  action        = "lambda:InvokeFunction"
  function_name = "${aws_lambda_function.one.function_name}"
  qualifier = "${module.one_alias.alias}"

  source_arn = "${aws_api_gateway_deployment.stage.execution_arn}/*/*"
}
# two alias
module "two_alias" {
  source = "mewa/lambda-alias/aws"
  version = "1.0.0"

  alias = "${local.environment}"
  function_arn = "${aws_lambda_function.two.arn}"
  function_name = "${aws_lambda_function.two.function_name}"
  function_version = "${aws_lambda_function.two.version}"
  invoke_arn = "${aws_lambda_function.two.invoke_arn}"
}

# two permission
resource "aws_lambda_permission" "two_lambda_permission" {
  statement_id  = "AllowAPIGatewayInvoke"
  principal     = "apigateway.amazonaws.com"
  action        = "lambda:InvokeFunction"
  function_name = "${aws_lambda_function.two.function_name}"
  qualifier = "${module.two_alias.alias}"

  source_arn = "${aws_api_gateway_deployment.stage.execution_arn}/*/*"
}

Future improvements

This project was written for my internal needs and serves its purpose.

However if it gains traction I could look into implementing a cleaner solution that abstracts the code for some common tasks and leaves open door for extension (plugins, probably).

The code presented here as an example could then look like this:

@replace "<fn>" with ["one", "two"]
# <fn> alias
module "<fn>_alias" {
    some_attribute = "${some_resource.<fn>.something}"
}
@end

terrascript's People

Contributors

mewa avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.