Skip to content

Commit

Permalink
feat: add template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
MickVanDuijn committed Mar 27, 2024
1 parent ee8eeea commit c3d38b3
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 18 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/terraform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: terraform
on: push

jobs:
tfsec:
uses: skyleague/node-standards/.github/workflows/reusable-tfsec.yml@main
with:
terraform-version: "1.7.1"
working-directory: "./"
# tfsec-var-files: '["test/default.tfvars", "test/a.tfvars"]'

tests:
name: tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: setup terraform and cache
uses: skyleague/node-standards/.github/actions/setup-terraform@main
with:
terraform-version: "1.7.1"
working-directory: "./test"
github-app-id: ${{ secrets.GITHUB_APP_ID }}
github-app-pem: ${{ secrets.GITHUB_APP_PEM }}
- name: terraform init
run: terraform init
working-directory: "./test"
- name: terraform test
run: terraform test
working-directory: "./test"
10 changes: 0 additions & 10 deletions .github/workflows/tfsec.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"editor.tabSize": 2
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": true,
"editor.wordSeparators": "`~!@#%^&*()-=+[{]}\\|;:'\",.<>/?",
Expand Down
10 changes: 6 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ data "external" "definition" {
])
}
locals {
raw_definition = endswith(var.definition.file, ".json") ? jsondecode(templatefile(var.definition.file, {
template_parameters = merge(coalesce(try(var.definition.template_parameters, null), {}), {
aws_region = data.aws_region.current.name
aws_account_id = data.aws_caller_identity.current.account_id
})) : data.external.definition[0].result
lambda_arns = jsondecode(local.raw_definition.lambda_arns)
definition = jsondecode(local.raw_definition.definition)
})
file_definition = endswith(var.definition.file, ".json") ? jsondecode(templatefile(var.definition.file, local.template_parameters)) : null
raw_definition = endswith(var.definition.file, ".json") ? local.file_definition : data.external.definition[0].result
lambda_arns = jsondecode(local.raw_definition.lambda_arns)
definition = jsondecode(local.raw_definition.definition)
}

resource "aws_sfn_state_machine" "this" {
Expand Down
22 changes: 22 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
variable "name" {
type = string
}

variable "file" {
type = string
}

variable "template_parameters" {
type = map(string)
default = null
}

module "sfn" {
source = "../"

name = var.name
definition = {
file = "${abspath(path.module)}/${var.file}"
template_parameters = var.template_parameters
}
}
4 changes: 4 additions & 0 deletions test/stubs/definition.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"definition": "{\"foo\":\"${bar}\", \"region\":\"${aws_region}\"}",
"lambda_arns": "[]"
}
39 changes: 39 additions & 0 deletions test/template_parameters.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
mock_provider "aws" {
alias = "mock"

override_data {
target = module.sfn.data.aws_iam_policy_document.tracing
values = {
json = "{}"
}
}

override_data {
target = module.sfn.data.aws_region.current
values = {
name = "eu-west-1"
}
}
}

run "template_parameters_replaced_correctly" {
command = plan

providers = {
aws = aws.mock
}

variables {
name = "foo"
file = "stubs/definition.json"
template_parameters = {
bar = "baz"
}
}

assert {
condition = jsondecode(module.sfn.state_machine.definition) == { foo = "baz", region = "eu-west-1" }
error_message = "Template was not rendered correctly"
}

}
12 changes: 9 additions & 3 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
variable "definition" {
description = "JSON definition of the Stepfunction"
type = object({
file = string
export = optional(string)
file = string
export = optional(string)
template_parameters = optional(map(string), {})
})

validation {
condition = endswith(var.definition.file, ".json") || endswith(var.definition.file, ".ts") || endswith(var.definition.file, ".js")
error_message = "Invalid definition type; supported types are: ts, json"
error_message = "Invalid definition type; supported types are: ts, js, json"
}

validation {
condition = endswith(var.definition.file, ".json") || var.definition.export != null
error_message = "Name for 'export' is required when including a js or ts definition."
}

validation {
condition = var.definition.template_parameters == null || endswith(var.definition.file, ".json")
error_message = "Template parameters are only supported for json definitions."
}
}
variable "name" {
description = "Name of the Stepfunction"
Expand Down

0 comments on commit c3d38b3

Please sign in to comment.