Skip to content

Commit d938970

Browse files
authored
Fix task private repository credentials variable issue (#14)
* Fix task private repository credentials variable issue * update readme with new version
1 parent 415bafb commit d938970

File tree

7 files changed

+100
-72
lines changed

7 files changed

+100
-72
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: git://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.29.0
21+
rev: v1.30.0
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "aws_ecs_cluster" "cluster" {
2828
2929
module "ecs-farage" {
3030
source = "umotif-public/ecs-fargate/aws"
31-
version = "~> 1.1"
31+
version = "~> 1.3.0"
3232
3333
name_prefix = "ecs-fargate-example"
3434
vpc_id = "vpc-abasdasd132"
@@ -88,6 +88,7 @@ No requirements.
8888
| capacity\_provider\_strategy | (Optional) The capacity\_provider\_strategy configuration block. This is a list of maps, where each map should contain "capacity\_provider ", "weight" and "base" | `list` | `[]` | no |
8989
| cluster\_id | The Amazon Resource Name (ARN) that identifies the cluster. | `string` | n/a | yes |
9090
| container\_name | Optional name for the container to be used instead of name\_prefix. | `string` | `""` | no |
91+
| create\_repository\_credentials\_iam\_policy | Set to true if you are specifying `repository_credentials` variable, it will attach IAM policy with necessary permissions to task role. | `bool` | `false` | no |
9192
| deployment\_controller\_type | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS. | `string` | `"ECS"` | no |
9293
| deployment\_maximum\_percent | The upper limit of the number of running tasks that can be running in a service during a deployment | `number` | `200` | no |
9394
| deployment\_minimum\_healthy\_percent | The lower limit of the number of running tasks that must remain running and healthy in a service during a deployment | `number` | `50` | no |

data.tf

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
11
data "aws_region" "current" {}
2+
3+
# Task role assume policy
4+
data "aws_iam_policy_document" "task_assume" {
5+
statement {
6+
effect = "Allow"
7+
actions = ["sts:AssumeRole"]
8+
9+
principals {
10+
type = "Service"
11+
identifiers = ["ecs-tasks.amazonaws.com"]
12+
}
13+
}
14+
}
15+
16+
# Task logging privileges
17+
data "aws_iam_policy_document" "task_permissions" {
18+
statement {
19+
effect = "Allow"
20+
21+
resources = [
22+
aws_cloudwatch_log_group.main.arn,
23+
]
24+
25+
actions = [
26+
"logs:CreateLogStream",
27+
"logs:PutLogEvents",
28+
]
29+
}
30+
}
31+
32+
# Task ecr privileges
33+
data "aws_iam_policy_document" "task_execution_permissions" {
34+
statement {
35+
effect = "Allow"
36+
37+
resources = [
38+
"*",
39+
]
40+
41+
actions = [
42+
"ecr:GetAuthorizationToken",
43+
"ecr:BatchCheckLayerAvailability",
44+
"ecr:GetDownloadUrlForLayer",
45+
"ecr:BatchGetImage",
46+
"logs:CreateLogStream",
47+
"logs:PutLogEvents",
48+
]
49+
}
50+
}
51+
52+
data "aws_kms_key" "secretsmanager_key" {
53+
count = var.create_repository_credentials_iam_policy ? 1 : 0
54+
55+
key_id = var.repository_credentials_kms_key
56+
}
57+
58+
data "aws_iam_policy_document" "read_repository_credentials" {
59+
count = var.create_repository_credentials_iam_policy ? 1 : 0
60+
61+
statement {
62+
effect = "Allow"
63+
64+
resources = [
65+
var.repository_credentials,
66+
data.aws_kms_key.secretsmanager_key[0].arn,
67+
]
68+
69+
actions = [
70+
"secretsmanager:GetSecretValue",
71+
"kms:Decrypt",
72+
]
73+
}
74+
}

examples/core/main.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ resource "aws_security_group_rule" "task_ingress_80" {
6767
source_security_group_id = module.alb.security_group_id
6868
}
6969

70+
#####
71+
# private repo credentials secretsmanager
72+
#####
73+
data "aws_kms_key" "secretsmanager_key" {
74+
key_id = "alias/aws/secretsmanager"
75+
}
76+
77+
resource "aws_secretsmanager_secret" "task_credentials" {
78+
name = "task_repository_credentials"
79+
80+
kms_key_id = data.aws_kms_key.secretsmanager_key.arn
81+
}
82+
7083
#####
7184
# ECS cluster and fargate
7285
#####
@@ -96,4 +109,8 @@ module "fargate" {
96109
port = "traffic-port"
97110
path = "/"
98111
}
112+
113+
### To use task credentials, below paramaters are required
114+
# create_repository_credentials_iam_policy = false
115+
# repository_credentials = aws_secretsmanager_secret.task_credentials.arn
99116
}

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ resource "aws_iam_role_policy" "task_execution" {
2424
}
2525

2626
resource "aws_iam_role_policy" "read_repository_credentials" {
27-
count = length(var.repository_credentials) != 0 ? 1 : 0
27+
count = var.create_repository_credentials_iam_policy ? 1 : 0
2828

2929
name = "${var.name_prefix}-read-repository-credentials"
3030
role = aws_iam_role.execution.id
31-
policy = data.aws_iam_policy_document.read_repository_credentials.json
31+
policy = data.aws_iam_policy_document.read_repository_credentials[0].json
3232
}
3333

3434
#####

policies.tf

Lines changed: 0 additions & 68 deletions
This file was deleted.

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ variable "repository_credentials_kms_key" {
146146
type = string
147147
}
148148

149+
variable "create_repository_credentials_iam_policy" {
150+
default = false
151+
description = "Set to true if you are specifying `repository_credentials` variable, it will attach IAM policy with necessary permissions to task role."
152+
}
153+
149154
variable "service_registry_arn" {
150155
default = ""
151156
description = "ARN of aws_service_discovery_service resource"

0 commit comments

Comments
 (0)