|
| 1 | +/** |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as cdk from '@aws-cdk/core'; |
| 15 | +import * as lambda from '@aws-cdk/aws-lambda'; |
| 16 | +import * as ec2 from '@aws-cdk/aws-ec2'; |
| 17 | +import * as iam from '@aws-cdk/aws-iam'; |
| 18 | +import * as sagemaker from '@aws-cdk/aws-sagemaker'; |
| 19 | +import * as defaults from '@aws-solutions-constructs/core'; |
| 20 | + |
| 21 | +/** |
| 22 | + * @summary The properties for the LambdaToSageMakerEndpoint class |
| 23 | + */ |
| 24 | +export interface LambdaToSageMakerEndpointProps { |
| 25 | + /** |
| 26 | + * Existing instance of Lambda Function object, if this is set then the lambdaFunctionProps is ignored |
| 27 | + * |
| 28 | + * @default - None |
| 29 | + */ |
| 30 | + readonly existingLambdaObj?: lambda.Function; |
| 31 | + /** |
| 32 | + * User provided props to override the default props for the Lambda function |
| 33 | + * |
| 34 | + * @default - Default props are used |
| 35 | + */ |
| 36 | + readonly lambdaFunctionProps?: lambda.FunctionProps; |
| 37 | + /** |
| 38 | + * Existing SageMaker Enpoint object, if this is set then the modelProps, endpointConfigProps, and endpointProps are ignored |
| 39 | + * |
| 40 | + * @default - None |
| 41 | + */ |
| 42 | + readonly existingSageMakerEndpointObj?: sagemaker.CfnEndpoint; |
| 43 | + /** |
| 44 | + * User provided props to create SageMaker Model |
| 45 | + * |
| 46 | + * @default - None |
| 47 | + */ |
| 48 | + readonly modelProps?: sagemaker.CfnModelProps; |
| 49 | + /** |
| 50 | + * User provided props to create SageMaker Endpoint Configuration |
| 51 | + * |
| 52 | + * @default - Default props are used |
| 53 | + */ |
| 54 | + readonly endpointConfigProps?: sagemaker.CfnEndpointConfigProps; |
| 55 | + /** |
| 56 | + * User provided props to create SageMaker Endpoint |
| 57 | + * |
| 58 | + * @default - Default props are used |
| 59 | + */ |
| 60 | + readonly endpointProps?: sagemaker.CfnEndpointProps; |
| 61 | + /** |
| 62 | + * An existing VPC for the construct to use (construct will NOT create a new VPC in this case) |
| 63 | + * |
| 64 | + * @default - None |
| 65 | + */ |
| 66 | + readonly existingVpc?: ec2.Vpc; |
| 67 | + /** |
| 68 | + * Properties to override default properties if deployVpc is true |
| 69 | + * |
| 70 | + * @default - None |
| 71 | + */ |
| 72 | + readonly vpcProps?: ec2.VpcProps; |
| 73 | + /** |
| 74 | + * Whether to deploy a new VPC |
| 75 | + * |
| 76 | + * @default - false |
| 77 | + */ |
| 78 | + readonly deployVpc?: boolean; |
| 79 | + /** |
| 80 | + * Whether to deploy a natgatway in the new VPC (if deployVpc is true). |
| 81 | + * If deployNatGateway is true, the construct creates Public and Private subnets. |
| 82 | + * Otherwise, it creates Isolated subnets only |
| 83 | + * |
| 84 | + * @default - false |
| 85 | + */ |
| 86 | + readonly deployNatGateway?: boolean; |
| 87 | + /** |
| 88 | + * IAM Role, with all required permissions, to be assumed by SageMaker to create resources |
| 89 | + * The Role is not required if existingSageMakerEndpointObj is provided. |
| 90 | + * |
| 91 | + * @default - None |
| 92 | + */ |
| 93 | + readonly role?: iam.Role; |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @summary The LambdaToSageMakerEndpoint class. |
| 98 | + */ |
| 99 | +export class LambdaToSageMakerEndpoint extends cdk.Construct { |
| 100 | + public readonly lambdaFunction: lambda.Function; |
| 101 | + public readonly sageMakerEndpoint: sagemaker.CfnEndpoint; |
| 102 | + public readonly sageMakerEndpointConfig?: sagemaker.CfnEndpointConfig; |
| 103 | + public readonly sageMakerModel?: sagemaker.CfnModel; |
| 104 | + public readonly vpc?: ec2.Vpc; |
| 105 | + |
| 106 | + /** |
| 107 | + * @summary Constructs a new instance of the LambdaToSageMakerEndpoint class. |
| 108 | + * @param {cdk.App} scope - represents the scope for all the resources. |
| 109 | + * @param {string} id - this is a a scope-unique id. |
| 110 | + * @param {LambdaToSageMakerEndpointProps} props - user provided props for the construct. |
| 111 | + * @since 1.76.0 |
| 112 | + * @access public |
| 113 | + */ |
| 114 | + constructor(scope: cdk.Construct, id: string, props: LambdaToSageMakerEndpointProps) { |
| 115 | + super(scope, id); |
| 116 | + |
| 117 | + if (props.deployVpc || props.existingVpc) { |
| 118 | + if (props.deployVpc && props.existingVpc) { |
| 119 | + throw new Error('More than 1 VPC specified in the properties'); |
| 120 | + } |
| 121 | + |
| 122 | + // If deployNatGateway is true, create Public and Private subnets. Otherwise, create Isolated subnets only |
| 123 | + const subnetConfiguration: ec2.SubnetConfiguration[] = props.deployNatGateway |
| 124 | + ? [ |
| 125 | + { |
| 126 | + cidrMask: 18, |
| 127 | + name: 'Public', |
| 128 | + subnetType: ec2.SubnetType.PUBLIC, |
| 129 | + }, |
| 130 | + { |
| 131 | + cidrMask: 18, |
| 132 | + name: 'Private', |
| 133 | + subnetType: ec2.SubnetType.PRIVATE, |
| 134 | + }, |
| 135 | + ] |
| 136 | + : [ |
| 137 | + { |
| 138 | + cidrMask: 18, |
| 139 | + name: 'Isolated', |
| 140 | + subnetType: ec2.SubnetType.ISOLATED, |
| 141 | + }, |
| 142 | + ]; |
| 143 | + |
| 144 | + // create the VPC |
| 145 | + this.vpc = defaults.buildVpc(scope, { |
| 146 | + existingVpc: props.existingVpc, |
| 147 | + userVpcProps: props.vpcProps, |
| 148 | + constructVpcProps: { |
| 149 | + enableDnsHostnames: true, |
| 150 | + enableDnsSupport: true, |
| 151 | + // set # NatGateways to 2 if deployNatGateway is true. Otherwise, 0 |
| 152 | + natGateways: props.deployNatGateway ? 2 : 0, |
| 153 | + subnetConfiguration, |
| 154 | + }, |
| 155 | + }); |
| 156 | + |
| 157 | + // Add S3 VPC Gateway Endpint, required by SageMaker to access Models artifacts via AWS private network |
| 158 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.S3); |
| 159 | + // Add SAGEMAKER_RUNTIME VPC Interface Endpint, required by the lambda function to invoke the SageMaker endpoint |
| 160 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.SAGEMAKER_RUNTIME); |
| 161 | + } |
| 162 | + |
| 163 | + // Build SageMaker Endpoint (inclduing SageMaker's Endpoint Configuration and Model) |
| 164 | + [this.sageMakerEndpoint, this.sageMakerEndpointConfig, this.sageMakerModel] = defaults.BuildSageMakerEndpoint( |
| 165 | + this, |
| 166 | + { |
| 167 | + ...props, |
| 168 | + vpc: this.vpc, |
| 169 | + } |
| 170 | + ); |
| 171 | + |
| 172 | + // Setup the Lambda function |
| 173 | + this.lambdaFunction = defaults.buildLambdaFunction(this, { |
| 174 | + existingLambdaObj: props.existingLambdaObj, |
| 175 | + lambdaFunctionProps: props.lambdaFunctionProps, |
| 176 | + vpc: this.vpc, |
| 177 | + }); |
| 178 | + |
| 179 | + // Add ENDPOINT_NAME environment variable |
| 180 | + this.lambdaFunction.addEnvironment('ENDPOINT_NAME', this.sageMakerEndpoint.attrEndpointName); |
| 181 | + |
| 182 | + // Add permission to invoke the SageMaker endpoint |
| 183 | + this.lambdaFunction.addToRolePolicy( |
| 184 | + new iam.PolicyStatement({ |
| 185 | + actions: ['sagemaker:InvokeEndpoint'], |
| 186 | + resources: [this.sageMakerEndpoint.ref], |
| 187 | + }) |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments