Skip to content

Commit 4fd796d

Browse files
authored
Updates to Fargate logging resources (#271)
* Add option for customizing container permissions * Add app_name for clear log group naming * Make policy name more clear * Fix how we add perms together * Fix how we add perms together * Clean up policy arn code a bit
1 parent 3a7f1f2 commit 4fd796d

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

tb_pulumi/cloudwatch.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class LogDestination(tb_pulumi.ThunderbirdComponentResource):
1818
with `Thunderbird logging guidelines
1919
<https://github.com/thunderbird/observability/blob/main/docs/rfc/application_logging/application_logging_guidelines.md>`_.
2020
21+
The name of the log group is constructed in the following way: ``/{org_name}/{stack_name}/{app_name}``. The first
22+
segment will be left off if no org_name is provided.
23+
2124
Produces the following ``resources``:
2225
2326
- *key_alias* - `aws.kms.Alias <https://www.pulumi.com/registry/packages/aws/api-docs/kms/alias/>`_ used to name
@@ -40,6 +43,9 @@ class LogDestination(tb_pulumi.ThunderbirdComponentResource):
4043
:param project: The ``ThunderbirdPulumiProject`` to build monitoring resources for.
4144
:type project: tb_pulumi.ThunderbirdPulumiProject
4245
46+
:param app_name: Final part of the name of the log group. If no app_name is provided, the name of the project will
47+
be used.
48+
4349
:param log_group: Dict of inputs to an `aws.cloudwatch.LogGroup
4450
<https://www.pulumi.com/registry/packages/aws/api-docs/cloudwatch/loggroup/#inputs>`_. This class assumes some
4551
deviations from defaults, intended to comply with Thunderbird Pro Services internal logging guidelines. Inputs
@@ -82,6 +88,7 @@ def __init__(
8288
self,
8389
name: str,
8490
project: tb_pulumi.ThunderbirdPulumiProject,
91+
app_name: str = None,
8592
log_group: dict = {},
8693
log_streams: dict = {},
8794
org_name: str = None,
@@ -98,11 +105,12 @@ def __init__(
98105
tags=tags,
99106
)
100107

108+
if not app_name:
109+
app_name = self.project.project
110+
101111
# Determine the log group's name before doing anything else; we need it for the key
102112
__log_group_name = (
103-
f'/{org_name}/{self.project.stack}/{self.project.project}'
104-
if org_name
105-
else f'/{self.project.stack}/{self.project.project}'
113+
f'/{org_name}/{self.project.stack}/{app_name}' if org_name else f'/{self.project.stack}/{app_name}'
106114
)
107115

108116
# KMS Keys need policies to describe who can manage the keys and who can use them for encryption operations.
@@ -253,7 +261,7 @@ def __init__(
253261

254262
__iam_policy_group_read = aws.iam.Policy(
255263
f'{self.project.name_prefix}-policy-read',
256-
name=f'{self.project.name_prefix}-cloudwatch-group-read-access',
264+
name=f'{self.project.name_prefix}-{app_name}-logs-read-access',
257265
description=__read_policy_description,
258266
path='/',
259267
policy=__iam_policy_group_read_doc,
@@ -265,7 +273,7 @@ def __init__(
265273
)
266274
__iam_policy_group_write = aws.iam.Policy(
267275
f'{self.project.name_prefix}-policy-write',
268-
name=f'{self.project.name_prefix}-cloudwatch-group-write-access',
276+
name=f'{self.project.name_prefix}-{app_name}-logs-write-access',
269277
description=__write_policy_description,
270278
path='/',
271279
policy=__iam_policy_group_write_doc,

tb_pulumi/fargate.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ class AutoscalingFargateCluster(tb_pulumi.ThunderbirdComponentResource):
9191
the only valid source. Defaults to {}.
9292
:type container_security_groups: _type_, optional
9393
94+
:param extra_policies: A dict where the keys are the names of services and the values are lists of ARNs of IAM
95+
Policies that service should operate with in addition to what is provided by using the ``registries``,
96+
``secrets``, and ``ssm_params`` parameters.
97+
9498
:param listeners: A nested dict describing your load balancers' listeners and which targets they point to. At the
9599
top level, the keys are names of load balancers and the values are other dicts. Those dicts' keys are the names of
96100
targets, and their values are inputs to an `aws.lb.Listener
@@ -169,6 +173,7 @@ def __init__(
169173
cluster: dict = {},
170174
cluster_name: str = None,
171175
container_security_groups: dict[str:dict] = {},
176+
extra_policies: dict[str:list] = {},
172177
listeners: dict[str, dict] = {},
173178
load_balancer_security_groups: dict[str, dict] = {},
174179
load_balancers: dict = {},
@@ -275,21 +280,28 @@ def __init__(
275280
}
276281

277282
# Build the execution roles using the policies from above, if they exist
278-
exec_roles = {
279-
service: aws.iam.Role(
283+
exec_roles = {}
284+
285+
for service in services.keys():
286+
_managed_policy_arns = [
287+
# This AWS managed policy allows access to ECR and log streams
288+
'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy',
289+
]
290+
_managed_policy_arns += [
291+
item
292+
for item in [
293+
exec_role_policies[service] if service in exec_role_policies else None,
294+
]
295+
if item is not None
296+
]
297+
_managed_policy_arns += extra_policies.get(service, [])
298+
299+
exec_roles[service] = aws.iam.Role(
280300
f'{name}-execrole-{service}',
281301
name=f'{name}-{service}',
282302
description=f'Task execution role for running the {service} service for {self.project.name_prefix}',
283303
assume_role_policy=arp,
284-
managed_policy_arns=[
285-
item
286-
for item in [
287-
# This AWS managed policy allows access to ECR and log streams
288-
'arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy',
289-
exec_role_policies[service] if service in exec_role_policies else None,
290-
]
291-
if item is not None
292-
],
304+
managed_policy_arns=_managed_policy_arns,
293305
tags=self.tags,
294306
opts=pulumi.ResourceOptions(
295307
parent=self,
@@ -300,8 +312,6 @@ def __init__(
300312
],
301313
),
302314
)
303-
for service in services.keys()
304-
}
305315

306316
# First we build out task definitions. Later, we can refer to them by name. Since task definitions are
307317
# one-to-one with cluster services, the task_name here is assumed to match with a service name.

0 commit comments

Comments
 (0)