Skip to content

Commit

Permalink
upgrades to new terraform v0.12 syntax (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
jritsema authored Aug 9, 2019
1 parent f3cdfac commit 9c4248e
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 155 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 5 additions & 4 deletions base/ecr.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
12 changes: 8 additions & 4 deletions base/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
terraform {
required_version = ">= 0.12"
}

/**
* main.tf
* The main entry point for Terraform run
Expand All @@ -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
}

/*
Expand All @@ -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
}
6 changes: 3 additions & 3 deletions base/state.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 7 additions & 4 deletions base/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
}
33 changes: 17 additions & 16 deletions env/dev/autoscale-perf.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -117,3 +117,4 @@ resource "aws_appautoscaling_policy" "app_down" {
}
}
}

25 changes: 13 additions & 12 deletions env/dev/autoscale-time.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

17 changes: 9 additions & 8 deletions env/dev/cicd.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,7 @@ data "aws_iam_policy_document" "cicd_policy" {
]

resources = [
"${data.aws_ecr_repository.ecr.arn}",
data.aws_ecr_repository.ecr.arn,
]
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}

56 changes: 28 additions & 28 deletions env/dev/ecs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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" {
Expand All @@ -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 = <<DEFINITION
[
Expand Down Expand Up @@ -114,67 +114,67 @@ resource "aws_ecs_task_definition" "app" {
]
DEFINITION

tags = "${var.tags}"

tags = var.tags
}

resource "aws_ecs_service" "app" {
name = "${var.app}-${var.environment}"
cluster = "${aws_ecs_cluster.app.id}"
launch_type = "FARGATE"
task_definition = "${aws_ecs_task_definition.app.arn}"
desired_count = "${var.replicas}"
name = "${var.app}-${var.environment}"
cluster = aws_ecs_cluster.app.id
launch_type = "FARGATE"
task_definition = aws_ecs_task_definition.app.arn
desired_count = var.replicas

network_configuration {
security_groups = ["${aws_security_group.nsg_task.id}"]
subnets = ["${split(",", var.private_subnets)}"]
security_groups = [aws_security_group.nsg_task.id]
subnets = split(",", var.private_subnets)
}

load_balancer {
target_group_arn = "${aws_lb_target_group.main.id}"
container_name = "${var.container_name}"
container_port = "${var.container_port}"
target_group_arn = aws_lb_target_group.main.id
container_name = var.container_name
container_port = var.container_port
}

tags = "${var.tags}"
tags = var.tags
enable_ecs_managed_tags = true
propagate_tags = "SERVICE"
propagate_tags = "SERVICE"

# workaround for https://github.com/hashicorp/terraform/issues/12634
depends_on = [
"aws_lb_listener.tcp",
]
depends_on = [aws_lb_listener.tcp]

# [after initial apply] don't override changes made to task_definition
# from outside of terrraform (i.e.; fargate cli)
lifecycle {
ignore_changes = ["task_definition"]
ignore_changes = [task_definition]
}
}

# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_execution_IAM_role.html
resource "aws_iam_role" "ecsTaskExecutionRole" {
name = "${var.app}-${var.environment}-ecs"
assume_role_policy = "${data.aws_iam_policy_document.assume_role_policy.json}"
name = "${var.app}-${var.environment}-ecs"
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}

data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}

resource "aws_iam_role_policy_attachment" "ecsTaskExecutionRole_policy" {
role = "${aws_iam_role.ecsTaskExecutionRole.name}"
role = aws_iam_role.ecsTaskExecutionRole.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}

resource "aws_cloudwatch_log_group" "logs" {
name = "/fargate/service/${var.app}-${var.environment}"
name = "/fargate/service/${var.app}-${var.environment}"
retention_in_days = "14"
tags = "${var.tags}"
tags = var.tags
}

Loading

0 comments on commit 9c4248e

Please sign in to comment.