Skip to content

Commit

Permalink
feat: replace template provider with templatefile function (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: cloudpossebot <[email protected]>
  • Loading branch information
kevcube and cloudpossebot authored Feb 17, 2022
1 parent 3f203ee commit 7f8fc52
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 26 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ Available targets:
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.55 |
| <a name="requirement_template"></a> [template](#requirement\_template) | >= 2.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.55 |
| <a name="provider_template"></a> [template](#provider\_template) | >= 2.1 |

## Modules

Expand All @@ -135,7 +133,6 @@ Available targets:
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_region.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route53_zone.domain](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
| [template_file.user_data](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |

## Inputs

Expand Down
3 changes: 0 additions & 3 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.55 |
| <a name="requirement_template"></a> [template](#requirement\_template) | >= 2.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.55 |
| <a name="provider_template"></a> [template](#provider\_template) | >= 2.1 |

## Modules

Expand All @@ -36,7 +34,6 @@
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_region.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
| [aws_route53_zone.domain](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
| [template_file.user_data](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file) | data source |

## Inputs

Expand Down
4 changes: 0 additions & 4 deletions examples/complete/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ terraform {
source = "hashicorp/aws"
version = ">= 2.55"
}
template = {
source = "hashicorp/template"
version = ">= 2.1"
}
}
}
18 changes: 6 additions & 12 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ locals {
replace(join("", aws_eip.default.*.public_ip), ".", "-"),
data.aws_region.default.name == "us-east-1" ? "compute-1" : format("%s.compute", data.aws_region.default.name)
) : null
user_data_templated = templatefile("${path.module}/${var.user_data_template}", {
user_data = join("\n", var.user_data)
ssm_enabled = var.ssm_enabled
ssh_user = var.ssh_user
})
}

data "aws_region" "default" {}
Expand Down Expand Up @@ -46,24 +51,13 @@ data "aws_route53_zone" "domain" {
zone_id = var.zone_id
}

data "template_file" "user_data" {
count = module.this.enabled ? 1 : 0
template = file("${path.module}/${var.user_data_template}")

vars = {
user_data = join("\n", var.user_data)
ssm_enabled = var.ssm_enabled
ssh_user = var.ssh_user
}
}

resource "aws_instance" "default" {
#bridgecrew:skip=BC_AWS_PUBLIC_12: Skipping `EC2 Should Not Have Public IPs` check. NAT instance requires public IP.
#bridgecrew:skip=BC_AWS_GENERAL_31: Skipping `Ensure Instance Metadata Service Version 1 is not enabled` check until BridgeCrew support condition evaluation. See https://github.com/bridgecrewio/checkov/issues/793
count = module.this.enabled ? 1 : 0
ami = coalesce(var.ami, join("", data.aws_ami.default.*.id))
instance_type = var.instance_type
user_data = length(var.user_data_base64) > 0 ? var.user_data_base64 : data.template_file.user_data[0].rendered
user_data = length(var.user_data_base64) > 0 ? var.user_data_base64 : local.user_data_templated
vpc_security_group_ids = compact(concat(module.security_group.*.id, var.security_groups))
iam_instance_profile = local.instance_profile
associate_public_ip_address = var.associate_public_ip_address
Expand Down
4 changes: 0 additions & 4 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ terraform {
source = "hashicorp/aws"
version = ">= 2.55"
}
template = {
source = "hashicorp/template"
version = ">= 2.1"
}
}
}

0 comments on commit 7f8fc52

Please sign in to comment.