-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbucket.py
59 lines (48 loc) · 1.68 KB
/
bucket.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
import json
from pulumi import ComponentResource, ResourceOptions
from pulumi_aws import ecs, iam, s3
class BucketArgs:
def __init__(
self,
role_name=None,
):
self.role_name = role_name
class Bucket(ComponentResource):
def __init__(
self,
name: str,
args: BucketArgs,
opts: ResourceOptions = None,
):
super().__init__("custom:resource:Bucket", name, {}, opts)
# Create an S3 bucket
self.bucket = s3.Bucket(name, acl="private", opts=ResourceOptions(parent=self))
# Grant the ECS task role permission to access the S3 bucket
self.bucket_policy = iam.Policy(
f"{name}-access-policy",
policy=self.bucket.arn.apply(
lambda arn: json.dumps(
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
f"{arn}/*", # Grant access to objects in the bucket
arn, # Grant access to the bucket itself
],
}
],
}
)
),
opts=ResourceOptions(parent=self),
)
self.rpa = iam.RolePolicyAttachment(
f"{name}-s3-role-policy-attachment",
policy_arn=self.bucket_policy.arn,
role=args.role_name,
opts=ResourceOptions(parent=self),
)
self.register_outputs({})