|
| 1 | +/** |
| 2 | + * Copyright 2021 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 | any; |
| 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.IVpc; |
| 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 | + |
| 81 | +/** |
| 82 | + * @summary The LambdaToSagemakerEndpoint class. |
| 83 | + */ |
| 84 | +export class LambdaToSagemakerEndpoint extends cdk.Construct { |
| 85 | + public readonly lambdaFunction: lambda.Function; |
| 86 | + public readonly sagemakerEndpoint: sagemaker.CfnEndpoint; |
| 87 | + public readonly sagemakerEndpointConfig?: sagemaker.CfnEndpointConfig; |
| 88 | + public readonly sagemakerModel?: sagemaker.CfnModel; |
| 89 | + public readonly vpc?: ec2.IVpc; |
| 90 | + |
| 91 | + /** |
| 92 | + * @summary Constructs a new instance of the LambdaToSagemakerEndpoint class. |
| 93 | + * @param {cdk.App} scope - represents the scope for all the resources. |
| 94 | + * @param {string} id - this is a scope-unique id. |
| 95 | + * @param {LambdaToSagemakerEndpointProps} props - user provided props for the construct. |
| 96 | + * @since 1.87.1 |
| 97 | + * @access public |
| 98 | + */ |
| 99 | + constructor(scope: cdk.Construct, id: string, props: LambdaToSagemakerEndpointProps) { |
| 100 | + super(scope, id); |
| 101 | + |
| 102 | + if (props.deployVpc || props.existingVpc) { |
| 103 | + if (props.deployVpc && props.existingVpc) { |
| 104 | + throw new Error('More than 1 VPC specified in the properties'); |
| 105 | + } |
| 106 | + |
| 107 | + // create the VPC |
| 108 | + this.vpc = defaults.buildVpc(scope, { |
| 109 | + defaultVpcProps: defaults.DefaultIsolatedVpcProps(), |
| 110 | + existingVpc: props.existingVpc, |
| 111 | + userVpcProps: props.vpcProps, |
| 112 | + constructVpcProps: { |
| 113 | + enableDnsHostnames: true, |
| 114 | + enableDnsSupport: true, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + // Add S3 VPC Gateway Endpoint, required by Sagemaker to access Models artifacts via AWS private network |
| 119 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.S3); |
| 120 | + // Add SAGEMAKER_RUNTIME VPC Interface Endpoint, required by the lambda function to invoke the Sagemaker endpoint |
| 121 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.SAGEMAKER_RUNTIME); |
| 122 | + } |
| 123 | + |
| 124 | + // Build Sagemaker Endpoint (inclduing Sagemaker's Endpoint Configuration and Model) |
| 125 | + [this.sagemakerEndpoint, this.sagemakerEndpointConfig, this.sagemakerModel] = defaults.BuildSagemakerEndpoint( |
| 126 | + this, |
| 127 | + { |
| 128 | + ...props, |
| 129 | + vpc: this.vpc, |
| 130 | + } |
| 131 | + ); |
| 132 | + |
| 133 | + // Setup the Lambda function |
| 134 | + this.lambdaFunction = defaults.buildLambdaFunction(this, { |
| 135 | + existingLambdaObj: props.existingLambdaObj, |
| 136 | + lambdaFunctionProps: props.lambdaFunctionProps, |
| 137 | + vpc: this.vpc, |
| 138 | + }); |
| 139 | + |
| 140 | + // Add SAGEMAKER_ENDPOINT_NAME environment variable |
| 141 | + this.lambdaFunction.addEnvironment('SAGEMAKER_ENDPOINT_NAME', this.sagemakerEndpoint.attrEndpointName); |
| 142 | + |
| 143 | + // Add permission to invoke the SageMaker endpoint |
| 144 | + this.lambdaFunction.addToRolePolicy( |
| 145 | + new iam.PolicyStatement({ |
| 146 | + actions: ['sagemaker:InvokeEndpoint'], |
| 147 | + resources: [this.sagemakerEndpoint.ref], |
| 148 | + }) |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments