Skip to content

Commit 453b6c0

Browse files
authored
upgrades to new terraform v0.12 syntax (#31)
1 parent a91117a commit 453b6c0

18 files changed

+289
-252
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ create an input vars file (`terraform.tfvars`)
8787
app = "my-app"
8888
environment = "dev"
8989
90-
internal = "true"
90+
internal = true
9191
container_port = "8080"
9292
replicas = "1"
9393
health_check = "/health"

base/ecr.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
# create an ECR repo at the app/image level
88
resource "aws_ecr_repository" "app" {
9-
name = "${var.app}"
9+
name = var.app
1010
}
1111

12-
data "aws_caller_identity" "current" {}
12+
data "aws_caller_identity" "current" {
13+
}
1314

1415
# grant access to saml users
1516
resource "aws_ecr_repository_policy" "app" {
16-
repository = "${aws_ecr_repository.app.name}"
17-
policy = "${data.aws_iam_policy_document.ecr.json}"
17+
repository = aws_ecr_repository.app.name
18+
policy = data.aws_iam_policy_document.ecr.json
1819
}
1920

2021
data "aws_iam_policy_document" "ecr" {

base/main.tf

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}
4+
15
/**
26
* main.tf
37
* The main entry point for Terraform run
@@ -9,8 +13,8 @@
913
# Using the AWS Provider
1014
# https://www.terraform.io/docs/providers/
1115
provider "aws" {
12-
region = "${var.region}"
13-
profile = "${var.aws_profile}"
16+
region = var.region
17+
profile = var.aws_profile
1418
}
1519

1620
/*
@@ -21,10 +25,10 @@ provider "aws" {
2125

2226
# Returns the name of the ECR registry, this will be used later in various scripts
2327
output "docker_registry" {
24-
value = "${aws_ecr_repository.app.repository_url}"
28+
value = aws_ecr_repository.app.repository_url
2529
}
2630

2731
# Returns the name of the S3 bucket that will be used in later Terraform files
2832
output "bucket" {
29-
value = "${module.tf_remote_state.bucket}"
33+
value = module.tf_remote_state.bucket
3034
}

base/state.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
module "tf_remote_state" {
1313
source = "github.com/turnerlabs/terraform-remote-state?ref=v2.2.0"
1414

15-
role = "${var.saml_role}"
16-
application = "${var.app}"
17-
tags = "${var.tags}"
15+
role = var.saml_role
16+
application = var.app
17+
tags = var.tags
1818
}

base/variables.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ variable "region" {
1111
}
1212

1313
# The AWS profile to use, this would be the same value used in AWS_PROFILE.
14-
variable "aws_profile" {}
14+
variable "aws_profile" {
15+
}
1516

1617
# The role that will have access to the S3 bucket, this should be a role that all
1718
# members of the team have access to.
18-
variable "saml_role" {}
19+
variable "saml_role" {
20+
}
1921

2022
# Name of the application. This value should usually match the application tag below.
21-
variable "app" {}
23+
variable "app" {
24+
}
2225

2326
# A map of the tags to apply to various resources. The required tags are:
2427
# `application`, name of the app;
@@ -27,5 +30,5 @@ variable "app" {}
2730
# `contact-email`, contact email for the _team_;
2831
# and `customer`, who the application was create for.
2932
variable "tags" {
30-
type = "map"
33+
type = map(string)
3134
}

env/dev/autoscale-perf.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
5454
namespace = "AWS/ECS"
5555
period = "60"
5656
statistic = "Average"
57-
threshold = "${var.ecs_as_cpu_high_threshold_per}"
57+
threshold = var.ecs_as_cpu_high_threshold_per
5858

59-
dimensions {
60-
ClusterName = "${aws_ecs_cluster.app.name}"
61-
ServiceName = "${aws_ecs_service.app.name}"
59+
dimensions = {
60+
ClusterName = aws_ecs_cluster.app.name
61+
ServiceName = aws_ecs_service.app.name
6262
}
6363

64-
alarm_actions = ["${aws_appautoscaling_policy.app_up.arn}"]
64+
alarm_actions = [aws_appautoscaling_policy.app_up.arn]
6565
}
6666

6767
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
@@ -72,21 +72,21 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
7272
namespace = "AWS/ECS"
7373
period = "60"
7474
statistic = "Average"
75-
threshold = "${var.ecs_as_cpu_low_threshold_per}"
75+
threshold = var.ecs_as_cpu_low_threshold_per
7676

77-
dimensions {
78-
ClusterName = "${aws_ecs_cluster.app.name}"
79-
ServiceName = "${aws_ecs_service.app.name}"
77+
dimensions = {
78+
ClusterName = aws_ecs_cluster.app.name
79+
ServiceName = aws_ecs_service.app.name
8080
}
8181

82-
alarm_actions = ["${aws_appautoscaling_policy.app_down.arn}"]
82+
alarm_actions = [aws_appautoscaling_policy.app_down.arn]
8383
}
8484

8585
resource "aws_appautoscaling_policy" "app_up" {
8686
name = "app-scale-up"
87-
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
88-
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
89-
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
87+
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
88+
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
89+
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
9090

9191
step_scaling_policy_configuration {
9292
adjustment_type = "ChangeInCapacity"
@@ -102,9 +102,9 @@ resource "aws_appautoscaling_policy" "app_up" {
102102

103103
resource "aws_appautoscaling_policy" "app_down" {
104104
name = "app-scale-down"
105-
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
106-
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
107-
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
105+
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
106+
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
107+
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
108108

109109
step_scaling_policy_configuration {
110110
adjustment_type = "ChangeInCapacity"

env/dev/autoscale-time.tf

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ variable "scale_down_max_capacity" {
3030
resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_up" {
3131
name = "app-autoscale-time-up-${var.app}-${var.environment}"
3232

33-
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
34-
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
35-
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
36-
schedule = "${var.scale_up_cron}"
33+
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
34+
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
35+
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
36+
schedule = var.scale_up_cron
3737

3838
scalable_target_action {
39-
min_capacity = "${aws_appautoscaling_target.app_scale_target.min_capacity}"
40-
max_capacity = "${aws_appautoscaling_target.app_scale_target.max_capacity}"
39+
min_capacity = aws_appautoscaling_target.app_scale_target.min_capacity
40+
max_capacity = aws_appautoscaling_target.app_scale_target.max_capacity
4141
}
4242
}
4343

@@ -46,13 +46,13 @@ resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_up" {
4646
resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_down" {
4747
name = "app-autoscale-time-down-${var.app}-${var.environment}"
4848

49-
service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}"
50-
resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}"
51-
scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}"
52-
schedule = "${var.scale_down_cron}"
49+
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
50+
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
51+
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
52+
schedule = var.scale_down_cron
5353

5454
scalable_target_action {
55-
min_capacity = "${var.scale_down_min_capacity}"
56-
max_capacity = "${var.scale_down_max_capacity}"
55+
min_capacity = var.scale_down_min_capacity
56+
max_capacity = var.scale_down_max_capacity
5757
}
5858
}

env/dev/cicd.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ resource "aws_iam_user" "cicd" {
44
}
55

66
resource "aws_iam_access_key" "cicd_keys" {
7-
user = "${aws_iam_user.cicd.name}"
7+
user = aws_iam_user.cicd.name
88
}
99

1010
# grant required permissions to deploy
@@ -24,7 +24,7 @@ data "aws_iam_policy_document" "cicd_policy" {
2424
]
2525

2626
resources = [
27-
"${data.aws_ecr_repository.ecr.arn}",
27+
data.aws_ecr_repository.ecr.arn,
2828
]
2929
}
3030

@@ -54,20 +54,20 @@ data "aws_iam_policy_document" "cicd_policy" {
5454
]
5555

5656
resources = [
57-
"${aws_iam_role.app_role.arn}",
58-
"${aws_iam_role.ecsTaskExecutionRole.arn}",
57+
aws_iam_role.app_role.arn,
58+
aws_iam_role.ecsTaskExecutionRole.arn,
5959
]
6060
}
6161
}
6262

6363
resource "aws_iam_user_policy" "cicd_user_policy" {
6464
name = "${var.app}_${var.environment}_cicd"
65-
user = "${aws_iam_user.cicd.name}"
66-
policy = "${data.aws_iam_policy_document.cicd_policy.json}"
65+
user = aws_iam_user.cicd.name
66+
policy = data.aws_iam_policy_document.cicd_policy.json
6767
}
6868

6969
data "aws_ecr_repository" "ecr" {
70-
name = "${var.app}"
70+
name = var.app
7171
}
7272

7373
# The AWS keys for the CICD user to use in a build system
@@ -77,5 +77,5 @@ output "cicd_keys" {
7777

7878
# The URL for the docker image repo in ECR
7979
output "docker_registry" {
80-
value = "${data.aws_ecr_repository.ecr.repository_url}"
80+
value = data.aws_ecr_repository.ecr.repository_url
8181
}

env/dev/ecs-event-stream.tf

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ resource "aws_cloudwatch_event_rule" "ecs_event_stream" {
1818
"clusterArn": ["${aws_ecs_cluster.app.arn}"]
1919
}
2020
}
21-
PATTERN
21+
22+
PATTERN
23+
2224
}
2325

2426
resource "aws_cloudwatch_event_target" "ecs_event_stream" {
25-
rule = "${aws_cloudwatch_event_rule.ecs_event_stream.name}"
26-
arn = "${aws_lambda_function.ecs_event_stream.arn}"
27+
rule = aws_cloudwatch_event_rule.ecs_event_stream.name
28+
arn = aws_lambda_function.ecs_event_stream.arn
2729
}
2830

2931
data "template_file" "lambda_source" {
@@ -32,44 +34,45 @@ exports.handler = (event, context, callback) => {
3234
console.log(JSON.stringify(event));
3335
}
3436
EOF
37+
3538
}
3639

3740
data "archive_file" "lambda_zip" {
38-
type = "zip"
39-
source_content = "${data.template_file.lambda_source.rendered}"
40-
source_content_filename = "index.js"
41-
output_path = "lambda-${var.app}.zip"
41+
type = "zip"
42+
source_content = data.template_file.lambda_source.rendered
43+
source_content_filename = "index.js"
44+
output_path = "lambda-${var.app}.zip"
4245
}
4346

4447
resource "aws_lambda_permission" "ecs_event_stream" {
45-
statement_id = "AllowExecutionFromCloudWatch"
46-
action = "lambda:InvokeFunction"
47-
function_name = "${aws_lambda_function.ecs_event_stream.arn}"
48-
principal = "events.amazonaws.com"
49-
source_arn = "${aws_cloudwatch_event_rule.ecs_event_stream.arn}"
48+
statement_id = "AllowExecutionFromCloudWatch"
49+
action = "lambda:InvokeFunction"
50+
function_name = aws_lambda_function.ecs_event_stream.arn
51+
principal = "events.amazonaws.com"
52+
source_arn = aws_cloudwatch_event_rule.ecs_event_stream.arn
5053
}
5154

5255
resource "aws_lambda_function" "ecs_event_stream" {
53-
function_name = "${var.app}-${var.environment}-ecs-event-stream"
54-
role = "${aws_iam_role.ecs_event_stream.arn}"
55-
filename = "${data.archive_file.lambda_zip.output_path}"
56-
source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
57-
handler = "index.handler"
58-
runtime = "nodejs8.10"
59-
tags = "${var.tags}"
56+
function_name = "${var.app}-${var.environment}-ecs-event-stream"
57+
role = aws_iam_role.ecs_event_stream.arn
58+
filename = data.archive_file.lambda_zip.output_path
59+
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
60+
handler = "index.handler"
61+
runtime = "nodejs8.10"
62+
tags = var.tags
6063
}
6164

6265
resource "aws_lambda_alias" "ecs_event_stream" {
63-
name = "${aws_lambda_function.ecs_event_stream.function_name}"
64-
description = "latest"
65-
function_name = "${aws_lambda_function.ecs_event_stream.function_name}"
66-
function_version = "$LATEST"
66+
name = aws_lambda_function.ecs_event_stream.function_name
67+
description = "latest"
68+
function_name = aws_lambda_function.ecs_event_stream.function_name
69+
function_version = "$LATEST"
6770
}
6871

6972
resource "aws_iam_role" "ecs_event_stream" {
70-
name = "${aws_cloudwatch_event_rule.ecs_event_stream.name}"
73+
name = aws_cloudwatch_event_rule.ecs_event_stream.name
7174

72-
assume_role_policy = <<EOF
75+
assume_role_policy = <<EOF
7376
{
7477
"Version": "2012-10-17",
7578
"Statement": [
@@ -84,18 +87,19 @@ resource "aws_iam_role" "ecs_event_stream" {
8487
]
8588
}
8689
EOF
90+
8791
}
8892

8993
resource "aws_iam_role_policy_attachment" "ecs_event_stream" {
90-
role = "${aws_iam_role.ecs_event_stream.name}"
91-
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
94+
role = aws_iam_role.ecs_event_stream.name
95+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
9296
}
9397

9498
# cloudwatch dashboard with logs insights query
9599
resource "aws_cloudwatch_dashboard" "ecs-event-stream" {
96-
dashboard_name = "${var.app}-${var.environment}-ecs-event-stream"
100+
dashboard_name = "${var.app}-${var.environment}-ecs-event-stream"
97101

98-
dashboard_body = <<EOF
102+
dashboard_body = <<EOF
99103
{
100104
"widgets": [
101105
{

0 commit comments

Comments
 (0)