|
3 | 3 |
|
4 | 4 | [![Cloud Posse][logo]](https://cpco.io/homepage)
|
5 | 5 |
|
6 |
| -# terraform-aws-ecs-alb-service-task [](https://travis-ci.org/cloudposse/terraform-aws-ecs-alb-service-task) [](https://github.com/cloudposse/terraform-aws-ecs-alb-service-task/releases/latest) [](https://slack.cloudposse.com) |
| 6 | +# terraform-aws-ecs-alb-service-task [](https://g.codefresh.io/public/accounts/cloudposse/pipelines/5db352c10c7c5a56af1de612) [](https://github.com/cloudposse/terraform-aws-ecs-alb-service-task/releases/latest) [](https://slack.cloudposse.com) |
7 | 7 |
|
8 | 8 |
|
9 | 9 | Terraform module to create an ECS Service for a web app (task), and an ALB target group to route requests.
|
@@ -48,68 +48,98 @@ Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest re
|
48 | 48 |
|
49 | 49 |
|
50 | 50 |
|
51 |
| -For a complete example, see [examples/complete](examples/complete) |
| 51 | +For a complete example, see [examples/complete](examples/complete). |
| 52 | + |
| 53 | +For automated test of the complete example using `bats` and `Terratest`, see [test](test). |
52 | 54 |
|
53 | 55 | ```hcl
|
54 |
| -module "label" { |
55 |
| - source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=master" |
56 |
| - namespace = "eg" |
57 |
| - stage = "staging" |
58 |
| - name = "app" |
59 |
| -} |
60 |
| -
|
61 |
| -module "container_definition" { |
62 |
| - source = "git::https://github.com/cloudposse/terraform-aws-ecs-container-definition.git?ref=master" |
63 |
| - container_name = "app" |
64 |
| - container_image = "cloudposse/geodesic:latest" |
65 |
| -
|
66 |
| - environment = [ |
67 |
| - { |
68 |
| - name = "string_var" |
69 |
| - value = "I am a string" |
70 |
| - }, |
71 |
| - { |
72 |
| - name = "true_boolean_var" |
73 |
| - value = true |
74 |
| - }, |
75 |
| - { |
76 |
| - name = "false_boolean_var" |
77 |
| - value = false |
78 |
| - }, |
79 |
| - { |
80 |
| - name = "integer_var" |
81 |
| - value = 42 |
82 |
| - }, |
83 |
| - ] |
84 |
| -
|
85 |
| - port_mappings = [ |
86 |
| - { |
87 |
| - containerPort = 8080 |
88 |
| - hostPort = 80 |
89 |
| - protocol = "tcp" |
90 |
| - }, |
91 |
| - { |
92 |
| - containerPort = 8081 |
93 |
| - hostPort = 443 |
94 |
| - protocol = "udp" |
95 |
| - }, |
96 |
| - ] |
97 |
| -} |
98 |
| -
|
99 |
| -module "alb_service_task" { |
100 |
| - source = "git::https://github.com/cloudposse/terraform-aws-ecs-alb-service-task.git?ref=master" |
101 |
| - namespace = "eg" |
102 |
| - stage = "staging" |
103 |
| - name = "app" |
104 |
| - alb_target_group_arn = "xxxxxxx" |
105 |
| - container_definition_json = "${module.container_definition.json}" |
106 |
| - container_name = "${module.label.id}" |
107 |
| - ecs_cluster_arn = "xxxxxxx" |
108 |
| - launch_type = "FARGATE" |
109 |
| - vpc_id = "xxxxxxx" |
110 |
| - security_group_ids = ["xxxxx", "yyyyy"] |
111 |
| - private_subnet_ids = ["xxxxx", "yyyyy", "zzzzz"] |
112 |
| -} |
| 56 | + provider "aws" { |
| 57 | + region = var.region |
| 58 | + } |
| 59 | +
|
| 60 | + module "label" { |
| 61 | + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.15.0" |
| 62 | + namespace = var.namespace |
| 63 | + name = var.name |
| 64 | + stage = var.stage |
| 65 | + delimiter = var.delimiter |
| 66 | + attributes = var.attributes |
| 67 | + tags = var.tags |
| 68 | + } |
| 69 | +
|
| 70 | + module "vpc" { |
| 71 | + source = "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=tags/0.8.1" |
| 72 | + namespace = var.namespace |
| 73 | + stage = var.stage |
| 74 | + name = var.name |
| 75 | + delimiter = var.delimiter |
| 76 | + attributes = var.attributes |
| 77 | + cidr_block = var.vpc_cidr_block |
| 78 | + tags = var.tags |
| 79 | + } |
| 80 | +
|
| 81 | + module "subnets" { |
| 82 | + source = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/0.16.1" |
| 83 | + availability_zones = var.availability_zones |
| 84 | + namespace = var.namespace |
| 85 | + stage = var.stage |
| 86 | + name = var.name |
| 87 | + attributes = var.attributes |
| 88 | + delimiter = var.delimiter |
| 89 | + vpc_id = module.vpc.vpc_id |
| 90 | + igw_id = module.vpc.igw_id |
| 91 | + cidr_block = module.vpc.vpc_cidr_block |
| 92 | + nat_gateway_enabled = true |
| 93 | + nat_instance_enabled = false |
| 94 | + tags = var.tags |
| 95 | + } |
| 96 | +
|
| 97 | + resource "aws_ecs_cluster" "default" { |
| 98 | + name = module.label.id |
| 99 | + tags = module.label.tags |
| 100 | + } |
| 101 | +
|
| 102 | + module "container_definition" { |
| 103 | + source = "git::https://github.com/cloudposse/terraform-aws-ecs-container-definition.git?ref=tags/0.21.0" |
| 104 | + container_name = var.container_name |
| 105 | + container_image = var.container_image |
| 106 | + container_memory = var.container_memory |
| 107 | + container_memory_reservation = var.container_memory_reservation |
| 108 | + container_cpu = var.container_cpu |
| 109 | + essential = var.container_essential |
| 110 | + readonly_root_filesystem = var.container_readonly_root_filesystem |
| 111 | + environment = var.container_environment |
| 112 | + port_mappings = var.container_port_mappings |
| 113 | + log_configuration = var.container_log_configuration |
| 114 | + } |
| 115 | +
|
| 116 | + module "ecs_alb_service_task" { |
| 117 | + source = "git::https://github.com/cloudposse/terraform-aws-ecs-alb-service-task.git?ref=master" |
| 118 | + namespace = var.namespace |
| 119 | + stage = var.stage |
| 120 | + name = var.name |
| 121 | + attributes = var.attributes |
| 122 | + delimiter = var.delimiter |
| 123 | + alb_security_group = module.vpc.vpc_default_security_group_id |
| 124 | + container_definition_json = module.container_definition.json |
| 125 | + ecs_cluster_arn = aws_ecs_cluster.default.arn |
| 126 | + launch_type = var.ecs_launch_type |
| 127 | + vpc_id = module.vpc.vpc_id |
| 128 | + security_group_ids = [module.vpc.vpc_default_security_group_id] |
| 129 | + subnet_ids = module.subnets.public_subnet_ids |
| 130 | + tags = var.tags |
| 131 | + ignore_changes_task_definition = var.ignore_changes_task_definition |
| 132 | + network_mode = var.network_mode |
| 133 | + assign_public_ip = var.assign_public_ip |
| 134 | + propagate_tags = var.propagate_tags |
| 135 | + health_check_grace_period_seconds = var.health_check_grace_period_seconds |
| 136 | + deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent |
| 137 | + deployment_maximum_percent = var.deployment_maximum_percent |
| 138 | + deployment_controller_type = var.deployment_controller_type |
| 139 | + desired_count = var.desired_count |
| 140 | + task_memory = var.task_memory |
| 141 | + task_cpu = var.task_cpu |
| 142 | + } |
113 | 143 | ```
|
114 | 144 |
|
115 | 145 | The `container_image` in the `container_definition` module is the Docker image used to start a container.
|
@@ -152,38 +182,39 @@ Available targets:
|
152 | 182 | | Name | Description | Type | Default | Required |
|
153 | 183 | |------|-------------|:----:|:-----:|:-----:|
|
154 | 184 | | alb_security_group | Security group of the ALB | string | - | yes |
|
155 |
| -| assign_public_ip | Assign a public IP address to the ENI (Fargate launch type only). Valid values are true or false. Default false. | string | `false` | no | |
156 |
| -| attributes | Additional attributes (e.g. `1`) | list | `<list>` | no | |
| 185 | +| assign_public_ip | Assign a public IP address to the ENI (Fargate launch type only). Valid values are `true` or `false`. Default `false` | bool | `false` | no | |
| 186 | +| attributes | Additional attributes (_e.g._ "1") | list(string) | `<list>` | no | |
157 | 187 | | container_definition_json | The JSON of the task container definition | string | - | yes |
|
158 |
| -| container_port | The port on the container to allow via the ingress security group | string | `80` | no | |
159 |
| -| delimiter | Delimiter to be used between `name`, `namespace`, `stage`, etc. | string | `-` | no | |
160 |
| -| deployment_controller_type | Type of deployment controller. Valid values: `CODE_DEPLOY`, `ECS`. | string | `ECS` | no | |
161 |
| -| deployment_maximum_percent | The upper limit of the number of tasks (as a percentage of `desired_count`) that can be running in a service during a deployment | string | `200` | no | |
162 |
| -| deployment_minimum_healthy_percent | The lower limit (as a percentage of `desired_count`) of the number of tasks that must remain running and healthy in a service during a deployment | string | `100` | no | |
163 |
| -| desired_count | The number of instances of the task definition to place and keep running | string | `1` | no | |
| 188 | +| container_port | The port on the container to allow via the ingress security group | number | `80` | no | |
| 189 | +| delimiter | Delimiter between `namespace`, `stage`, `name` and `attributes` | string | `-` | no | |
| 190 | +| deployment_controller_type | Type of deployment controller. Valid values are `CODE_DEPLOY` and `ECS` | string | `ECS` | no | |
| 191 | +| deployment_maximum_percent | The upper limit of the number of tasks (as a percentage of `desired_count`) that can be running in a service during a deployment | number | `200` | no | |
| 192 | +| deployment_minimum_healthy_percent | The lower limit (as a percentage of `desired_count`) of the number of tasks that must remain running and healthy in a service during a deployment | number | `100` | no | |
| 193 | +| desired_count | The number of instances of the task definition to place and keep running | number | `1` | no | |
164 | 194 | | ecs_cluster_arn | The ARN of the ECS cluster where service will be provisioned | string | - | yes |
|
165 |
| -| ecs_load_balancers | A list of load balancer config objects for the ECS service; see `load_balancer` docs https://www.terraform.io/docs/providers/aws/r/ecs_service.html | list | `<list>` | no | |
166 |
| -| health_check_grace_period_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers | string | `0` | no | |
167 |
| -| ignore_changes_task_definition | Whether to ignore changes in container definition and task definition in the ECS service | string | `true` | no | |
| 195 | +| ecs_load_balancers | A list of load balancer config objects for the ECS service; see `load_balancer` docs https://www.terraform.io/docs/providers/aws/r/ecs_service.html | object | `<list>` | no | |
| 196 | +| enabled | Set to false to prevent the module from creating any resources | bool | `true` | no | |
| 197 | +| health_check_grace_period_seconds | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers | number | `0` | no | |
| 198 | +| ignore_changes_task_definition | Whether to ignore changes in container definition and task definition in the ECS service | bool | `true` | no | |
168 | 199 | | launch_type | The launch type on which to run your service. Valid values are `EC2` and `FARGATE` | string | `FARGATE` | no |
|
169 |
| -| name | Solution name, e.g. 'app' or 'cluster' | string | - | yes | |
170 |
| -| namespace | Namespace, which could be your organization name, e.g. 'eg' or 'cp' | string | - | yes | |
171 |
| -| network_mode | The network mode to use for the task. This is required to be awsvpc for `FARGATE` `launch_type` | string | `awsvpc` | no | |
172 |
| -| propagate_tags | Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK_DEFINITION. | string | `` | no | |
173 |
| -| security_group_ids | Security group IDs to allow in Service `network_configuration` | list | - | yes | |
174 |
| -| stage | Stage, e.g. 'prod', 'staging', 'dev', or 'test' | string | - | yes | |
175 |
| -| subnet_ids | Subnet IDs | list | - | yes | |
176 |
| -| tags | Additional tags (e.g. `map('BusinessUnit`,`XYZ`) | map | `<map>` | no | |
177 |
| -| task_cpu | The number of CPU units used by the task. If using `FARGATE` launch type `task_cpu` must match supported memory values (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | string | `256` | no | |
178 |
| -| task_memory | The amount of memory (in MiB) used by the task. If using Fargate launch type `task_memory` must match supported cpu value (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | string | `512` | no | |
179 |
| -| volumes | Task volume definitions as list of maps | list | `<list>` | no | |
| 200 | +| name | Name of the application | string | - | yes | |
| 201 | +| namespace | Namespace (e.g. `eg` or `cp`) | string | `` | no | |
| 202 | +| network_mode | The network mode to use for the task. This is required to be `awsvpc` for `FARGATE` `launch_type` | string | `awsvpc` | no | |
| 203 | +| propagate_tags | Specifies whether to propagate the tags from the task definition or the service to the tasks. The valid values are SERVICE and TASK_DEFINITION | string | `null` | no | |
| 204 | +| security_group_ids | Security group IDs to allow in Service `network_configuration` | list(string) | `<list>` | no | |
| 205 | +| stage | Stage (e.g. `prod`, `dev`, `staging`) | string | `` | no | |
| 206 | +| subnet_ids | Subnet IDs | list(string) | - | yes | |
| 207 | +| tags | Additional tags (_e.g._ { BusinessUnit : ABC }) | map(string) | `<map>` | no | |
| 208 | +| task_cpu | The number of CPU units used by the task. If using `FARGATE` launch type `task_cpu` must match supported memory values (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | number | `256` | no | |
| 209 | +| task_memory | The amount of memory (in MiB) used by the task. If using Fargate launch type `task_memory` must match supported cpu value (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size) | number | `512` | no | |
| 210 | +| volumes | Task volume definitions as list of configuration objects | object | `<list>` | no | |
180 | 211 | | vpc_id | The VPC ID where resources are created | string | - | yes |
|
181 | 212 |
|
182 | 213 | ## Outputs
|
183 | 214 |
|
184 | 215 | | Name | Description |
|
185 | 216 | |------|-------------|
|
186 |
| -| ecs_exec_role_policy_id | The ECS service role policy ID, in the form of role_name:role_policy_name | |
| 217 | +| ecs_exec_role_policy_id | The ECS service role policy ID, in the form of `role_name:role_policy_name` | |
187 | 218 | | ecs_exec_role_policy_name | ECS service role name |
|
188 | 219 | | service_name | ECS Service name |
|
189 | 220 | | service_role_arn | ECS Service role ARN |
|
|
0 commit comments