Skip to content
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
79 changes: 53 additions & 26 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@
# https://www.terraform.io/docs/providers/aws/r/codedeploy_app.html
resource "aws_codedeploy_app" "default" {
compute_platform = "ECS"
name = "${var.name}"
name = var.name

tags = merge(
{
"Name" = local.iam_name
},
var.tags,
)
}

# https://www.terraform.io/docs/providers/aws/r/codedeploy_deployment_group.html
resource "aws_codedeploy_deployment_group" "default" {
app_name = "${aws_codedeploy_app.default.name}"
deployment_group_name = "${var.name}"
service_role_arn = "${aws_iam_role.default.arn}"
app_name = aws_codedeploy_app.default.name
deployment_group_name = var.name
service_role_arn = aws_iam_role.default.arn
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"

# You can configure a deployment group or deployment to automatically roll back when a deployment fails or when a
# monitoring threshold you specify is met. In this case, the last known good version of an application revision is deployed.
# https://docs.aws.amazon.com/codedeploy/latest/userguide/deployment-groups-configure-advanced-options.html
auto_rollback_configuration {
# If you enable automatic rollback, you must specify at least one event type.
enabled = "${var.auto_rollback_enabled}"
enabled = var.auto_rollback_enabled

# The event type or types that trigger a rollback. Supported types are DEPLOYMENT_FAILURE and DEPLOYMENT_STOP_ON_ALARM.
events = ["${var.auto_rollback_events}"]
events = var.auto_rollback_events
}

# You can configure options for a blue/green deployment.
Expand All @@ -38,12 +45,12 @@ resource "aws_codedeploy_deployment_group" "default" {
# - STOP_DEPLOYMENT: Do not register new instances with a load balancer unless traffic rerouting is started
# using ContinueDeployment. If traffic rerouting is not started before the end of the specified
# wait period, the deployment status is changed to Stopped.
action_on_timeout = "${var.action_on_timeout}"
action_on_timeout = var.action_on_timeout

# The number of minutes to wait before the status of a blue/green deployment is changed to Stopped
# if rerouting is not started manually. Applies only to the STOP_DEPLOYMENT option for action_on_timeout.
# Can not be set to STOP_DEPLOYMENT when timeout is set to 0 minutes.
wait_time_in_minutes = "${var.wait_time_in_minutes}"
wait_time_in_minutes = var.wait_time_in_minutes
}

# You can configure how instances in the original environment are terminated when a blue/green deployment is successful.
Expand All @@ -55,7 +62,7 @@ resource "aws_codedeploy_deployment_group" "default" {

# The number of minutes to wait after a successful blue/green deployment before terminating instances
# from the original environment. The maximum setting is 2880 minutes (2 days).
termination_wait_time_in_minutes = "${var.termination_wait_time_in_minutes}"
termination_wait_time_in_minutes = var.termination_wait_time_in_minutes
}
}

Expand All @@ -67,8 +74,8 @@ resource "aws_codedeploy_deployment_group" "default" {

# Configuration block(s) of the ECS services for a deployment group.
ecs_service {
cluster_name = "${var.ecs_cluster_name}"
service_name = "${var.ecs_service_name}"
cluster_name = var.ecs_cluster_name
service_name = var.ecs_service_name
}

# You can configure the Load Balancer to use in a deployment.
Expand All @@ -79,26 +86,34 @@ resource "aws_codedeploy_deployment_group" "default" {
target_group_pair_info {
# The path used by a load balancer to route production traffic when an Amazon ECS deployment is complete.
prod_traffic_route {
listener_arns = ["${var.lb_listener_arns}"]
listener_arns = var.lb_listener_arns
}

# One pair of target groups. One is associated with the original task set.
# The second target is associated with the task set that serves traffic after the deployment completes.
target_group {
name = "${var.blue_lb_target_group_name}"
name = var.blue_lb_target_group_name
}

target_group {
name = "${var.green_lb_target_group_name}"
name = var.green_lb_target_group_name
}

# An optional path used by a load balancer to route test traffic after an Amazon ECS deployment.
# Validation can happen while test traffic is served during a deployment.
test_traffic_route {
listener_arns = ["${var.test_traffic_route_listener_arns}"]
listener_arns = var.test_traffic_route_listener_arns
}
}
}

tags = merge(
{
"Name" = local.iam_name
},
var.tags,
)

}

