2
2
# SPDX-License-Identifier: Apache-2.0 OR ISC
3
3
4
4
import subprocess
5
+ import typing
6
+
5
7
import boto3
6
8
7
9
from botocore .exceptions import ClientError
8
- from aws_cdk import CfnTag , Duration , Stack , Tags , aws_ec2 as ec2 , aws_codebuild as codebuild , aws_iam as iam , aws_s3 as s3 , aws_logs as logs
10
+ from aws_cdk import CfnTag , Duration , Stack , Tags , aws_ec2 as ec2 , aws_codebuild as codebuild , aws_iam as iam , \
11
+ aws_s3 as s3 , aws_logs as logs , Environment
9
12
from constructs import Construct
10
13
11
14
from cdk .components import PruneStaleGitHubBuilds
12
- from util .metadata import AWS_ACCOUNT , AWS_REGION , GITHUB_PUSH_CI_BRANCH_TARGETS , GITHUB_REPO_OWNER , GITHUB_REPO_NAME , LINUX_AARCH_ECR_REPO , \
13
- LINUX_X86_ECR_REPO
15
+ from util .metadata import AWS_ACCOUNT , AWS_REGION , GITHUB_PUSH_CI_BRANCH_TARGETS , GITHUB_REPO_OWNER , GITHUB_REPO_NAME , \
16
+ LINUX_AARCH_ECR_REPO , \
17
+ LINUX_X86_ECR_REPO , PRE_PROD_ACCOUNT , STAGING_GITHUB_REPO_OWNER , STAGING_GITHUB_REPO_NAME
14
18
from util .iam_policies import code_build_batch_policy_in_json , ec2_policies_in_json , ssm_policies_in_json , s3_read_write_policy_in_json , ecr_power_user_policy_in_json
15
19
from util .build_spec_loader import BuildSpecLoader
16
20
@@ -23,13 +27,21 @@ def __init__(self,
23
27
scope : Construct ,
24
28
id : str ,
25
29
spec_file_path : str ,
30
+ env : typing .Optional [typing .Union [Environment , typing .Dict [str , typing .Any ]]],
26
31
** kwargs ) -> None :
27
- super ().__init__ (scope , id , ** kwargs )
32
+ super ().__init__ (scope , id , env = env , ** kwargs )
33
+
34
+ github_repo_owner = GITHUB_REPO_OWNER
35
+ github_repo_name = GITHUB_REPO_NAME
36
+
37
+ if env .account == PRE_PROD_ACCOUNT :
38
+ github_repo_owner = STAGING_GITHUB_REPO_OWNER
39
+ github_repo_name = STAGING_GITHUB_REPO_NAME
28
40
29
41
# Define CodeBuild resource.
30
42
git_hub_source = codebuild .Source .git_hub (
31
- owner = GITHUB_REPO_OWNER ,
32
- repo = GITHUB_REPO_NAME ,
43
+ owner = github_repo_owner ,
44
+ repo = github_repo_name ,
33
45
webhook = True ,
34
46
webhook_filters = [
35
47
codebuild .FilterGroup .in_event_of (
@@ -62,15 +74,14 @@ def __init__(self,
62
74
selected_subnets = vpc .select_subnets (subnet_type = ec2 .SubnetType .PRIVATE_WITH_EGRESS )
63
75
64
76
# create security group with default rules
65
- security_group = ec2 .SecurityGroup (self , id = "{}-ec2-sg" .format (id ),
66
- allow_all_outbound = True ,
67
- vpc = vpc ,
68
- security_group_name = 'codebuild_ec2_sg' )
69
-
77
+ # security_group = ec2.SecurityGroup(self, id="{}-ec2-sg".format(id),
78
+ # allow_all_outbound=True,
79
+ # vpc=vpc,
80
+ # security_group_name='codebuild_ec2_sg')
70
81
71
82
# Define a IAM role for this stack.
72
83
code_build_batch_policy = iam .PolicyDocument .from_json (code_build_batch_policy_in_json ([id ]))
73
- ec2_policy = iam .PolicyDocument .from_json (ec2_policies_in_json (ec2_role .role_name , security_group . security_group_id , selected_subnets .subnets [0 ].subnet_id , vpc .vpc_id ))
84
+ ec2_policy = iam .PolicyDocument .from_json (ec2_policies_in_json (ec2_role .role_name , vpc . vpc_default_security_group , selected_subnets .subnets [0 ].subnet_id , vpc .vpc_id ))
74
85
ssm_policy = iam .PolicyDocument .from_json (ssm_policies_in_json ())
75
86
codebuild_inline_policies = {"code_build_batch_policy" : code_build_batch_policy ,
76
87
"ec2_policy" : ec2_policy ,
@@ -97,7 +108,7 @@ def __init__(self,
97
108
build_spec = BuildSpecLoader .load (spec_file_path ),
98
109
environment_variables = {
99
110
"EC2_SECURITY_GROUP_ID" : codebuild .BuildEnvironmentVariable (
100
- value = security_group . security_group_id
111
+ value = vpc . vpc_default_security_group
101
112
),
102
113
"EC2_SUBNET_ID" : codebuild .BuildEnvironmentVariable (
103
114
value = selected_subnets .subnets [0 ].subnet_id
0 commit comments