-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.py
79 lines (70 loc) · 2.65 KB
/
cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import json
from pulumi import ComponentResource, ResourceOptions
from pulumi_aws import ecs, iam, config, get_caller_identity
class Cluster(ComponentResource):
def __init__(
self,
name: str,
opts: ResourceOptions = None,
):
super().__init__("custom:resource:Cluster", name, {}, opts)
# Create an AWS ECS Cluster
self.cluster = ecs.Cluster(
f"{name}",
opts=ResourceOptions(parent=self),
)
self.role = iam.Role(
f"{name}-task-role",
assume_role_policy=json.dumps(
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {"Service": "ecs-tasks.amazonaws.com"},
"Action": "sts:AssumeRole",
}
],
}
),
opts=ResourceOptions(parent=self),
)
self.rpa = iam.RolePolicyAttachment(
f"{name}-task-policy",
role=self.role.name,
policy_arn="arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
opts=ResourceOptions(parent=self),
)
self.log_policy = iam.Policy(
f"{name}-task-log-policy",
policy=json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
],
# Broad resource specification; consider narrowing down
"Resource": [
f"arn:aws:logs:{config.region}:{get_caller_identity().account_id}:log-group:/ecs/*",
f"arn:aws:logs:{config.region}:{get_caller_identity().account_id}:log-group:/ecs/*:log-stream:*",
],
}
],
}
),
opts=ResourceOptions(parent=self),
)
iam.RolePolicyAttachment(
f"{name}-task-log-policy-attachment",
policy_arn=self.log_policy.arn,
role=self.role.name,
opts=ResourceOptions(parent=self),
)
self.register_outputs({})