Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform 0.12 Upgrade #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ jobs:
working_directory: ~/repo

docker:
- image: unifio/ci:3.0.622-ruby-2.5.3
- image: unifio/ci:5.0.945-ruby-2.5.5

environment:
AWS_REGION: 'us-east-2'
AWS_REGION: 'us-east-1'
CI_REPORTS: 'reports/infrastructure'
TF_PLUGIN_CACHE_DIR: "/root/.terraform.d/plugin-cache"

Expand Down
5 changes: 4 additions & 1 deletion .env.docker
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
AWS_REGION=us-east-2
AWS_REGION=us-east-1
AWS_PROFILE=unifiouat
AWS_DEFAULT_PROFILE=unifiouat
AWS_DEFAULT_REGION=us-east-1
COVALENCE_LOG=info
COVALENCE_TEST_ENVS=basic,complete
CHECKPOINT_DISABLE=1
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#### IMPROVEMENTS / NEW FEATURES:
* Add support for application auto scaling

## 0.4.0 (May 28, 2020)

#### IMPROVEMENTS / NEW FEATURES:
* Updated for Terraform v0.12

#### BACKWARDS INCOMPATIBILITIES / NOTES:
* Terraform versions earlier than 0.12.0 no longer supported.

## 0.3.3 (March 10, 2019)

#### BACKWARDS INCOMPATIBILITIES / NOTES:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Terraform module for the deployment of an AWS Elastic Container Service (ECS) cl

## Requirements ##

- Terraform 0.11.0 or newer
- Terraform 0.12.0 or newer
- AWS provider

## Cluster module ##
Expand Down
29 changes: 15 additions & 14 deletions cluster/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ data "aws_iam_policy_document" "agent_policy" {
}

resource "aws_iam_role" "agent_role" {
assume_role_policy = "${data.aws_iam_policy_document.agent_policy.json}"
assume_role_policy = data.aws_iam_policy_document.agent_policy.json
name = "ecs-agent-${var.cluster_label}-${var.stack_item_label}-${data.aws_region.current.name}"
path = "${var.iam_path}"
path = var.iam_path
}

resource "aws_iam_instance_profile" "agent_profile" {
name = "ecs-agent-${var.cluster_label}-${var.stack_item_label}-${data.aws_region.current.name}"
path = "${var.iam_path}"
role = "${aws_iam_role.agent_role.name}"
path = var.iam_path
role = aws_iam_role.agent_role.name
}

