Skip to content

Commit aeb106c

Browse files
committed
init
1 parent e567d81 commit aeb106c

16 files changed

+933
-35
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.tf]
15+
indent_size = 2

.gitignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
crash.*.log
11+
12+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
13+
# .tfvars files are managed as part of configuration and so should be included in
14+
# version control.
15+
*.tfvars
16+
*.tfvars.json
17+
18+
# Ignore override files as they are usually used to override resources locally and so
19+
# are not checked in
20+
override.tf
21+
override.tf.json
22+
*_override.tf
23+
*_override.tf.json
24+
25+
# Include override files you do wish to add to version control using negated pattern
26+
#
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
*tfplan*
31+
32+
# Ignore CLI configuration files
33+
.terraformrc
34+
terraform.rc
35+
36+
.infracost

.terraform.lock.hcl

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+20-35
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1-
# Project name
1+
# terraform-aws-ecs-service
22

33
[![GitHub contributors][ico-contributors]][link-contributors]
44
[![GitHub last commit][ico-last-commit]][link-last-commit]
55
[![License: MPL 2.0][ico-license]][link-license]
66

7-
Insert bullets description of the project if available.
7+
A Terraform module to provision and manage an AWS ECS Cluster.
88

9-
[See the project live][link-production]
10-
11-
Give a short introduction of your project. Let this section explain the objectives or the motivation behind this project.
12-
13-
[Contributing](#contributing) | [Built with](#built-with) | [Repos and projects](#repos-and-projects) | [Deployment](#deployment) | [Feedback](#feedback) | [License](#license) | [About Code for Romania](#about-code-for-romania)
9+
[Contributing](#contributing) | [Usage](#usage) | [Feedback](#feedback) | [License](#license) | [About Code for Romania](#about-code-for-romania)
1410

1511
## Contributing
1612

1713
This project is built by amazing volunteers and you can be one of them! Here's a list of ways in [which you can contribute to this project][link-contributing]. If you want to make any change to this repository, please **make a fork first**.
1814

19-
Help us out by testing this project in the [staging environment][link-staging]. If you see something that doesn't quite work the way you expect it to, open an Issue. Make sure to describe what you _expect to happen_ and _what is actually happening_ in detail.
15+
If you see something that doesn't quite work the way you expect it to, open an Issue. Make sure to describe what you _expect to happen_ and _what is actually happening_ in detail.
2016

2117
If you would like to suggest new functionality, open an Issue and mark it as a __[Feature request]__. Please be specific about why you think this functionality will be of use. If you can, please include some visual description of what you would like the UI to look like, if you are suggesting new UI elements.
2218

23-
## Built With
24-
25-
### Programming languages
26-
27-
### Platforms
19+
## Usage
2820

29-
### Frontend framework
21+
```hcl
22+
module "ecs_cluster" {
23+
source = "code4romania/ecs-service/aws"
24+
version = "0.1.0"
25+
namespace = "project_name"
26+
}
27+
```
3028

31-
### Package managers
29+
### Inputs
3230

33-
### Database technology & provider
31+
TBD
3432

35-
## Repos and projects
33+
### Outputs
3634

37-
Mention all related repos and projects.
38-
39-
## Deployment
40-
41-
Guide users through getting your code up and running on their own system. In this section you can talk about:
42-
1. Installation process
43-
2. Software dependencies
44-
3. Latest releases
45-
4. API references
46-
47-
Describe and show how to build your code and run the tests.
35+
TBD
4836

4937
## Feedback
5038

@@ -64,17 +52,14 @@ Started in 2016, Code for Romania is a civic tech NGO, official member of the Co
6452
Last, but not least, we rely on donations to ensure the infrastructure, logistics and management of our community that is widely spread across 11 timezones, coding for social change to make Romania and the world a better place. If you want to support us, [you can do it here][link-donate].
6553

6654

67-
[ico-contributors]: https://img.shields.io/github/contributors/code4romania/standard-repo-template.svg?style=for-the-badge
68-
[ico-last-commit]: https://img.shields.io/github/last-commit/code4romania/standard-repo-template.svg?style=for-the-badge
55+
[ico-contributors]: https://img.shields.io/github/contributors/code4romania/terraform-aws-ecs-service.svg?style=for-the-badge
56+
[ico-last-commit]: https://img.shields.io/github/last-commit/code4romania/terraform-aws-ecs-service.svg?style=for-the-badge
6957
[ico-license]: https://img.shields.io/badge/license-MPL%202.0-brightgreen.svg?style=for-the-badge
7058

71-
[link-contributors]: https://github.com/code4romania/standard-repo-template/graphs/contributors
72-
[link-last-commit]: https://github.com/code4romania/standard-repo-template/commits/main
59+
[link-contributors]: https://github.com/code4romania/terraform-aws-ecs-service/graphs/contributors
60+
[link-last-commit]: https://github.com/code4romania/terraform-aws-ecs-service/commits/main
7361
[link-license]: https://opensource.org/licenses/MPL-2.0
7462
[link-contributing]: https://github.com/code4romania/.github/blob/main/CONTRIBUTING.md
7563

76-
[link-production]: insert_link_here
77-
[link-staging]: insert_link_here
78-
7964
[link-code4]: https://www.code4.ro/en/
8065
[link-donate]: https://code4.ro/en/donate/

autoscaling.tf

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
resource "aws_appautoscaling_target" "this" {
2+
count = local.fixed_capacity ? 0 : 1
3+
4+
service_namespace = "ecs"
5+
resource_id = "service/${data.aws_ecs_cluster.this.cluster_name}/${aws_ecs_service.this.name}"
6+
scalable_dimension = "ecs:service:DesiredCount"
7+
min_capacity = var.min_capacity
8+
max_capacity = var.max_capacity
9+
}
10+
11+
resource "aws_appautoscaling_policy" "this" {
12+
count = local.fixed_capacity ? 0 : 1
13+
14+
name = "${var.namespace}-target-scaling"
15+
resource_id = aws_appautoscaling_target.this.0.resource_id
16+
scalable_dimension = aws_appautoscaling_target.this.0.scalable_dimension
17+
service_namespace = aws_appautoscaling_target.this.0.service_namespace
18+
policy_type = "TargetTrackingScaling"
19+
20+
target_tracking_scaling_policy_configuration {
21+
target_value = var.target_value
22+
scale_in_cooldown = var.scale_in_cooldown
23+
scale_out_cooldown = var.scale_out_cooldown
24+
25+
predefined_metric_specification {
26+
predefined_metric_type = var.predefined_metric_type
27+
}
28+
}
29+
}

data.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_region" "current" {}
2+
3+
data "aws_ecs_cluster" "this" {
4+
cluster_name = var.cluster_name
5+
}

ecs_task_definition.tf

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
resource "aws_ecs_task_definition" "this" {
2+
container_definitions = local.container_definitions
3+
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
4+
task_role_arn = var.task_role_arn
5+
family = var.namespace
6+
ipc_mode = var.ipc_mode
7+
network_mode = var.network_mode
8+
pid_mode = var.pid_mode
9+
10+
memory = var.container_memory_hard_limit
11+
12+
dynamic "placement_constraints" {
13+
for_each = var.placement_constraints
14+
content {
15+
expression = lookup(placement_constraints.value, "expression", null)
16+
type = placement_constraints.value.type
17+
}
18+
}
19+
20+
requires_compatibilities = var.requires_compatibilities
21+
22+
dynamic "volume" {
23+
for_each = var.volumes
24+
25+
content {
26+
host_path = lookup(volume.value, "host_path", null)
27+
name = volume.value.name
28+
29+
dynamic "docker_volume_configuration" {
30+
for_each = lookup(volume.value, "docker_volume_configuration", [])
31+
32+
content {
33+
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
34+
driver = lookup(docker_volume_configuration.value, "driver", null)
35+
driver_opts = lookup(docker_volume_configuration.value, "driver_opts", null)
36+
labels = lookup(docker_volume_configuration.value, "labels", null)
37+
scope = lookup(docker_volume_configuration.value, "scope", null)
38+
}
39+
}
40+
41+
dynamic "efs_volume_configuration" {
42+
for_each = lookup(volume.value, "efs_volume_configuration", [])
43+
44+
content {
45+
file_system_id = element(lookup(efs_volume_configuration.value, "file_system_id", null), 0)
46+
root_directory = lookup(efs_volume_configuration.value, "root_directory", null)
47+
transit_encryption = lookup(efs_volume_configuration.value, "transit_encryption", null)
48+
}
49+
}
50+
}
51+
}
52+
53+
tags = var.tags
54+
}

iam.tf

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
### IAM resources
2+
data "aws_iam_policy_document" "ecs_task_assume" {
3+
statement {
4+
actions = ["sts:AssumeRole"]
5+
6+
principals {
7+
type = "Service"
8+
identifiers = ["ecs-tasks.amazonaws.com"]
9+
}
10+
}
11+
}
12+
13+
data "aws_iam_policy_document" "ecs_secret_policy" {
14+
statement {
15+
actions = ["secretsmanager:GetSecretValue"]
16+
resources = var.allowed_secrets
17+
}
18+
}
19+
20+
data "aws_iam_policy_document" "ssm_policy" {
21+
statement {
22+
actions = [
23+
"ssmmessages:CreateControlChannel",
24+
"ssmmessages:CreateDataChannel",
25+
"ssmmessages:OpenControlChannel",
26+
"ssmmessages:OpenDataChannel"
27+
]
28+
resources = ["*"]
29+
}
30+
}
31+
32+
resource "aws_iam_role" "ecs_task_execution_role" {
33+
name = "${var.namespace}-ecs-task-execution-role"
34+
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume.json
35+
36+
dynamic "inline_policy" {
37+
for_each = var.allowed_secrets == null ? [] : [1]
38+
39+
content {
40+
name = "SecretsPolicy"
41+
policy = data.aws_iam_policy_document.ecs_secret_policy.json
42+
}
43+
}
44+
45+
dynamic "inline_policy" {
46+
for_each = var.enable_execute_command ? [1] : []
47+
48+
content {
49+
name = "SSMPolicy"
50+
policy = data.aws_iam_policy_document.ssm_policy.json
51+
}
52+
}
53+
54+
dynamic "inline_policy" {
55+
for_each = var.additional_policy == null ? [] : [1]
56+
57+
content {
58+
name = "AdditionalPolicy"
59+
policy = var.additional_policy
60+
}
61+
}
62+
63+
tags = var.tags
64+
}
65+
66+
resource "aws_iam_role_policy_attachment" "AmazonEC2ContainerServiceforEC2Role" {
67+
role = aws_iam_role.ecs_task_execution_role.name
68+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
69+
}
70+
71+
resource "aws_iam_role_policy_attachment" "managed_policies_attachment" {
72+
count = length(var.managed_policies)
73+
role = aws_iam_role.ecs_task_execution_role.name
74+
policy_arn = var.managed_policies[count.index]
75+
}

lb.tf

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "aws_lb_target_group" "this" {
2+
count = var.use_load_balancer ? 1 : 0
3+
4+
name = var.namespace
5+
port = var.container_port
6+
protocol = "HTTP"
7+
vpc_id = var.lb_vpc_id
8+
target_type = "ip"
9+
10+
health_check {
11+
enabled = var.lb_health_check_enabled
12+
healthy_threshold = var.lb_healthy_threshold
13+
interval = var.lb_interval
14+
protocol = var.lb_protocol
15+
matcher = var.lb_matcher
16+
timeout = var.lb_timeout
17+
path = var.lb_path
18+
unhealthy_threshold = var.lb_unhealthy_threshold
19+
}
20+
}
21+
22+
resource "aws_lb_listener_rule" "routing" {
23+
count = var.use_load_balancer ? 1 : 0
24+
25+
listener_arn = var.lb_listener_arn
26+
27+
action {
28+
type = "forward"
29+
target_group_arn = aws_lb_target_group.this.0.arn
30+
}
31+
32+
condition {
33+
host_header {
34+
values = var.lb_hosts
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)