From 4612040e773505a68952f2d85a3c65fbbe9d8362 Mon Sep 17 00:00:00 2001 From: Anton Babenko Date: Sun, 9 Jun 2019 17:01:19 +0200 Subject: [PATCH] Updated Terraform 0.12 --- .pre-commit-config.yaml | 6 +-- README.md | 3 +- examples/complete-ecs/main.tf | 53 ++++++++++--------- .../complete-ecs/service-hello-world/main.tf | 4 +- .../service-hello-world/variables.tf | 1 + main.tf | 14 ++--- .../{instance-policy.tf => main.tf} | 6 +-- modules/ecs-instance-profile/outputs.tf | 2 +- modules/ecs-instance-profile/variables.tf | 1 + outputs.tf | 6 +-- variables.tf | 3 ++ 11 files changed, 49 insertions(+), 50 deletions(-) rename modules/ecs-instance-profile/{instance-policy.tf => main.tf} (85%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6269b3b..f0e6b77 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,10 @@ repos: - repo: git://github.com/antonbabenko/pre-commit-terraform - rev: v1.11.0 + rev: v1.12.0 hooks: - id: terraform_fmt - - id: terraform_docs +# - id: terraform_docs - repo: git://github.com/pre-commit/pre-commit-hooks - rev: v2.1.0 + rev: v2.2.3 hooks: - id: check-merge-conflict diff --git a/README.md b/README.md index 321081e..cde67a4 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,8 @@ Sometimes you need to have a way to create ECS resources conditionally but Terra ```hcl # ECS cluster will not be created module "ecs" { - source = "terraform-aws-modules/ecs/aws" + source = "terraform-aws-modules/ecs/aws" + version = "~> 2.0" create_ecs = false # ... omitted diff --git a/examples/complete-ecs/main.tf b/examples/complete-ecs/main.tf index d383ec9..151140b 100644 --- a/examples/complete-ecs/main.tf +++ b/examples/complete-ecs/main.tf @@ -2,8 +2,6 @@ provider "aws" { region = "eu-west-1" } -provider "terraform" {} - locals { name = "complete-ecs" environment = "dev" @@ -14,8 +12,9 @@ locals { module "vpc" { source = "terraform-aws-modules/vpc/aws" + version = "~> 2.0" - name = "${local.name}" + name = local.name cidr = "10.1.0.0/16" @@ -23,31 +22,30 @@ module "vpc" { private_subnets = ["10.1.1.0/24", "10.1.2.0/24"] public_subnets = ["10.1.11.0/24", "10.1.12.0/24"] - enable_nat_gateway = true - single_nat_gateway = true + enable_nat_gateway = false # this is faster, but should be "true" for real tags = { - Environment = "${local.environment}" - Name = "${local.name}" + Environment = local.environment + Name = local.name } } #----- ECS -------- module "ecs" { source = "../../" - name = "${local.name}" + name = local.name } module "ec2-profile" { source = "../../modules/ecs-instance-profile" - name = "${local.name}" + name = local.name } #----- ECS Services-------- module "hello-world" { - source = "service-hello-world" - cluster_id = "${module.ecs.this_ecs_cluster_id}" + source = "./service-hello-world" + cluster_id = module.ecs.this_ecs_cluster_id } #----- ECS Resources-------- @@ -56,6 +54,8 @@ module "hello-world" { data "aws_ami" "amazon_linux_ecs" { most_recent = true + owners = ["amazon"] + filter { name = "name" values = ["amzn-ami-*-amazon-ecs-optimized"] @@ -68,46 +68,47 @@ data "aws_ami" "amazon_linux_ecs" { } module "this" { - source = "terraform-aws-modules/autoscaling/aws" + source = "terraform-aws-modules/autoscaling/aws" + version = "~> 3.0" - name = "${local.ec2_resources_name}" + name = local.ec2_resources_name # Launch configuration - lc_name = "${local.ec2_resources_name}" + lc_name = local.ec2_resources_name - image_id = "${data.aws_ami.amazon_linux_ecs.id}" + image_id = data.aws_ami.amazon_linux_ecs.id instance_type = "t2.micro" - security_groups = ["${module.vpc.default_security_group_id}"] - iam_instance_profile = "${module.ec2-profile.this_iam_instance_profile_id}" - user_data = "${data.template_file.user_data.rendered}" + security_groups = [module.vpc.default_security_group_id] + iam_instance_profile = module.ec2-profile.this_iam_instance_profile_id + user_data = data.template_file.user_data.rendered # Auto scaling group - asg_name = "${local.ec2_resources_name}" - vpc_zone_identifier = "${module.vpc.private_subnets}" + asg_name = local.ec2_resources_name + vpc_zone_identifier = module.vpc.private_subnets health_check_type = "EC2" min_size = 0 max_size = 1 - desired_capacity = 1 + desired_capacity = 0 wait_for_capacity_timeout = 0 tags = [ { key = "Environment" - value = "${local.environment}" + value = local.environment propagate_at_launch = true }, { key = "Cluster" - value = "${local.name}" + value = local.name propagate_at_launch = true }, ] } data "template_file" "user_data" { - template = "${file("${path.module}/templates/user-data.sh")}" + template = file("${path.module}/templates/user-data.sh") - vars { - cluster_name = "${local.name}" + vars = { + cluster_name = local.name } } diff --git a/examples/complete-ecs/service-hello-world/main.tf b/examples/complete-ecs/service-hello-world/main.tf index c53d486..91a2953 100644 --- a/examples/complete-ecs/service-hello-world/main.tf +++ b/examples/complete-ecs/service-hello-world/main.tf @@ -28,8 +28,8 @@ EOF resource "aws_ecs_service" "hello_world" { name = "hello_world" - cluster = "${var.cluster_id}" - task_definition = "${aws_ecs_task_definition.hello_world.arn}" + cluster = var.cluster_id + task_definition = aws_ecs_task_definition.hello_world.arn desired_count = 1 diff --git a/examples/complete-ecs/service-hello-world/variables.tf b/examples/complete-ecs/service-hello-world/variables.tf index 024d0ef..848d4da 100644 --- a/examples/complete-ecs/service-hello-world/variables.tf +++ b/examples/complete-ecs/service-hello-world/variables.tf @@ -1,3 +1,4 @@ variable "cluster_id" { description = "The ECS cluster ID" + type = string } diff --git a/main.tf b/main.tf index 08b4e02..314f1bb 100644 --- a/main.tf +++ b/main.tf @@ -1,14 +1,6 @@ -terraform { - required_version = ">= 0.11.7" -} - -provider "template" { - version = ">= 1.0.0" -} - resource "aws_ecs_cluster" "this" { - count = "${var.create_ecs ? 1 : 0}" + count = var.create_ecs ? 1 : 0 - name = "${var.name}" - tags = "${var.tags}" + name = var.name + tags = var.tags } diff --git a/modules/ecs-instance-profile/instance-policy.tf b/modules/ecs-instance-profile/main.tf similarity index 85% rename from modules/ecs-instance-profile/instance-policy.tf rename to modules/ecs-instance-profile/main.tf index 4081fab..45f8940 100644 --- a/modules/ecs-instance-profile/instance-policy.tf +++ b/modules/ecs-instance-profile/main.tf @@ -20,15 +20,15 @@ EOF resource "aws_iam_instance_profile" "this" { name = "${var.name}_ecs_instance_profile" - role = "${aws_iam_role.this.name}" + role = aws_iam_role.this.name } resource "aws_iam_role_policy_attachment" "ecs_ec2_role" { - role = "${aws_iam_role.this.id}" + role = aws_iam_role.this.id policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role" } resource "aws_iam_role_policy_attachment" "ecs_ec2_cloudwatch_role" { - role = "${aws_iam_role.this.id}" + role = aws_iam_role.this.id policy_arn = "arn:aws:iam::aws:policy/CloudWatchLogsFullAccess" } diff --git a/modules/ecs-instance-profile/outputs.tf b/modules/ecs-instance-profile/outputs.tf index 4c79e89..b5ae2ce 100644 --- a/modules/ecs-instance-profile/outputs.tf +++ b/modules/ecs-instance-profile/outputs.tf @@ -1,3 +1,3 @@ output "this_iam_instance_profile_id" { - value = "${aws_iam_instance_profile.this.id}" + value = aws_iam_instance_profile.this.id } diff --git a/modules/ecs-instance-profile/variables.tf b/modules/ecs-instance-profile/variables.tf index ea24778..d54028f 100644 --- a/modules/ecs-instance-profile/variables.tf +++ b/modules/ecs-instance-profile/variables.tf @@ -1,3 +1,4 @@ variable "name" { description = "Name to be used on all the resources as identifier" +type = string } diff --git a/outputs.tf b/outputs.tf index c9caa6c..cd2d25a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,12 +1,12 @@ output "this_ecs_cluster_id" { - value = "${element(concat(aws_ecs_cluster.this.*.id, list("")), 0)}" + value = concat(aws_ecs_cluster.this.*.id, [""])[0] } output "this_ecs_cluster_arn" { - value = "${element(concat(aws_ecs_cluster.this.*.arn, list("")), 0)}" + value = concat(aws_ecs_cluster.this.*.arn, [""])[0] } output "this_ecs_cluster_name" { description = "The name of the ECS cluster" - value = "${var.name}" + value = var.name } diff --git a/variables.tf b/variables.tf index 4ca3eea..1797931 100644 --- a/variables.tf +++ b/variables.tf @@ -1,13 +1,16 @@ variable "create_ecs" { description = "Controls if ECS should be created" + type = bool default = true } variable "name" { description = "Name to be used on all the resources as identifier, also the name of the ECS cluster" + type = string } variable "tags" { description = "A map of tags to add to ECS Cluster" + type = map(string) default = {} }