### Creates monitoring policy
Expand All @@ -40,8 +40,8 @@ data "aws_iam_policy_document" "monitoring_policy" {

resource "aws_iam_role_policy" "monitoring_policy" {
name = "monitoring"
policy = "${data.aws_iam_policy_document.monitoring_policy.json}"
role = "${aws_iam_role.agent_role.id}"
policy = data.aws_iam_policy_document.monitoring_policy.json
role = aws_iam_role.agent_role.id
}

### Creates resource tagging policy
Expand All @@ -55,8 +55,8 @@ data "aws_iam_policy_document" "tagging_policy" {

resource "aws_iam_role_policy" "tagging_policy" {
name = "tagging"
policy = "${data.aws_iam_policy_document.tagging_policy.json}"
role = "${aws_iam_role.agent_role.id}"
policy = data.aws_iam_policy_document.tagging_policy.json
role = aws_iam_role.agent_role.id
}

### Creates Elastic Container Service (ECS) service policy
Expand All @@ -80,13 +80,13 @@ data "aws_iam_policy_document" "ecs_policy" {

resource "aws_iam_role_policy" "ecs_policy" {
name = "ecs"
policy = "${data.aws_iam_policy_document.ecs_policy.json}"
role = "${aws_iam_role.agent_role.id}"
policy = data.aws_iam_policy_document.ecs_policy.json
role = aws_iam_role.agent_role.id
}

### Creates Simple Storage Service (S3) policy for logging buckets
data "aws_iam_policy_document" "logging_policy" {
count = "${var.logs_bucket_enabled == "true" ? "1" : "0"}"
count = var.logs_bucket_enabled == "true" ? "1" : "0"

statement {
actions = ["s3:ListBucket"]
Expand All @@ -106,9 +106,10 @@ data "aws_iam_policy_document" "logging_policy" {
}

resource "aws_iam_role_policy" "logging_policy" {
count = "${var.logs_bucket_enabled == "true" ? "1" : "0"}"
count = var.logs_bucket_enabled == "true" ? "1" : "0"

name = "logging"
policy = "${data.aws_iam_policy_document.logging_policy.json}"
role = "${aws_iam_role.agent_role.id}"
policy = data.aws_iam_policy_document.logging_policy[0].json
role = aws_iam_role.agent_role.id
}

146 changes: 73 additions & 73 deletions cluster/main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Elastic Container Service (ECS) cluster

## Set Terraform version constraint
terraform {
required_version = "> 0.11.0"
data "aws_region" "current" {
}

data "aws_region" "current" {}

## Creates cloud-config data for agent cluster
data "template_file" "user_data" {
template = "${var.user_data_override != "" ? "" : file("${path.module}/templates/user_data.hcl")}"
template = var.user_data_override != "" ? "" : file("${path.module}/templates/user_data.hcl")

vars {
cluster_label = "${var.cluster_label}"
stack_item_label = "${var.stack_item_label}"
vars = {
cluster_label = var.cluster_label
stack_item_label = var.stack_item_label
}
}

Expand Down Expand Up @@ -44,66 +40,69 @@ data "aws_ami" "ecs_ami" {
}

module "cluster" {
source = "github.com/unifio/terraform-aws-asg?ref=v0.3.7//group"
source = "github.com/unifio/terraform-aws-asg?ref=upgrade-0.12//group"

# Resource tags
stack_item_fullname = "${var.stack_item_fullname}"
stack_item_fullname = var.stack_item_fullname
stack_item_label = "${var.cluster_label}-${var.stack_item_label}"

# VPC parameters
subnets = ["${var.subnets}"]
vpc_id = "${var.vpc_id}"
subnets = var.subnets
vpc_id = var.vpc_id

# LC parameters
ami = "${coalesce(var.ami_override,data.aws_ami.ecs_ami.id)}"
associate_public_ip_address = "${var.associate_public_ip_address}"
ebs_optimized = "${var.ebs_optimized}"
ebs_vol_del_on_term = "${var.ebs_vol_del_on_term}"
ebs_vol_device_name = "${var.ebs_vol_device_name}"
ebs_vol_encrypted = "${var.ebs_vol_encrypted}"
ebs_vol_iops = "${var.ebs_vol_iops}"
ebs_vol_size = "${var.ebs_vol_size}"
ebs_vol_snapshot_id = "${var.ebs_vol_snapshot_id}"
ebs_vol_type = "${var.ebs_vol_type}"
enable_monitoring = "${var.enable_monitoring}"
instance_based_naming_enabled = "${var.instance_based_naming_enabled}"
instance_name_prefix = "${var.instance_name_prefix}"
instance_profile = "${aws_iam_instance_profile.agent_profile.id}"
instance_tags = "${var.instance_tags}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
placement_tenancy = "${var.placement_tenancy}"
root_vol_del_on_term = "${var.root_vol_del_on_term}"
root_vol_iops = "${var.root_vol_iops}"
root_vol_size = "${var.root_vol_size}"
root_vol_type = "${var.root_vol_type}"
security_groups = ["${distinct(concat(list(module.consul.sg_id), compact(var.security_groups)))}"]
spot_price = "${var.spot_price}"
user_data = "${coalesce(var.user_data_override,data.template_file.user_data.rendered)}"
ami = coalesce(var.ami_override, data.aws_ami.ecs_ami.id)
associate_public_ip_address = var.associate_public_ip_address
ebs_optimized = var.ebs_optimized
ebs_vol_del_on_term = var.ebs_vol_del_on_term
ebs_vol_device_name = var.ebs_vol_device_name
ebs_vol_encrypted = var.ebs_vol_encrypted
ebs_vol_iops = var.ebs_vol_iops
ebs_vol_size = var.ebs_vol_size
ebs_vol_snapshot_id = var.ebs_vol_snapshot_id
ebs_vol_type = var.ebs_vol_type
enable_monitoring = var.enable_monitoring
instance_based_naming_enabled = var.instance_based_naming_enabled
instance_name_prefix = var.instance_name_prefix
instance_profile = aws_iam_instance_profile.agent_profile.id
instance_tags = var.instance_tags
instance_type = var.instance_type
key_name = var.key_name
placement_tenancy = var.placement_tenancy
root_vol_del_on_term = var.root_vol_del_on_term
root_vol_iops = var.root_vol_iops
root_vol_size = var.root_vol_size
root_vol_type = var.root_vol_type
security_groups = distinct(concat([module.consul.sg_id], compact(var.security_groups)))
spot_price = var.spot_price
user_data = coalesce(
var.user_data_override,
data.template_file.user_data.rendered,
)

# ASG parameters
default_cooldown = "${var.default_cooldown}"
desired_capacity = "${var.desired_capacity}"
enabled_metrics = ["${var.enabled_metrics}"]
force_delete = "${var.force_delete}"
hc_check_type = "${var.hc_check_type}"
hc_grace_period = "${var.hc_grace_period}"
max_size = "${var.max_size}"
min_size = "${var.min_size}"
placement_group = "${var.placement_group}"
protect_from_scale_in = "${var.protect_from_scale_in}"
suspended_processes = ["${var.suspended_processes}"]
target_group_arns = ["${var.target_group_arns}"]
termination_policies = ["${var.termination_policies}"]
wait_for_capacity_timeout = "${var.wait_for_capacity_timeout}"
default_cooldown = var.default_cooldown
desired_capacity = var.desired_capacity
enabled_metrics = var.enabled_metrics
force_delete = var.force_delete
hc_check_type = var.hc_check_type
hc_grace_period = var.hc_grace_period
max_size = var.max_size
min_size = var.min_size
placement_group = var.placement_group
protect_from_scale_in = var.protect_from_scale_in
suspended_processes = var.suspended_processes
target_group_arns = var.target_group_arns
termination_policies = var.termination_policies
wait_for_capacity_timeout = var.wait_for_capacity_timeout
}

## Updates security groups
resource "aws_security_group_rule" "agent_egress" {
cidr_blocks = ["0.0.0.0/0"]
from_port = 0
protocol = -1
security_group_id = "${module.cluster.sg_id}"
security_group_id = module.cluster.sg_id
to_port = 0
type = "egress"
}
Expand All @@ -118,31 +117,32 @@ module "consul" {
source = "../consul"

# Resource tags
stack_item_fullname = "${var.stack_item_fullname}"
stack_item_fullname = var.stack_item_fullname
stack_item_label = "${var.cluster_label}-${var.stack_item_label}"

# ECS parameters
cluster_id = "${aws_ecs_cluster.cluster.id}"
cluster_name = "${aws_ecs_cluster.cluster.name}"
cluster_sg_id = "${module.cluster.sg_id}"
iam_path = "${var.iam_path}"
vpc_id = "${var.vpc_id}"
cluster_id = aws_ecs_cluster.cluster.id
cluster_name = aws_ecs_cluster.cluster.name
cluster_sg_id = module.cluster.sg_id
iam_path = var.iam_path
vpc_id = var.vpc_id

# Service discovery parameters
## TODO: Enable for auto scaling

agent_config_override = "${var.agent_config_override}"
agent_desired_count = "${((length(var.desired_capacity) > 0 ? var.desired_capacity : var.min_size) - var.server_desired_count) >= 0 ? (var.min_size - var.server_desired_count) : "0"}"
agent_task_arn_override = "${var.agent_task_arn_override}"
consul_dc = "${var.consul_dc}"
consul_docker_image = "${var.consul_docker_image}"
registrator_config_override = "${var.registrator_config_override}"
registrator_desired_count = "${length(var.desired_capacity) > 0 ? var.desired_capacity : var.min_size}"
registrator_docker_image = "${var.registrator_docker_image}"
registrator_task_arn_override = "${var.registrator_task_arn_override}"
server_config_override = "${var.server_config_override}"
server_desired_count = "${var.server_desired_count}"
server_task_arn_override = "${var.server_task_arn_override}"
service_discovery_enabled = "${(var.min_size - var.server_desired_count) < 0 ? "false" : var.service_discovery_enabled}"
service_registration_enabled = "${var.service_registration_enabled}"
agent_config_override = var.agent_config_override
agent_desired_count = length(var.desired_capacity) > 0 ? var.desired_capacity : var.min_size - var.server_desired_count >= 0 ? var.min_size - var.server_desired_count : "0"
agent_task_arn_override = var.agent_task_arn_override
consul_dc = var.consul_dc
consul_docker_image = var.consul_docker_image
registrator_config_override = var.registrator_config_override
registrator_desired_count = length(var.desired_capacity) > 0 ? var.desired_capacity : var.min_size
registrator_docker_image = var.registrator_docker_image
registrator_task_arn_override = var.registrator_task_arn_override
server_config_override = var.server_config_override
server_desired_count = var.server_desired_count
server_task_arn_override = var.server_task_arn_override
service_discovery_enabled = var.min_size - var.server_desired_count < 0 ? "false" : var.service_discovery_enabled
service_registration_enabled = var.service_registration_enabled
}

13 changes: 7 additions & 6 deletions cluster/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
# Outputs

output "agent_role_id" {
value = "${aws_iam_role.agent_role.id}"
value = aws_iam_role.agent_role.id
}

output "cluster_id" {
value = "${aws_ecs_cluster.cluster.id}"
value = aws_ecs_cluster.cluster.id
}

output "cluster_name" {
value = "${aws_ecs_cluster.cluster.name}"
value = aws_ecs_cluster.cluster.name
}

output "consul_sg_id" {
value = "${module.consul.sg_id}"
value = module.consul.sg_id
}

output "consul_target_group_arn" {
value = "${module.consul.target_group_arn}"
value = module.consul.target_group_arn
}

output "sg_id" {
value = "${module.cluster.sg_id}"
value = module.cluster.sg_id
}

Loading