All files / src activity.ts

96.76% Statements 599/619
89.53% Branches 77/86
100% Functions 23/23
96.76% Lines 599/619

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 6201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 75x 75x 75x 75x     75x 75x 1x       1x 75x 75x     75x 75x     75x 75x 4x       4x 75x 75x 2x 2x 75x 75x     75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 35x     35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 1x 1x 1x 1x 1x 1x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 9x 9x 4x 4x 4x 1x 1x 81x 65x 65x 16x 81x 2x 2x 81x     81x 81x 81x 81x 81x 81x 81x 1x 1x 79x 63x 63x 79x 79x 1x 1x 79x     79x 79x 79x 79x 79x 79x 1x 1x  
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
 
import { getTemplateFile, parameterValidationRegex, replacePatterns } from './util';
 
export interface ActivityProps {
 
  /**
   * Name of the SageMaker Activity. This name will be used to name the IAM policy that is created from this Activity.
   *
   */
  readonly activityName: string;
 
  /**
   * Version of the SageMaker Activity. This version will be used to fetch the policy template that corresponds to the
   * Activity.
   *
   * @default - 1
   */
  readonly version?: number;
 
  /**
   * ECR Repositories to give image pull permissions
   *
   * @default - none
   */
  readonly ecrRepositories?: ecr.IRepository[];
 
  /**
   * Roles to allow passing as passed roles to actions
   *
   * @default - none
   */
  readonly rolesToPass?: iam.IRole[];
 
  /**
   * S3 Buckets to give read and write permissions
   *
   * @default - none
   */
  readonly s3Buckets?: s3.IBucket[];
 
  /**
   * Names of the Athena workgroups to give query permissions
   *
   * @default - none
   */
  readonly athenaWorkgroupNames?: string[];
 
  /**
   * Names of the Glue Databases to give permissions to search tables
   *
   * @default - none
   */
  readonly glueDatabaseNames?: string[];
 
  /**
   * Whether the activity supports customization for vpc subnets and vpc security groups
   *
   * @default - false
   */
  readonly isCustomizationAvailableForVPC: boolean;
 
  /**
   * Whether the activity supports customization for kms data keys and volume keys
   *
   * @default - false
   */
  readonly isCustomizationAvailableForKMS: boolean;
}
 
/**
 * SageMaker Activity Static Function Options
 */
export interface AccessAwsServicesOptions extends VPCOptions, KMSOptions {
  readonly ecrRepositories: ecr.IRepository[];
  readonly s3Buckets: s3.IBucket[];
}
 
export interface AccessS3AllResourcesOptions extends VPCOptions, KMSOptions {}
 
export interface AccessS3AllResourcesV2Options extends VPCOptions, KMSOptions {}
 
export interface AccessS3BucketsOptions extends VPCOptions, KMSOptions {
  readonly s3Buckets: s3.IBucket[];
}
 
export interface ManageJobsOptions extends VPCOptions, KMSOptions {
  readonly rolesToPass: iam.IRole[];
}
 
export interface ManageEndpointsOptions extends VPCOptions, KMSOptions {}
 
export interface ManageExperimentsOptions extends VPCOptions, KMSOptions {}
 
export interface ManageGlueTablesOptions extends VPCOptions, KMSOptions {
  readonly s3Buckets: s3.IBucket[];
  readonly glueDatabaseNames: string[];
}
 
export interface ManageModelsOptions extends VPCOptions, KMSOptions {
  readonly rolesToPass: iam.IRole[];
}
 
export interface ManagePipelinesOptions extends VPCOptions, KMSOptions {
  readonly rolesToPass: iam.IRole[];
}
 
export interface MonitorModelsOptions extends VPCOptions, KMSOptions {
  readonly rolesToPass: iam.IRole[];
}
 
export interface QueryAthenaGroupsOptions extends VPCOptions, KMSOptions {
  readonly athenaWorkgroupNames: string[];
}
 
export interface RunStudioAppsOptions extends VPCOptions, KMSOptions {
  readonly rolesToPass: iam.IRole[];
}
 
export interface RunStudioAppsV2Options extends VPCOptions, KMSOptions {}
 
export interface VisualizeExperimentsOptions extends VPCOptions, KMSOptions {}
 
