| 7 | ) |
| 8 | |
| 9 | class Base(Stack): |
| 10 | def __init__(self, app: App, id: str, props, **kwargs) -> None: |
| 11 | super().__init__(app, id, **kwargs) |
| 12 | |
| 13 | # pipeline requires versioned bucket |
| 14 | bucket = aws_s3.Bucket( |
| 15 | self, "SourceBucket", |
| 16 | bucket_name=f"{props['namespace'].lower()}-{Aws.ACCOUNT_ID}", |
| 17 | versioned=True, |
| 18 | removal_policy=RemovalPolicy.DESTROY) |
| 19 | # ssm parameter to get bucket name later |
| 20 | bucket_param = aws_ssm.StringParameter( |
| 21 | self, "ParameterB", |
| 22 | parameter_name=f"{props['namespace']}-bucket", |
| 23 | string_value=bucket.bucket_name, |
| 24 | description='cdk pipeline bucket' |
| 25 | ) |
| 26 | # ecr repo to push docker container into |
| 27 | ecr = aws_ecr.Repository( |
| 28 | self, "ECR", |
| 29 | repository_name=f"{props['namespace']}", |
| 30 | removal_policy=RemovalPolicy.DESTROY |
| 31 | ) |
| 32 | # codebuild project meant to run in pipeline |
| 33 | cb_docker_build = aws_codebuild.PipelineProject( |
| 34 | self, "DockerBuild", |
| 35 | project_name=f"{props['namespace']}-Docker-Build", |
| 36 | build_spec=aws_codebuild.BuildSpec.from_source_filename( |
| 37 | filename='pipeline_delivery/docker_build_buildspec.yml'), |
| 38 | environment=aws_codebuild.BuildEnvironment( |
| 39 | privileged=True, |
| 40 | ), |
| 41 | # pass the ecr repo uri into the codebuild project so codebuild knows where to push |
| 42 | environment_variables={ |
| 43 | 'ecr': aws_codebuild.BuildEnvironmentVariable( |
| 44 | value=ecr.repository_uri), |
| 45 | 'tag': aws_codebuild.BuildEnvironmentVariable( |
| 46 | value='cdk') |
| 47 | }, |
| 48 | description='Pipeline for CodeBuild', |
| 49 | timeout=Duration.minutes(60), |
| 50 | ) |
| 51 | # codebuild iam permissions to read write s3 |
| 52 | bucket.grant_read_write(cb_docker_build) |
| 53 | |
| 54 | # codebuild permissions to interact with ecr |
| 55 | ecr.grant_pull_push(cb_docker_build) |
| 56 | |
| 57 | CfnOutput( |
| 58 | self, "ECRURI", |
| 59 | description="ECR URI", |
| 60 | value=ecr.repository_uri, |
| 61 | ) |
| 62 | CfnOutput( |
| 63 | self, "S3Bucket", |
| 64 | description="S3 Bucket", |
| 65 | value=bucket.bucket_name |
| 66 | ) |