{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/5278","repository_url":"https://api.github.com/repos/hashicorp/terraform","labels_url":"https://api.github.com/repos/hashicorp/terraform/issues/5278/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/terraform/issues/5278/comments","events_url":"https://api.github.com/repos/hashicorp/terraform/issues/5278/events","html_url":"https://github.com/hashicorp/terraform/issues/5278","id":135851021,"number":5278,"title":"Embed lua interpreter in terraform","user":{"login":"sheerun","id":292365,"avatar_url":"https://avatars.githubusercontent.com/u/292365?v=3","gravatar_id":"","url":"https://api.github.com/users/sheerun","html_url":"https://github.com/sheerun","followers_url":"https://api.github.com/users/sheerun/followers","following_url":"https://api.github.com/users/sheerun/following{/other_user}","gists_url":"https://api.github.com/users/sheerun/gists{/gist_id}","starred_url":"https://api.github.com/users/sheerun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sheerun/subscriptions","organizations_url":"https://api.github.com/users/sheerun/orgs","repos_url":"https://api.github.com/users/sheerun/repos","events_url":"https://api.github.com/users/sheerun/events{/privacy}","received_events_url":"https://api.github.com/users/sheerun/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/hashicorp/terraform/labels/core","name":"core","color":"cccccc"},{"url":"https://api.github.com/repos/hashicorp/terraform/labels/enhancement","name":"enhancement","color":"d4c5f9"},{"url":"https://api.github.com/repos/hashicorp/terraform/labels/thinking","name":"thinking","color":"fad8c7"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":16,"created_at":"2016-02-23T20:11:39Z","updated_at":"2016-03-12T01:13:22Z","closed_at":null,"body":"Hey,\r\n\r\nI'd like to discuss embedding lua interpreter into terraform. The same has been done by redis, haproxy, and nginx to give few examples. This feature has good use cases in and avoid transforming terraform syntax into ugly turing-complete language.\r\n\r\n## Use case 1\r\n\r\nI'd like to have default for variable computed dynamically instead of static string. For now it's not possible and can be only simulated with \"template_file\" resource as demonstrated by \"etcd_discovery_url\" in following config: https://github.com/Capgemini/Apollo/blob/devel/terraform/digitalocean/main.tf\r\n\r\nCould we instead allow for dynamic custom variables so following is possible?\r\n\r\n```\r\nvariable \"etcd_discovery_url_file\" {\r\n  default = \"#{\r\n    return http.request('https://discovery.etcd.io/new?size=' .. (${var.masters} + ${var.slaves})')\r\n  }\"\r\n}\r\n```\r\n\r\n## Use case 2\r\n\r\nIt's like to provide custom logic for configuration for resources. This already known use case as shown in: #1604 #4548 #3358\r\n\r\nInstead trying to hack terraform creating new language, let's just allow to embed lua:\r\n\r\n```\r\nresource \"aws_instance\" \"machine\" {\r\n  ami = \"#{\r\n    if ${var.atlas_enabled} then\r\n      return '${atlas_artifact.machine.id}'\r\n    else\r\n      return '${var.ami_string}'\r\n    end\r\n  }\"\r\n}\r\n```\r\n\r\nCode blocks are executed only one time to get defaults for configurations and variables.\r\n\r\nAs per [redis documentation](http://redis.io/commands/EVAL):\r\n\r\n> All Redis commands must be analyzed before execution to determine which keys the command will operate on. (...) Lua scripts can return a value that is converted from the Lua type to the Redis protocol using a set of conversion rules.\r\n\r\n## Proposed behavior\r\n\r\n1. Introduce `#{lua_code}` interpolation syntax similar to variable interpolation `${variable_name}` but where content is body of lua function, and value is the \"return\" value from this function.\r\n2. In lua code allow to embed terraform variables using usual syntax: `${variable}`\r\n\r\nThis way *terraform knows on what variables functions depend on*, so it knows when to re-execute function (one-time only, to result is cached if input variables don't change)."},"comment":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/comments/195622663","html_url":"https://github.com/hashicorp/terraform/issues/5278#issuecomment-195622663","issue_url":"https://api.github.com/repos/hashicorp/terraform/issues/5278","id":195622663,"user":{"login":"mitchellh","id":1299,"avatar_url":"https://avatars.githubusercontent.com/u/1299?v=3","gravatar_id":"","url":"https://api.github.com/users/mitchellh","html_url":"https://github.com/mitchellh","followers_url":"https://api.github.com/users/mitchellh/followers","following_url":"https://api.github.com/users/mitchellh/following{/other_user}","gists_url":"https://api.github.com/users/mitchellh/gists{/gist_id}","starred_url":"https://api.github.com/users/mitchellh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mitchellh/subscriptions","organizations_url":"https://api.github.com/users/mitchellh/orgs","repos_url":"https://api.github.com/users/mitchellh/repos","events_url":"https://api.github.com/users/mitchellh/events{/privacy}","received_events_url":"https://api.github.com/users/mitchellh/received_events","type":"User","site_admin":false},"created_at":"2016-03-12T01:13:22Z","updated_at":"2016-03-12T01:13:22Z","body":"Adding something to the core means we can't remove it without severely hurting backwards compatbility. All the while, for users that _need_ this to get things done, what you're proposing is a simple Ruby script that anyone can write, and doesn't need to burden core with backwards compat concerns."}}