/**
 * Global Condition Customization Options
 */
export interface VPCOptions {
  readonly subnets?: ec2.ISubnet[];
  readonly securityGroups?: ec2.ISecurityGroup[];
}
 
export interface KMSOptions {
  readonly dataKeys?: kms.IKey[];
  readonly volumeKeys?: kms.IKey[];
}
 
export class Activity extends Construct {
 
  // Activity Default Names
  public static readonly ACCESS_AWS_SERVICES = 'SM_ComputeExecutionRole';
  public static readonly ACCESS_S3_ALL_RESOURCES = 'SageMakerS3AllResourcesPolicyTemplate';
  public static readonly ACCESS_S3_BUCKETS = 'SageMakerS3BucketPolicyTemplate';
  public static readonly MANAGE_ENDPOINTS_ACTIVITY_NAME = 'SM_EndpointDeployment';
  public static readonly MANAGE_EXPERIMENTS_ACTIVITY_NAME = 'SM_ExperimentsManagement';
  public static readonly MANAGE_GLUE_TABLES_ACTIVITY_NAME = 'SM_GlueTableManagement';
  public static readonly MANAGE_JOBS_ACTIVITY_NAME = 'SM_CommonJobManagement';
  public static readonly MANAGE_MODELS_ACTIVITY_NAME = 'SM_ModelManagement';
  public static readonly MANAGE_PIPELINES_ACTIVITY_NAME = 'SM_PipelineManagement';
  public static readonly MONITOR_MODELS_ACTIVITY_NAME = 'SM_ModelMonitoring';
  public static readonly QUERY_ATHENA_WORKGROUPS = 'SM_AthenaQueryAccess';
  public static readonly RUN_STUDIO_APPS = 'SM_StudioAppPermissions';
  public static readonly VISUALIZE_EXPERIMENTS = 'SM_ExperimentsVisualization';
 
  // Activity Default Parameter Values
  public static readonly ATHENA_WORKGROUP_NAMES_DEFAULT_VALUE = ['primary'];
 
