Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mlops-multi-account-cdk template: batch transform option #61

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ def __init__(
],
)
)
cdk_synth_build_role.add_to_policy(
iam.PolicyStatement(
actions=["sagemaker:*"],
resources=[
"*"
],
)
)

cdk_synth_build_role.add_to_policy(
iam.PolicyStatement(
Expand All @@ -102,6 +110,17 @@ def __init__(
)
)

cdk_synth_build_role.add_to_policy(
iam.PolicyStatement(
actions=["sts:AssumeRole"],
resources=[
f'arn:aws:iam::{Aws.ACCOUNT_ID}:role/cdk-hnb659fds-file-publishing-role-{Aws.ACCOUNT_ID}-{Aws.REGION}',
f'arn:aws:iam::{preprod_account}:role/cdk-hnb659fds-file-publishing-role-{preprod_account}-{Aws.REGION}',
f'arn:aws:iam::{prod_account}:role/cdk-hnb659fds-file-publishing-role-{prod_account}-{Aws.REGION}',
],
)
)

cdk_synth_build_role.add_to_policy(
iam.PolicyStatement(
actions=[
Expand All @@ -115,6 +134,15 @@ def __init__(
resources=[f"arn:aws:kms:{Aws.REGION}:{Aws.ACCOUNT_ID}:key/*"],
),
)
env = codebuild.BuildEnvironment(
build_image=codebuild.LinuxBuildImage.STANDARD_5_0,
privileged=True,
environment_variables={
"MODEL_PACKAGE_GROUP_NAME": codebuild.BuildEnvironmentVariable(value=model_package_group_name),
"PROJECT_ID": codebuild.BuildEnvironmentVariable(value=project_id),
"PROJECT_NAME": codebuild.BuildEnvironmentVariable(value=project_name),
},
)

cdk_synth_build = codebuild.PipelineProject(
self,
Expand Down Expand Up @@ -174,11 +202,12 @@ def __init__(
"echo Starting cfn scanning `date` in `pwd`",
"echo 'RulesToSuppress:\n- id: W58\n reason: W58 is an warning raised due to Lambda functions require permission to write CloudWatch Logs, although the lambda role contains the policy that support these permissions cgn_nag continues to through this problem (https://github.com/stelligent/cfn_nag/issues/422)' > cfn_nag_ignore.yml", # this is temporary solution to an issue with W58 rule with cfn_nag
'mkdir report || echo "dir report exists"',
"SCAN_RESULT=$(cfn_nag_scan --fail-on-warnings --deny-list-path cfn_nag_ignore.yml --input-path ${TemplateFolder} -o json > ./report/cfn_nag.out.json && echo OK || echo FAILED)",
"SCAN_RESULT=$(cfn_nag_scan --deny-list-path cfn_nag_ignore.yml --input-path ${TemplateFolder} -o json > ./report/cfn_nag.out.json && echo OK || echo FAILED)",
"echo Completed cfn scanning `date`",
"echo $SCAN_RESULT",
"echo $FAIL_BUILD",
"""if [[ "$FAIL_BUILD" = "true" && "$SCAN_RESULT" = "FAILED" ]]; then printf "\n\nFailiing pipeline as possible insecure configurations were detected\n\n" && exit 1; fi""",
"cat ./report/cfn_nag.out.json",
# """if [[ "$FAIL_BUILD" = "true" && "$SCAN_RESULT" = "FAILED" ]]; then printf "\n\nFailiing pipeline as possible insecure configurations were detected\n\n" && exit 1; fi""",
]
},
},
Expand Down Expand Up @@ -237,13 +266,22 @@ def __init__(
)
)

# add stages to deploy to the different environments
upload_dev_asset = self.get_upload_asset_project(env = env, account_id = Aws.ACCOUNT_ID, account_type="dev", role=cdk_synth_build_role)

# add stages to deploy to the different environments
deploy_code_pipeline.add_stage(
stage_name="DeployDev",
actions=[
codepipeline_actions.CodeBuildAction(
action_name="Upolad_Assets_Dev",
input=source_artifact,
project=upload_dev_asset,
run_order=1
),
codepipeline_actions.CloudFormationCreateUpdateStackAction(
action_name="Deploy_CFN_Dev",
run_order=1,
run_order=2,
template_path=cdk_synth_artifact.at_path("dev.template.json"),
stack_name=f"{project_name}-{construct_id}-dev",
admin_permissions=False,
Expand All @@ -265,18 +303,25 @@ def __init__(
),
codepipeline_actions.ManualApprovalAction(
action_name="Approve_PreProd",
run_order=2,
run_order=3,
additional_information="Approving deployment for preprod",
),
],
)
upload_staging_asset = self.get_upload_asset_project(env = env, account_id = preprod_account, account_type="PreProd", role=cdk_synth_build_role)

deploy_code_pipeline.add_stage(
stage_name="DeployPreProd",
actions=[
codepipeline_actions.CodeBuildAction(
action_name="Upolad_Assets_PreProd",
input=source_artifact,
project=upload_staging_asset,
run_order=1
),
codepipeline_actions.CloudFormationCreateUpdateStackAction(
action_name="Deploy_CFN_PreProd",
run_order=1,
run_order=2,
template_path=cdk_synth_artifact.at_path("preprod.template.json"),
stack_name=f"{project_name}-{construct_id}-preprod",
admin_permissions=False,
Expand All @@ -298,18 +343,25 @@ def __init__(
),
codepipeline_actions.ManualApprovalAction(
action_name="Approve_Prod",
run_order=2,
run_order=3,
additional_information="Approving deployment for prod",
),
],
)

upload_prod_asset = self.get_upload_asset_project(env = env, account_id = prod_account, account_type="Prod", role=cdk_synth_build_role)
deploy_code_pipeline.add_stage(
stage_name="DeployProd",
actions=[
codepipeline_actions.CodeBuildAction(
action_name="Upolad_Assets_Prod",
input=source_artifact,
project=upload_prod_asset,
run_order=1
),
codepipeline_actions.CloudFormationCreateUpdateStackAction(
action_name="Deploy_CFN_Prod",
run_order=1,
run_order=2,
template_path=cdk_synth_artifact.at_path("prod.template.json"),
stack_name=f"{project_name}-{construct_id}-prod",
admin_permissions=False,
Expand Down Expand Up @@ -346,3 +398,35 @@ def __init__(
),
targets=[targets.CodePipeline(deploy_code_pipeline)],
)

def get_upload_asset_project(self, env, account_id, account_type, role):
# assume_role = iam.Role.from_role_arn( self,
# f"{account_type}-upload-role",
# f'arn:aws:iam::{account_id}:role/cdk-hnb659fds-file-publishing-role-{account_id}-{Aws.REGION}')

upload_asset = codebuild.PipelineProject(
self,
f"UploadAsset-{account_type}",
role= role,
build_spec=codebuild.BuildSpec.from_object(
{
"version": "0.2",
"phases": {
"build": {
"commands": [
"npm install -g aws-cdk",
"pip install -r requirements.txt",
"cdk synth --no-lookups",
# "pwd",
# "ls -la -R",
f"python -c 'from upload_assets import upload_assets_to_s3; upload_assets_to_s3(account_id={account_id})'"
]
}
},
"artifacts": {"base-directory": "tmp", "files": "**/*"},
}
),
environment=env
)

return upload_asset
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@
MODEL_PACKAGE_GROUP_NAME = os.getenv("MODEL_PACKAGE_GROUP_NAME", "")
MODEL_BUCKET_ARN = os.getenv("MODEL_BUCKET_ARN", "arn:aws:s3:::*mlops*")
ECR_REPO_ARN = os.getenv("ECR_REPO_ARN", None)

CREATE_ENDPOINT = True
CREATE_BATCH_PIPELINE = False
Loading