# ECS AWS CodeDeploy IAM Role
Expand All @@ -107,11 +122,16 @@ resource "aws_codedeploy_deployment_group" "default" {

# https://www.terraform.io/docs/providers/aws/r/iam_role.html
resource "aws_iam_role" "default" {
name = "${local.iam_name}"
assume_role_policy = "${data.aws_iam_policy_document.assume_role_policy.json}"
path = "${var.iam_path}"
description = "${var.description}"
tags = "${merge(map("Name", local.iam_name), var.tags)}"
name = local.iam_name
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
path = var.iam_path
description = var.description
tags = merge(
{
"Name" = local.iam_name
},
var.tags,
)
}

data "aws_iam_policy_document" "assume_role_policy" {
Expand All @@ -127,10 +147,16 @@ data "aws_iam_policy_document" "assume_role_policy" {

# https://www.terraform.io/docs/providers/aws/r/iam_policy.html
resource "aws_iam_policy" "default" {
name = "${local.iam_name}"
policy = "${data.aws_iam_policy_document.policy.json}"
path = "${var.iam_path}"
description = "${var.description}"
name = local.iam_name
policy = data.aws_iam_policy_document.policy.json
path = var.iam_path
description = var.description
tags = merge(
{
"Name" = local.iam_name
},
var.tags,
)
}

data "aws_iam_policy_document" "policy" {
Expand Down Expand Up @@ -216,10 +242,11 @@ data "aws_iam_policy_document" "policy" {

# https://www.terraform.io/docs/providers/aws/r/iam_role_policy_attachment.html
resource "aws_iam_role_policy_attachment" "default" {
role = "${aws_iam_role.default.name}"
policy_arn = "${aws_iam_policy.default.arn}"
role = aws_iam_role.default.name
policy_arn = aws_iam_policy.default.arn
}

locals {
iam_name = "${var.name}-ecs-codedeploy"
}

29 changes: 15 additions & 14 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,69 +1,70 @@
output "codedeploy_app_id" {
value = "${aws_codedeploy_app.default.id}"
value = aws_codedeploy_app.default.id
description = "Amazon's assigned ID for the application."
}

output "codedeploy_app_name" {
value = "${aws_codedeploy_app.default.name}"
value = aws_codedeploy_app.default.name
description = "The application's name."
}

output "codedeploy_deployment_group_id" {
value = "${aws_codedeploy_deployment_group.default.id}"
value = aws_codedeploy_deployment_group.default.id
description = "Application name and deployment group name."
}

output "iam_role_arn" {
value = "${aws_iam_role.default.arn}"
value = aws_iam_role.default.arn
description = "The Amazon Resource Name (ARN) specifying the IAM Role."
}

output "iam_role_create_date" {
value = "${aws_iam_role.default.create_date}"
value = aws_iam_role.default.create_date
description = "The creation date of the IAM Role."
}

output "iam_role_unique_id" {
value = "${aws_iam_role.default.unique_id}"
value = aws_iam_role.default.unique_id
description = "The stable and unique string identifying the IAM Role."
}

output "iam_role_name" {
value = "${aws_iam_role.default.name}"
value = aws_iam_role.default.name
description = "The name of the IAM Role."
}

output "iam_role_description" {
value = "${aws_iam_role.default.description}"
value = aws_iam_role.default.description
description = "The description of the IAM Role."
}

output "iam_policy_id" {
value = "${aws_iam_policy.default.id}"
value = aws_iam_policy.default.id
description = "The IAM Policy's ID."
}

output "iam_policy_arn" {
value = "${aws_iam_policy.default.arn}"
value = aws_iam_policy.default.arn
description = "The ARN assigned by AWS to this IAM Policy."
}

output "iam_policy_description" {
value = "${aws_iam_policy.default.description}"
value = aws_iam_policy.default.description
description = "The description of the IAM Policy."
}

output "iam_policy_name" {
value = "${aws_iam_policy.default.name}"
value = aws_iam_policy.default.name
description = "The name of the IAM Policy."
}

output "iam_policy_path" {
value = "${aws_iam_policy.default.path}"
value = aws_iam_policy.default.path
description = "The path of the IAM Policy."
}

output "iam_policy_document" {
value = "${aws_iam_policy.default.policy}"
value = aws_iam_policy.default.policy
description = "The policy document of the IAM Policy."
}

31 changes: 16 additions & 15 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
variable "name" {
type = "string"
type = string
description = "The name of the application."
}

variable "ecs_cluster_name" {
type = "string"
type = string
description = "The ECS Cluster name."
}

variable "ecs_service_name" {
type = "string"
type = string
description = "The ECS Service name."
}

variable "lb_listener_arns" {
type = "list"
type = list(string)
description = "List of Amazon Resource Names (ARNs) of the load balancer listeners."
}

variable "blue_lb_target_group_name" {
type = "string"
type = string
description = "Name of the blue target group."
}

variable "green_lb_target_group_name" {
type = "string"
type = string
description = "Name of the green target group."
}

variable "auto_rollback_enabled" {
default = true
type = "string"
type = string
description = "Indicates whether a defined automatic rollback configuration is currently enabled for this Deployment Group."
}

variable "auto_rollback_events" {
default = ["DEPLOYMENT_FAILURE", "DEPLOYMENT_STOP_ON_ALARM"]
type = "list"
type = list(string)
description = "The event type or types that trigger a rollback."
}

variable "action_on_timeout" {
default = "CONTINUE_DEPLOYMENT"
type = "string"
type = string
description = "When to reroute traffic from an original environment to a replacement environment in a blue/green deployment."
}

variable "wait_time_in_minutes" {
default = 0
type = "string"
type = string
description = "The number of minutes to wait before the status of a blue/green deployment changed to Stopped if rerouting is not started manually."
}

variable "termination_wait_time_in_minutes" {
default = 5
type = "string"
type = string
description = "The number of minutes to wait after a successful blue/green deployment before terminating instances from the original environment."
}

variable "test_traffic_route_listener_arns" {
default = []
type = "list"
type = list(string)
description = "List of Amazon Resource Names (ARNs) of the load balancer to route test traffic listeners."
}

variable "iam_path" {
default = "/"
type = "string"
type = string
description = "Path in which to create the IAM Role and the IAM Policy."
}

variable "description" {
default = "Managed by Terraform"
type = "string"
type = string
description = "The description of the all resources."
}

variable "tags" {
default = {}
type = "map"
type = map(string)
description = "A mapping of tags to assign to all resources."
}