|
| 1 | +import * as cdk from '@aws-cdk/core'; |
| 2 | +import ec2 = require('@aws-cdk/aws-ec2'); |
| 3 | +import iam = require('@aws-cdk/aws-iam'); |
| 4 | +import rds = require('@aws-cdk/aws-rds'); |
| 5 | +import glue = require('@aws-cdk/aws-glue'); |
| 6 | +import s3 = require('@aws-cdk/aws-s3'); |
| 7 | +import s3assets = require('@aws-cdk/aws-s3-assets'); |
| 8 | +import sagemaker = require('@aws-cdk/aws-sagemaker'); |
| 9 | +import { DataSetStack, DataSetStackProps} from './dataset-stack'; |
| 10 | + |
| 11 | +export interface AnalyticsStackProps extends cdk.StackProps{ |
| 12 | + targetVpc: ec2.Vpc |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +export class AnalyticsStack extends cdk.Stack { |
| 17 | + |
| 18 | + public readonly NotebookRole: iam.Role; |
| 19 | + |
| 20 | + constructor(scope: cdk.Construct, id: string, props: AnalyticsStackProps) { |
| 21 | + super(scope, id, props); |
| 22 | + |
| 23 | + |
| 24 | + const notebookSg = new ec2.SecurityGroup(this, 'notebookSg',{ |
| 25 | + vpc: props.targetVpc |
| 26 | + }); |
| 27 | + |
| 28 | + const athenaStagingDirectory = new s3.Bucket(this, 'athenaStagingDir', {}); |
| 29 | + |
| 30 | + const lifecycleCode = [ |
| 31 | + {"content": cdk.Fn.base64(` |
| 32 | + wget -O /home/ec2-user/SageMaker/opentargets.chembl.example.ipynb https://raw.githubusercontent.com/paulu-aws/chembl-opentargets-data-lake-example/master/scripts/sagemaker.opentargets.chembl.example.ipynb |
| 33 | + sudo chown ec2-user /home/ec2-user/SageMaker/opentargets.chembl.example.ipynb |
| 34 | + sed -i 's/XXXXAthenaStagingDirectoryXXXX/${athenaStagingDirectory.bucketName}/g' /home/ec2-user/SageMaker/opentargets.chembl.example.ipynb |
| 35 | + sed -i 's/XXXXAthenaRegionXXXX/${cdk.Stack.of(this).region}/g' /home/ec2-user/SageMaker/opentargets.chembl.example.ipynb |
| 36 | + `) } |
| 37 | + ]; |
| 38 | + const sageMakerIntanceLifecyclePolicy = new sagemaker.CfnNotebookInstanceLifecycleConfig(this, 'notebookLifecyclePolicy', { |
| 39 | + notebookInstanceLifecycleConfigName: "Boostrap-Chembl-OpenTargets-Demo-Notebook", |
| 40 | + onStart: lifecycleCode |
| 41 | + |
| 42 | + }); |
| 43 | + |
| 44 | + const notebookPolicy = { |
| 45 | + "Version": "2012-10-17", |
| 46 | + "Statement": [ |
| 47 | + { |
| 48 | + "Effect": "Allow", |
| 49 | + "Action": [ |
| 50 | + "cloudwatch:PutMetricData", |
| 51 | + "logs:CreateLogStream", |
| 52 | + "logs:PutLogEvents", |
| 53 | + "logs:CreateLogGroup", |
| 54 | + "logs:DescribeLogStreams", |
| 55 | + ], |
| 56 | + "Resource": "*" |
| 57 | + } |
| 58 | + ] |
| 59 | + }; |
| 60 | + |
| 61 | + const notebookPolicyDoc = iam.PolicyDocument.fromJson(notebookPolicy); |
| 62 | + |
| 63 | + this.NotebookRole = new iam.Role(this, 'notebookInstanceRole', { |
| 64 | + roleName: "chemblOpenTargetsNotebookRole", |
| 65 | + assumedBy: new iam.ServicePrincipal('sagemaker'), |
| 66 | + inlinePolicies: { |
| 67 | + "notebookPermissions": notebookPolicyDoc |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + athenaStagingDirectory.grantReadWrite(this.NotebookRole); |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + new sagemaker.CfnNotebookInstance(this, 'analyticsNotebook', { |
| 76 | + instanceType : 'ml.t2.medium', |
| 77 | + volumeSizeInGb: 100, |
| 78 | + securityGroupIds: [notebookSg.securityGroupId], |
| 79 | + subnetId: props.targetVpc.selectSubnets({subnetType: ec2.SubnetType.PRIVATE}).subnetIds[0], |
| 80 | + notebookInstanceName: "Chembl-OpenTargets-Demo-Notebook", |
| 81 | + roleArn: this.NotebookRole.roleArn, |
| 82 | + directInternetAccess: 'Disabled', |
| 83 | + lifecycleConfigName: sageMakerIntanceLifecyclePolicy.notebookInstanceLifecycleConfigName |
| 84 | + }); |
| 85 | + |
| 86 | + |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments