From 9c4248ec2b6412002938a69756c39a3c067efd76 Mon Sep 17 00:00:00 2001 From: John Ritsema Date: Fri, 9 Aug 2019 13:19:11 -0400 Subject: [PATCH] upgrades to new terraform v0.12 syntax (#8) --- README.md | 2 +- base/ecr.tf | 9 +++--- base/main.tf | 12 +++++--- base/state.tf | 6 ++-- base/variables.tf | 11 ++++--- env/dev/autoscale-perf.tf | 33 ++++++++++---------- env/dev/autoscale-time.tf | 25 ++++++++-------- env/dev/cicd.tf | 17 ++++++----- env/dev/ecs.tf | 56 +++++++++++++++++----------------- env/dev/logs-logzio.tf | 63 +++++++++++++++++++++------------------ env/dev/main.tf | 12 ++++---- env/dev/nlb.tf | 29 +++++++++--------- env/dev/nsg.tf | 28 +++++++++-------- env/dev/role.tf | 15 ++++++---- env/dev/variables.tf | 28 +++++++++++------ 15 files changed, 191 insertions(+), 155 deletions(-) diff --git a/README.md b/README.md index db510d2..e8308cc 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ create an input vars file (`terraform.tfvars`) app = "my-app" environment = "dev" -internal = "true" +internal = true container_port = "8080" replicas = "1" region = "us-east-1" diff --git a/base/ecr.tf b/base/ecr.tf index 9569cab..c503cdf 100644 --- a/base/ecr.tf +++ b/base/ecr.tf @@ -6,15 +6,16 @@ # create an ECR repo at the app/image level resource "aws_ecr_repository" "app" { - name = "${var.app}" + name = var.app } -data "aws_caller_identity" "current" {} +data "aws_caller_identity" "current" { +} # grant access to saml users resource "aws_ecr_repository_policy" "app" { - repository = "${aws_ecr_repository.app.name}" - policy = "${data.aws_iam_policy_document.ecr.json}" + repository = aws_ecr_repository.app.name + policy = data.aws_iam_policy_document.ecr.json } data "aws_iam_policy_document" "ecr" { diff --git a/base/main.tf b/base/main.tf index aca9776..da275fd 100644 --- a/base/main.tf +++ b/base/main.tf @@ -1,3 +1,7 @@ +terraform { + required_version = ">= 0.12" +} + /** * main.tf * The main entry point for Terraform run @@ -9,8 +13,8 @@ # Using the AWS Provider # https://www.terraform.io/docs/providers/ provider "aws" { - region = "${var.region}" - profile = "${var.aws_profile}" + region = var.region + profile = var.aws_profile } /* @@ -21,10 +25,10 @@ provider "aws" { # Returns the name of the ECR registry, this will be used later in various scripts output "docker_registry" { - value = "${aws_ecr_repository.app.repository_url}" + value = aws_ecr_repository.app.repository_url } # Returns the name of the S3 bucket that will be used in later Terraform files output "bucket" { - value = "${module.tf_remote_state.bucket}" + value = module.tf_remote_state.bucket } diff --git a/base/state.tf b/base/state.tf index c3146ba..86c5f6e 100644 --- a/base/state.tf +++ b/base/state.tf @@ -12,7 +12,7 @@ module "tf_remote_state" { source = "github.com/turnerlabs/terraform-remote-state?ref=v2.2.0" - role = "${var.saml_role}" - application = "${var.app}" - tags = "${var.tags}" + role = var.saml_role + application = var.app + tags = var.tags } diff --git a/base/variables.tf b/base/variables.tf index 222a984..d7ef4d7 100644 --- a/base/variables.tf +++ b/base/variables.tf @@ -11,14 +11,17 @@ variable "region" { } # The AWS profile to use, this would be the same value used in AWS_PROFILE. -variable "aws_profile" {} +variable "aws_profile" { +} # The role that will have access to the S3 bucket, this should be a role that all # members of the team have access to. -variable "saml_role" {} +variable "saml_role" { +} # Name of the application. This value should usually match the application tag below. -variable "app" {} +variable "app" { +} # A map of the tags to apply to various resources. The required tags are: # `application`, name of the app; @@ -27,5 +30,5 @@ variable "app" {} # `contact-email`, contact email for the _team_; # and `customer`, who the application was create for. variable "tags" { - type = "map" + type = map(string) } diff --git a/env/dev/autoscale-perf.tf b/env/dev/autoscale-perf.tf index d177e66..8aee5a2 100644 --- a/env/dev/autoscale-perf.tf +++ b/env/dev/autoscale-perf.tf @@ -54,14 +54,14 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" { namespace = "AWS/ECS" period = "60" statistic = "Average" - threshold = "${var.ecs_as_cpu_high_threshold_per}" + threshold = var.ecs_as_cpu_high_threshold_per - dimensions { - ClusterName = "${aws_ecs_cluster.app.name}" - ServiceName = "${aws_ecs_service.app.name}" + dimensions = { + ClusterName = aws_ecs_cluster.app.name + ServiceName = aws_ecs_service.app.name } - alarm_actions = ["${aws_appautoscaling_policy.app_up.arn}"] + alarm_actions = [aws_appautoscaling_policy.app_up.arn] } resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" { @@ -72,21 +72,21 @@ resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" { namespace = "AWS/ECS" period = "60" statistic = "Average" - threshold = "${var.ecs_as_cpu_low_threshold_per}" + threshold = var.ecs_as_cpu_low_threshold_per - dimensions { - ClusterName = "${aws_ecs_cluster.app.name}" - ServiceName = "${aws_ecs_service.app.name}" + dimensions = { + ClusterName = aws_ecs_cluster.app.name + ServiceName = aws_ecs_service.app.name } - alarm_actions = ["${aws_appautoscaling_policy.app_down.arn}"] + alarm_actions = [aws_appautoscaling_policy.app_down.arn] } resource "aws_appautoscaling_policy" "app_up" { name = "app-scale-up" - service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}" - resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}" - scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}" + service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace + resource_id = aws_appautoscaling_target.app_scale_target.resource_id + scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension step_scaling_policy_configuration { adjustment_type = "ChangeInCapacity" @@ -102,9 +102,9 @@ resource "aws_appautoscaling_policy" "app_up" { resource "aws_appautoscaling_policy" "app_down" { name = "app-scale-down" - service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}" - resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}" - scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}" + service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace + resource_id = aws_appautoscaling_target.app_scale_target.resource_id + scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension step_scaling_policy_configuration { adjustment_type = "ChangeInCapacity" @@ -117,3 +117,4 @@ resource "aws_appautoscaling_policy" "app_down" { } } } + diff --git a/env/dev/autoscale-time.tf b/env/dev/autoscale-time.tf index 494d124..6c4e9f3 100644 --- a/env/dev/autoscale-time.tf +++ b/env/dev/autoscale-time.tf @@ -30,14 +30,14 @@ variable "scale_down_max_capacity" { resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_up" { name = "app-autoscale-time-up-${var.app}-${var.environment}" - service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}" - resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}" - scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}" - schedule = "${var.scale_up_cron}" + service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace + resource_id = aws_appautoscaling_target.app_scale_target.resource_id + scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension + schedule = var.scale_up_cron scalable_target_action { - min_capacity = "${aws_appautoscaling_target.app_scale_target.min_capacity}" - max_capacity = "${aws_appautoscaling_target.app_scale_target.max_capacity}" + min_capacity = aws_appautoscaling_target.app_scale_target.min_capacity + max_capacity = aws_appautoscaling_target.app_scale_target.max_capacity } } @@ -46,13 +46,14 @@ resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_up" { resource "aws_appautoscaling_scheduled_action" "app_autoscale_time_down" { name = "app-autoscale-time-down-${var.app}-${var.environment}" - service_namespace = "${aws_appautoscaling_target.app_scale_target.service_namespace}" - resource_id = "${aws_appautoscaling_target.app_scale_target.resource_id}" - scalable_dimension = "${aws_appautoscaling_target.app_scale_target.scalable_dimension}" - schedule = "${var.scale_down_cron}" + service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace + resource_id = aws_appautoscaling_target.app_scale_target.resource_id + scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension + schedule = var.scale_down_cron scalable_target_action { - min_capacity = "${var.scale_down_min_capacity}" - max_capacity = "${var.scale_down_max_capacity}" + min_capacity = var.scale_down_min_capacity + max_capacity = var.scale_down_max_capacity } } + diff --git a/env/dev/cicd.tf b/env/dev/cicd.tf index 61c2db9..5a09bfa 100644 --- a/env/dev/cicd.tf +++ b/env/dev/cicd.tf @@ -4,7 +4,7 @@ resource "aws_iam_user" "cicd" { } resource "aws_iam_access_key" "cicd_keys" { - user = "${aws_iam_user.cicd.name}" + user = aws_iam_user.cicd.name } # grant required permissions to deploy @@ -24,7 +24,7 @@ data "aws_iam_policy_document" "cicd_policy" { ] resources = [ - "${data.aws_ecr_repository.ecr.arn}", + data.aws_ecr_repository.ecr.arn, ] } @@ -54,20 +54,20 @@ data "aws_iam_policy_document" "cicd_policy" { ] resources = [ - "${aws_iam_role.app_role.arn}", - "${aws_iam_role.ecsTaskExecutionRole.arn}", + aws_iam_role.app_role.arn, + aws_iam_role.ecsTaskExecutionRole.arn, ] } } resource "aws_iam_user_policy" "cicd_user_policy" { name = "${var.app}_${var.environment}_cicd" - user = "${aws_iam_user.cicd.name}" - policy = "${data.aws_iam_policy_document.cicd_policy.json}" + user = aws_iam_user.cicd.name + policy = data.aws_iam_policy_document.cicd_policy.json } data "aws_ecr_repository" "ecr" { - name = "${var.app}" + name = var.app } # The AWS keys for the CICD user to use in a build system @@ -77,5 +77,6 @@ output "cicd_keys" { # The URL for the docker image repo in ECR output "docker_registry" { - value = "${data.aws_ecr_repository.ecr.repository_url}" + value = data.aws_ecr_repository.ecr.repository_url } + diff --git a/env/dev/ecs.tf b/env/dev/ecs.tf index 561064f..7bde7b3 100644 --- a/env/dev/ecs.tf +++ b/env/dev/ecs.tf @@ -38,7 +38,7 @@ variable "ecs_autoscale_max_instances" { resource "aws_ecs_cluster" "app" { name = "${var.app}-${var.environment}" - tags = "${var.tags}" + tags = var.tags } # The default docker image to deploy with the infrastructure. @@ -56,8 +56,8 @@ resource "aws_appautoscaling_target" "app_scale_target" { service_namespace = "ecs" resource_id = "service/${aws_ecs_cluster.app.name}/${aws_ecs_service.app.name}" scalable_dimension = "ecs:service:DesiredCount" - max_capacity = "${var.ecs_autoscale_max_instances}" - min_capacity = "${var.ecs_autoscale_min_instances}" + max_capacity = var.ecs_autoscale_max_instances + min_capacity = var.ecs_autoscale_min_instances } resource "aws_ecs_task_definition" "app" { @@ -66,10 +66,10 @@ resource "aws_ecs_task_definition" "app" { network_mode = "awsvpc" cpu = "256" memory = "512" - execution_role_arn = "${aws_iam_role.ecsTaskExecutionRole.arn}" + execution_role_arn = aws_iam_role.ecsTaskExecutionRole.arn # defined in role.tf - task_role_arn = "${aws_iam_role.app_role.arn}" + task_role_arn = aws_iam_role.app_role.arn container_definitions = <