Skip to content

Commit

Permalink
Add var.service_role_arn (#110)
Browse files Browse the repository at this point in the history
* Add var.service_iam_role

* Add var.service_role_arn

* Add var.service_role_arn

* Auto Format

* Trigger validate-codeowners

* Trigger validate-codeowners

Co-authored-by: cloudpossebot <[email protected]>
Co-authored-by: PePe Amengual <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2021
1 parent a4d023d commit 25dc252
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ Available targets:
| security\_group\_ids | Security group IDs to allow in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `[]` | no |
| service\_placement\_constraints | The rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. See [`placement_constraints`](https://www.terraform.io/docs/providers/aws/r/ecs_service.html#placement_constraints-1) docs | <pre>list(object({<br> type = string<br> expression = string<br> }))</pre> | `[]` | no |
| service\_registries | The service discovery registries for the service. The maximum number of service\_registries blocks is 1. The currently supported service registry is Amazon Route 53 Auto Naming Service - `aws_service_discovery_service`; see `service_registries` docs https://www.terraform.io/docs/providers/aws/r/ecs_service.html#service_registries-1 | <pre>list(object({<br> registry_arn = string<br> port = number<br> container_name = string<br> container_port = number<br> }))</pre> | `[]` | no |
| service\_role\_arn | ARN of the IAM role that allows Amazon ECS to make calls to your load balancer on your behalf. This parameter is required if you are using a load balancer with your service, but only if your task definition does not use the awsvpc network mode. If using awsvpc network mode, do not specify this role. If your account has already created the Amazon ECS service-linked role, that role is used by default for your service unless you specify a role here. | `string` | `null` | no |
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| subnet\_ids | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
| security\_group\_ids | Security group IDs to allow in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `[]` | no |
| service\_placement\_constraints | The rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. See [`placement_constraints`](https://www.terraform.io/docs/providers/aws/r/ecs_service.html#placement_constraints-1) docs | <pre>list(object({<br> type = string<br> expression = string<br> }))</pre> | `[]` | no |
| service\_registries | The service discovery registries for the service. The maximum number of service\_registries blocks is 1. The currently supported service registry is Amazon Route 53 Auto Naming Service - `aws_service_discovery_service`; see `service_registries` docs https://www.terraform.io/docs/providers/aws/r/ecs_service.html#service_registries-1 | <pre>list(object({<br> registry_arn = string<br> port = number<br> container_name = string<br> container_port = number<br> }))</pre> | `[]` | no |
| service\_role\_arn | ARN of the IAM role that allows Amazon ECS to make calls to your load balancer on your behalf. This parameter is required if you are using a load balancer with your service, but only if your task definition does not use the awsvpc network mode. If using awsvpc network mode, do not specify this role. If your account has already created the Amazon ECS service-linked role, that role is used by default for your service unless you specify a role here. | `string` | `null` | no |
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| subnet\_ids | Subnet IDs used in Service `network_configuration` if `var.network_mode = "awsvpc"` | `list(string)` | `null` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |
Expand Down
10 changes: 5 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ data "aws_iam_policy_document" "ecs_service" {
}

resource "aws_iam_role" "ecs_service" {
count = local.enable_ecs_service_role ? 1 : 0
count = local.enable_ecs_service_role && var.service_role_arn == null ? 1 : 0
name = module.service_label.id
assume_role_policy = join("", data.aws_iam_policy_document.ecs_service.*.json)
permissions_boundary = var.permissions_boundary == "" ? null : var.permissions_boundary
tags = module.service_label.tags
}

data "aws_iam_policy_document" "ecs_service_policy" {
count = local.enable_ecs_service_role ? 1 : 0
count = local.enable_ecs_service_role && var.service_role_arn == null ? 1 : 0

statement {
effect = "Allow"
Expand All @@ -172,7 +172,7 @@ data "aws_iam_policy_document" "ecs_service_policy" {
}

resource "aws_iam_role_policy" "ecs_service" {
count = local.enable_ecs_service_role ? 1 : 0
count = local.enable_ecs_service_role && var.service_role_arn == null ? 1 : 0
name = module.service_label.id
policy = join("", data.aws_iam_policy_document.ecs_service_policy.*.json)
role = join("", aws_iam_role.ecs_service.*.id)
Expand Down Expand Up @@ -296,7 +296,7 @@ resource "aws_ecs_service" "ignore_changes_task_definition" {
platform_version = var.launch_type == "FARGATE" ? var.platform_version : null
scheduling_strategy = var.launch_type == "FARGATE" ? "REPLICA" : var.scheduling_strategy
enable_ecs_managed_tags = var.enable_ecs_managed_tags
iam_role = local.enable_ecs_service_role ? join("", aws_iam_role.ecs_service.*.arn) : null
iam_role = local.enable_ecs_service_role ? coalesce(var.service_role_arn, join("", aws_iam_role.ecs_service.*.arn)) : null
wait_for_steady_state = var.wait_for_steady_state

dynamic "capacity_provider_strategy" {
Expand Down Expand Up @@ -379,7 +379,7 @@ resource "aws_ecs_service" "default" {
platform_version = var.launch_type == "FARGATE" ? var.platform_version : null
scheduling_strategy = var.launch_type == "FARGATE" ? "REPLICA" : var.scheduling_strategy
enable_ecs_managed_tags = var.enable_ecs_managed_tags
iam_role = local.enable_ecs_service_role ? join("", aws_iam_role.ecs_service.*.arn) : null
iam_role = local.enable_ecs_service_role ? coalesce(var.service_role_arn, join("", aws_iam_role.ecs_service.*.arn)) : null
wait_for_steady_state = var.wait_for_steady_state

dynamic "capacity_provider_strategy" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ variable "task_policy_arns" {
default = []
}

variable "service_role_arn" {
type = string
description = "ARN of the IAM role that allows Amazon ECS to make calls to your load balancer on your behalf. This parameter is required if you are using a load balancer with your service, but only if your task definition does not use the awsvpc network mode. If using awsvpc network mode, do not specify this role. If your account has already created the Amazon ECS service-linked role, that role is used by default for your service unless you specify a role here."
default = null
}

variable "desired_count" {
type = number
description = "The number of instances of the task definition to place and keep running"
Expand Down

0 comments on commit 25dc252

Please sign in to comment.