  // Activity Selection Functions
  public static accessAwsServices(scope: Construct, id: string, options: AccessAwsServicesOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.ACCESS_AWS_SERVICES,
      isCustomizationAvailableForVPC: true,
      isCustomizationAvailableForKMS: true,
      ecrRepositories: options.ecrRepositories,
      s3Buckets: options.s3Buckets,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static accessS3AllResources(scope: Construct, id: string, options: AccessS3AllResourcesOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.ACCESS_S3_ALL_RESOURCES,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static accessS3AllResourcesV2(scope: Construct, id: string, options: AccessS3AllResourcesV2Options): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.ACCESS_S3_ALL_RESOURCES,
      version: 2,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static accessS3Buckets(scope: Construct, id: string, options: AccessS3BucketsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.ACCESS_S3_BUCKETS,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
      s3Buckets: options.s3Buckets,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static manageEndpoints(scope: Construct, id: string, options: ManageEndpointsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_ENDPOINTS_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: true,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static manageExperiments(scope: Construct, id: string, options: ManageExperimentsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_EXPERIMENTS_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static manageGlueTables(scope: Construct, id: string, options: ManageGlueTablesOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_GLUE_TABLES_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: true,
      s3Buckets: options.s3Buckets,
      glueDatabaseNames: options.glueDatabaseNames,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static manageJobs(scope: Construct, id: string, options: ManageJobsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_JOBS_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: true,
      isCustomizationAvailableForKMS: true,
      rolesToPass: options.rolesToPass,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static manageModels(scope: Construct, id: string, options: ManageModelsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_MODELS_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: true,
      isCustomizationAvailableForKMS: false,
      rolesToPass: options.rolesToPass,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static managePipelines(scope: Construct, id: string, options: ManagePipelinesOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MANAGE_PIPELINES_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
      rolesToPass: options.rolesToPass,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static monitorModels(scope: Construct, id: string, options: MonitorModelsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.MONITOR_MODELS_ACTIVITY_NAME,
      isCustomizationAvailableForVPC: true,
      isCustomizationAvailableForKMS: true,
      rolesToPass: options.rolesToPass,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static queryAthenaGroups(scope: Construct, id: string, options: QueryAthenaGroupsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.QUERY_ATHENA_WORKGROUPS,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
      athenaWorkgroupNames: options.athenaWorkgroupNames,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static runStudioApps(scope: Construct, id: string, options: RunStudioAppsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.RUN_STUDIO_APPS,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: true,
      rolesToPass: options.rolesToPass,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static runStudioAppsV2(scope: Construct, id: string, options: RunStudioAppsV2Options): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.RUN_STUDIO_APPS,
      version: 2,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  public static visualizeExperiments(scope: Construct, id: string, options: VisualizeExperimentsOptions): Activity {
 
    const activity = new Activity(scope, id, {
      activityName: Activity.VISUALIZE_EXPERIMENTS,
      isCustomizationAvailableForVPC: false,
      isCustomizationAvailableForKMS: false,
    });
 
    activity.customizeVPC(options.subnets, options.securityGroups);
    activity.customizeKMS(options.dataKeys, options.volumeKeys);
 
    return activity;
  }
 
  // Single Value Replacement Parameter Names
  private static readonly ACCOUNT_ID_PARAMETER_NAME = 'AccountId';
  private static readonly REGION_PARAMETER_NAME = 'Region';
 
  // Multi Value Replacement Parameter Names
  private static readonly ATHENA_WORKGROUP_NAMES_PARAMETER_NAME = 'WorkGroupNames';
  private static readonly ECR_REPOSITORIES_PARAMETER_NAME = 'ECRRepoArns';
  private static readonly GLUE_DATABASE_NAMES_PARAMETER_NAME = 'GlueDbNames';
  private static readonly PASSED_ROLES_PARAMETER_NAME = 'PassRoles';
  private static readonly S3_BUCKETS_PARAMETER_NAME = 'S3Buckets';
  private static readonly SUBNETS_PARAMETER_NAME = 'VpcSubnets';
  private static readonly SECURITY_GROUPS_PARAMETER_NAME = 'VpcSecurityGroups';
  private static readonly DATA_KEYS_PARAMETER_NAME = 'DataKmsKeys';
  private static readonly VOLUME_KEYS_PARAMETER_NAME = 'VolumeKmsKeys';
 
  /**
   * Get grant options from statement for Grant.addToPrincipal() function
   * @param identity identity to be granted permissions
   * @param statement the statement from which to get the actions, resources and conditions
   * @returns - The options for the Grant.addToPrincipal() function
   */
  private static getGrantOptions(identity: iam.IGrantable, statement: any): iam.GrantOnPrincipalOptions {
    const actions = typeof(statement.Action) === 'string' ? [statement.Action] : statement.Action;
    const resourceArns = typeof(statement.Resource) === 'string' ? [statement.Resource] : statement.Resource;
    const conditions = statement.Condition ? statement.Condition: {};
 
    return {
      grantee: identity,
      actions: actions,
      resourceArns: resourceArns,
      conditions: conditions,
    };
  }
 
  public readonly activityName: string;
  public readonly version: number;
 
  private readonly singleValueReplacements: Map<string, string>;
  private readonly multiValueReplacements: Map<string, string[]>;
 
  private readonly isCustomizationAvailableForVPC: boolean;
  private readonly isCustomizationAvailableForKMS: boolean;
  public isVPCCustomized: boolean = false;
  public isKMSCustomized: boolean = false;
 
  private constructor(scope: Construct, id: string, props: ActivityProps) {
    super(scope, id);
 
    // Validate passed values for multi value replacements
    if (props.athenaWorkgroupNames && props.athenaWorkgroupNames.length === 0) {
      throw TypeError(`The value of the athenaWorkgroupNames parameter should be of type ${props.athenaWorkgroupNames} with at least one element.`);
    }
 
    for (const athenaWorkGroupName of props.athenaWorkgroupNames || []) {
      if (!parameterValidationRegex.test(athenaWorkGroupName)) {
        throw TypeError(`The value ${athenaWorkGroupName} of athenaWorkgroupNames array element contains an invalid character,
             athenaWorkgroupNames elements must be alphanumeric with no spaces and only the special characters: _/:.-`);
      }
    }
 
    if (props.ecrRepositories && props.ecrRepositories.length === 0) {
      throw TypeError(`The value of the ecrRepositories parameter should be of type ${props.ecrRepositories} with at least one element.`);
    }
 
    if (props.glueDatabaseNames && props.glueDatabaseNames.length === 0) {
      throw TypeError(`The value of the glueDatabaseNames parameter should be of type ${props.glueDatabaseNames} with at least one element.`);
    }
 
    for (const glueDatabaseName of props.glueDatabaseNames || []) {
      if (!parameterValidationRegex.test(glueDatabaseName)) {
        throw TypeError(`The value ${glueDatabaseName} of glueDatabaseNames array element contains an invalid character,
             glueDatabaseNames elements must be alphanumeric with no spaces and only the special characters: _/:.-`);
      }
    }
 
    if (props.rolesToPass && props.rolesToPass.length === 0) {
      throw TypeError(`The value of the rolesToPass parameter should be of type ${props.rolesToPass} with at least one element.`);
    }
 
    if (props.s3Buckets && props.s3Buckets.length === 0) {
      throw TypeError(`The value of the s3Buckets parameter should be of type ${props.s3Buckets} with at least one element.`);
    }
 
    this.activityName = props.activityName;
    this.version = props.version || 1;
 
    // Set single value replacements
    this.singleValueReplacements = new Map<string, string>();
    this.singleValueReplacements.set(Activity.ACCOUNT_ID_PARAMETER_NAME,
      cdk.Token.isUnresolved(cdk.Stack.of(this).account) ? '*' : cdk.Stack.of(this).account);
    this.singleValueReplacements.set(Activity.REGION_PARAMETER_NAME,
      cdk.Token.isUnresolved(cdk.Stack.of(this).region) ? '*' : cdk.Stack.of(this).region);
 
    // Set multi value replacements
    this.multiValueReplacements = new Map<string, string[]>();
 
    this.multiValueReplacements.set(Activity.ATHENA_WORKGROUP_NAMES_PARAMETER_NAME, props.athenaWorkgroupNames ?? []);
    this.multiValueReplacements.set(Activity.ECR_REPOSITORIES_PARAMETER_NAME, (props.ecrRepositories ?? []).map(
      (ecrRepository) => ecrRepository.repositoryArn));
    this.multiValueReplacements.set(Activity.GLUE_DATABASE_NAMES_PARAMETER_NAME, props.glueDatabaseNames ?? []);
    this.multiValueReplacements.set(Activity.PASSED_ROLES_PARAMETER_NAME, (props.rolesToPass ?? []).map(
      (role) => role.roleArn));
    this.multiValueReplacements.set(Activity.S3_BUCKETS_PARAMETER_NAME, (props.s3Buckets ?? []).map(
      (s3Bucket) => s3Bucket.bucketName));
 
    this.isCustomizationAvailableForVPC = props.isCustomizationAvailableForVPC;
    this.isCustomizationAvailableForKMS = props.isCustomizationAvailableForKMS;
  }
 
  /**
   * Creates role with permissions of activity
   * @param scope the Construct scope.
   * @param id the resource id.
   * @param roleNameSuffix the name suffix of the role that will be created, if empty the role will have the name of the activity.
   * @param roleDescription the description of the role that will be created.
   * @returns - The role that is created with the permissions of the activity
   */
  public createRole(scope: Construct, id: string, roleNameSuffix: string, roleDescription: string = ''): iam.IRole {
    if (!roleNameSuffix || !roleNameSuffix.length) {
      throw TypeError('The role name should be a non empty string');
    }
 
    const policy = this.createPolicy(scope);
 
    const role = new iam.Role(scope, id, {
      roleName: `SageMaker-${roleNameSuffix}`,
      description: roleDescription,
      assumedBy: this.createPrincipal(),
    });
 
    role.attachInlinePolicy(policy);
 
    return role;
  }
 
  /**
   * Creates policy with permissions of activity
   * @param scope the Construct scope.
   * @returns - The policy that is created with the permissions of the activity
   */
  public createPolicy(scope: Construct): iam.Policy {
    const templateFile = getTemplateFile(this.activityName, this.version, this.isVPCCustomized, this.isKMSCustomized);
    const timestamp = Date.now().toString();
    const templateName = `${templateFile.name}_V${this.version}_${timestamp}`;
    const templateAsString = JSON.stringify(templateFile.templateJson);
 
    // Replace singleValueReplacements and multiValueReplacements in templateDocument
    const policyDocumentJSON = JSON.parse(replacePatterns(templateAsString, this.singleValueReplacements, this.multiValueReplacements));
 
    const policyDocument = iam.PolicyDocument.fromJson(policyDocumentJSON);
 
    return new iam.Policy(scope, `${templateName} id`, {
      policyName: templateName,
      document: policyDocument,
    });
  }
 
  /**
   * Creates ML Activity service principal using ML Activity trust template
   * @returns - The service principal of the ML Activity
   */
  public createPrincipal(): iam.ServicePrincipal {
    const templateFile = getTemplateFile(this.activityName, this.version, this.isVPCCustomized, this.isKMSCustomized);
    const templateAsString = JSON.stringify(templateFile.trustTemplateJson);
 
    // Replace singleValueReplacements and multiValueReplacements in templateDocument
    const policyDocumentJSON = JSON.parse(replacePatterns(templateAsString, this.singleValueReplacements, this.multiValueReplacements));
    const policyDocumentStatement = policyDocumentJSON.Statement[0];
 
    return new iam.ServicePrincipal(policyDocumentStatement.Principal.Service, {
      conditions: policyDocumentStatement.Condition,
    });
  }
 
  /**
   * Grant permissions of activity to identity
   * @param identity identity to be granted permissions
   * @returns - The grant with the permissions granted to the identity
   */
  public grantPermissionsTo(identity: iam.IGrantable): iam.Grant {
    const templateFile = getTemplateFile(this.activityName, this.version, this.isVPCCustomized, this.isKMSCustomized);
    const templateAsString = JSON.stringify(templateFile.templateJson);
 
    // Replace singleValueReplacements and multiValueReplacements in templateDocument
    let policyDocumentJSON = JSON.parse(replacePatterns(templateAsString, this.singleValueReplacements, this.multiValueReplacements));
 
    let grant = iam.Grant.addToPrincipal(Activity.getGrantOptions(identity, policyDocumentJSON.Statement[0]));
 
    for (let i = 1; i < policyDocumentJSON.Statement.length; i++) {
      grant = grant.combine(iam.Grant.addToPrincipal(Activity.getGrantOptions(identity, policyDocumentJSON.Statement[i])));
    }
 
    return grant;
  }
 
  public customizeVPC(subnets?: ec2.ISubnet[], securityGroups?: ec2.ISecurityGroup[]) {
    if (!this.isCustomizationAvailableForVPC || (!subnets && !securityGroups)) {
      return;
    }
 
    if (!subnets || !subnets.length) {
      throw TypeError('The array subnets must be of type ec2.ISubnet[] with at least one element.');
    }
    if (!securityGroups || !securityGroups.length) {
      throw TypeError('The array securityGroups must be of type ec2.ISecurityGroup[] with at least one element.');
    }
 
    this.multiValueReplacements.set(Activity.SUBNETS_PARAMETER_NAME, subnets.map((subnet) => subnet.subnetId));
    this.multiValueReplacements.set(Activity.SECURITY_GROUPS_PARAMETER_NAME, securityGroups.map(
      (securityGroup) => securityGroup.securityGroupId));
 
    this.isVPCCustomized = true;
  }
 
  public customizeKMS(dataKeys?: kms.IKey[], volumeKeys?: kms.IKey[]) {
    if (!this.isCustomizationAvailableForKMS || (!dataKeys && !volumeKeys)) {
      return;
    }
 
    if (!dataKeys || !dataKeys.length) {
      throw TypeError('The array dataKeys must be of type kms.IKey[] with at least one element.');
    }
    if (!volumeKeys || !volumeKeys.length) {
      throw TypeError('The array volumeKeys must be of type kms.IKey[] with at least one element.');
    }
 
    this.multiValueReplacements.set(Activity.DATA_KEYS_PARAMETER_NAME, dataKeys.map((key) => key.keyId));
    this.multiValueReplacements.set(Activity.VOLUME_KEYS_PARAMETER_NAME, volumeKeys.map((key) => key.keyId));
 
    this.isKMSCustomized = true;
  }
 
}