@@ -15,6 +15,15 @@ def validate_args(args, extra_args):
1515 f'Error parsing reference: "{ args .repository } " is not a valid repository/tag'
1616 )
1717
18+ vpc_config = [args .vpc_id , args .subnets , args .security_groups ]
19+ none_arg_count = sum (arg is None for arg in [args .vpc_id , args .subnets , args .security_groups ])
20+
21+ if none_arg_count > 0 and none_arg_count < 3 :
22+ raise ValueError (
23+ 'Invalid input of the VPC configuration. Please either provide all of the VPC arguments or none of them,' \
24+ 'in which case the CodeBuild Project, by default, will not run within a VPC.'
25+ )
26+
1827 # Validate extra_args
1928 for idx , extra_arg in enumerate (extra_args ):
2029 # Validate that the path to the Dockerfile is within the PWD.
@@ -46,11 +55,24 @@ def get_role(args):
4655 )
4756
4857
58+ def construct_vpc_config (args ):
59+ if args .vpc_id is None :
60+ return None
61+ else :
62+ vpc_config = {
63+ 'vpcId' : args .vpc_id ,
64+ 'subnets' : args .subnets .split (',' ),
65+ 'securityGroupIds' : args .security_groups .split (',' )
66+ }
67+ return vpc_config
68+
69+
4970def build_image (args , extra_args ):
5071 validate_args (args , extra_args )
5172
5273 builder .build_image (
53- args .repository , get_role (args ), args .bucket , args .compute_type , extra_args , log = not args .no_logs
74+ args .repository , get_role (args ), args .bucket , args .compute_type ,
75+ construct_vpc_config (args ), extra_args , log = not args .no_logs
5476 )
5577
5678
@@ -74,7 +96,7 @@ def main():
7496 "--compute-type" ,
7597 help = "The CodeBuild compute type (default: BUILD_GENERAL1_SMALL)" ,
7698 choices = ["BUILD_GENERAL1_SMALL" , "BUILD_GENERAL1_MEDIUM" ,
77- "BUILD_GENERAL1_LARGE" , "BUILD_GENERAL1_2XLARGE" ]
99+ "BUILD_GENERAL1_LARGE" , "BUILD_GENERAL1_2XLARGE" ],
78100 default = "BUILD_GENERAL1_SMALL"
79101 )
80102 build_parser .add_argument (
@@ -85,6 +107,18 @@ def main():
85107 "--bucket" ,
86108 help = "The S3 bucket to use for sending data to CodeBuild (if None, use the SageMaker SDK default bucket)." ,
87109 )
110+ build_parser .add_argument (
111+ "--vpc-id" ,
112+ help = "The Id of the VPC that will host the CodeBuild Project (such as vpc-05c09f91d48831c8c)." ,
113+ )
114+ build_parser .add_argument (
115+ "--subnets" ,
116+ help = "The comma-separated list of subnet ids for the CodeBuild Project (such as subnet-0b31f1863e9d31a67)" ,
117+ )
118+ build_parser .add_argument (
119+ "--security-groups" ,
120+ help = "The comma-separated list of security group ids for the CodeBuild Project (such as sg-0ce4ec0d0414d2ddc)." ,
121+ )
88122 build_parser .add_argument (
89123 "--no-logs" ,
90124 action = "store_true" ,
0 commit comments