Skip to content

Commit

Permalink
Updated Terraform 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
antonbabenko committed Jun 9, 2019
1 parent 820e5f5 commit 4612040
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 27 additions & 26 deletions examples/complete-ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ provider "aws" {
region = "eu-west-1"
}

provider "terraform" {}

locals {
name = "complete-ecs"
environment = "dev"
Expand All @@ -14,40 +12,40 @@ locals {

module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 2.0"

name = "${local.name}"
name = local.name

cidr = "10.1.0.0/16"

azs = ["eu-west-1a", "eu-west-1b"]
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--------
Expand All @@ -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"]
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions examples/complete-ecs/service-hello-world/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions examples/complete-ecs/service-hello-world/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
variable "cluster_id" {
description = "The ECS cluster ID"
type = string
}
14 changes: 3 additions & 11 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion modules/ecs-instance-profile/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
output "this_iam_instance_profile_id" {
value = "${aws_iam_instance_profile.this.id}"
value = aws_iam_instance_profile.this.id
}
1 change: 1 addition & 0 deletions modules/ecs-instance-profile/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
variable "name" {
description = "Name to be used on all the resources as identifier"
type = string
}
6 changes: 3 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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 = {}
}

0 comments on commit 4612040

Please sign in to comment.