|
| 1 | +import json |
| 2 | +import io |
| 3 | +import zipfile |
| 4 | +import subprocess |
| 5 | +import boto3 |
| 6 | + |
| 7 | +# aws related |
| 8 | +REGION = "eu-central-1" |
| 9 | +ENDPOINT_URL = "http://localhost:4566" |
| 10 | + |
| 11 | +AWS_CONFIG = {"region_name": REGION, "endpoint_url": ENDPOINT_URL} |
| 12 | + |
| 13 | +# sagemaker config |
| 14 | +SAGEMAKER_CONTAINER_IMAGE_URI = "763104351884.dkr.ecr.eu-central-1.amazonaws.com/pytorch-inference:1.10.2-cpu-py38-ubuntu20.04-sagemaker" |
| 15 | +MODEL_BUCKET_NAME = "mnist-model-bucket" |
| 16 | +MNIST_MODEL_LOCATION = "ml/results/zip/model.tar.gz" |
| 17 | +MNIST_MODEL_KEY = "model.tar.gz" |
| 18 | +MNIST_MODEL_NAME = "mnist-model" |
| 19 | +MNIST_EP_CONFIGURATION_NAME = "mnist-epc" |
| 20 | +MNIST_EP_NAME = "mnist-endpoint" |
| 21 | + |
| 22 | +# lambda config |
| 23 | +LAMBDA_NAME = "MnistHandlerLambda" |
| 24 | + |
| 25 | +# website config |
| 26 | +WEBSITE_BUCKET_NAME = "mnist-website" |
| 27 | + |
| 28 | + |
| 29 | +def create_sagemaker_endpoint(): |
| 30 | + # create s3 bucket for model hosting |
| 31 | + s3 = boto3.client("s3", **AWS_CONFIG) |
| 32 | + s3.create_bucket( |
| 33 | + Bucket=MODEL_BUCKET_NAME, |
| 34 | + CreateBucketConfiguration={"LocationConstraint": REGION}, |
| 35 | + ) |
| 36 | + |
| 37 | + # upload ml model to s3 |
| 38 | + s3.upload_file( |
| 39 | + Filename=MNIST_MODEL_LOCATION, |
| 40 | + Bucket=MODEL_BUCKET_NAME, |
| 41 | + Key=MNIST_MODEL_KEY, |
| 42 | + ) |
| 43 | + |
| 44 | + # create role for sagemaker to access s3 |
| 45 | + iam = boto3.client("iam", **AWS_CONFIG) |
| 46 | + |
| 47 | + document = { |
| 48 | + "Version": "2012-10-17", |
| 49 | + "Statement": [ |
| 50 | + { |
| 51 | + "Effect": "Allow", |
| 52 | + "Principal": {"Service": "sagemaker.amazonaws.com"}, |
| 53 | + "Action": "sts:AssumeRole", |
| 54 | + } |
| 55 | + ], |
| 56 | + } |
| 57 | + sagemaker_role = iam.create_role( |
| 58 | + RoleName="sagemaker-role", AssumeRolePolicyDocument=json.dumps(document) |
| 59 | + ) |
| 60 | + iam.attach_role_policy( |
| 61 | + RoleName="sagemaker-role", |
| 62 | + PolicyArn="arn:aws:iam::aws:policy/AmazonSageMakerFullAccess", |
| 63 | + ) |
| 64 | + s3_policy = { |
| 65 | + "Version": "2012-10-17", |
| 66 | + "Statement": [ |
| 67 | + { |
| 68 | + "Effect": "Allow", |
| 69 | + "Action": [ |
| 70 | + "s3:GetObject", |
| 71 | + "s3:PutObject", |
| 72 | + "s3:DeleteObject", |
| 73 | + "s3:ListBucket", |
| 74 | + ], |
| 75 | + "Resource": "arn:aws:s3:::*", |
| 76 | + } |
| 77 | + ], |
| 78 | + } |
| 79 | + iam.put_role_policy( |
| 80 | + RoleName="sagemaker-role", |
| 81 | + PolicyName="sagemaker-s3-access", |
| 82 | + PolicyDocument=json.dumps(s3_policy), |
| 83 | + ) |
| 84 | + |
| 85 | + # create sagemaker model and endpoint |
| 86 | + sm = boto3.client("sagemaker", **AWS_CONFIG) |
| 87 | + |
| 88 | + model_data_url = f"s3://{MODEL_BUCKET_NAME}/model.tar.gz" |
| 89 | + |
| 90 | + sm.create_model( |
| 91 | + ModelName=MNIST_MODEL_NAME, |
| 92 | + PrimaryContainer={ |
| 93 | + "Image": SAGEMAKER_CONTAINER_IMAGE_URI, |
| 94 | + "Mode": "SingleModel", |
| 95 | + "ModelDataUrl": model_data_url, |
| 96 | + }, |
| 97 | + ExecutionRoleArn=sagemaker_role["Role"]["Arn"], |
| 98 | + ) |
| 99 | + |
| 100 | + sm.create_endpoint_config( |
| 101 | + EndpointConfigName=MNIST_EP_CONFIGURATION_NAME, |
| 102 | + ProductionVariants=[ |
| 103 | + { |
| 104 | + "VariantName": "single-variant", |
| 105 | + "ModelName": MNIST_MODEL_NAME, |
| 106 | + "ServerlessConfig": { |
| 107 | + "MemorySizeInMB": 6144, |
| 108 | + "MaxConcurrency": 8, |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + ) |
| 113 | + |
| 114 | + sm.create_endpoint( |
| 115 | + EndpointName=MNIST_EP_NAME, |
| 116 | + EndpointConfigName=MNIST_EP_CONFIGURATION_NAME, |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +def create_lambda(): |
| 121 | + # create function for handling requests which are passed onto sagemaker endpoint |
| 122 | + lambda_client = boto3.client("lambda", **AWS_CONFIG) |
| 123 | + |
| 124 | + # Zip up the Lambda code from the specified directory |
| 125 | + with io.BytesIO() as buffer: |
| 126 | + with zipfile.ZipFile(buffer, mode="w") as zip_file: |
| 127 | + zip_file.write("lambda/index.py", arcname="index.py") |
| 128 | + zip_content = buffer.getvalue() |
| 129 | + |
| 130 | + # Create the Lambda function |
| 131 | + response = lambda_client.create_function( |
| 132 | + FunctionName=LAMBDA_NAME, |
| 133 | + Runtime="python3.9", |
| 134 | + Role="arn:aws:iam::000000000000:role/lambda-role", |
| 135 | + Handler="index.lambda_handler", |
| 136 | + Code={"ZipFile": zip_content}, |
| 137 | + Timeout=300, |
| 138 | + Environment={"Variables": {"SAGEMAKER_ENDPOINT_NAME": MNIST_EP_NAME}}, |
| 139 | + ) |
| 140 | + |
| 141 | + response = lambda_client.create_function_url_config( |
| 142 | + FunctionName=LAMBDA_NAME, |
| 143 | + AuthType="NONE", |
| 144 | + Cors={ |
| 145 | + "AllowCredentials": True, |
| 146 | + "AllowMethods": [ |
| 147 | + "*", |
| 148 | + ], |
| 149 | + "AllowOrigins": [ |
| 150 | + "*", |
| 151 | + ], |
| 152 | + "MaxAge": 123, |
| 153 | + }, |
| 154 | + ) |
| 155 | + |
| 156 | + return response["FunctionUrl"] |
| 157 | + |
| 158 | + |
| 159 | +def build_webapp(lambdaUrl: str): |
| 160 | + for env in ["production", "development"]: |
| 161 | + f = open(f"web/.env.{env}", "w") |
| 162 | + f.write(f"REACT_APP_LAMBDA_URL={lambdaUrl}") |
| 163 | + f.close() |
| 164 | + |
| 165 | + # build web app |
| 166 | + subprocess.run("npm run build", cwd="./web", shell=True) |
| 167 | + |
| 168 | + |
| 169 | +def host_website(): |
| 170 | + # create s3 bucket for hosting the web app |
| 171 | + s3 = boto3.client("s3", **AWS_CONFIG) |
| 172 | + s3.create_bucket( |
| 173 | + Bucket=WEBSITE_BUCKET_NAME, |
| 174 | + CreateBucketConfiguration={"LocationConstraint": "eu-central-1"}, |
| 175 | + ) |
| 176 | + |
| 177 | + # Set the bucket policy for static website hosting |
| 178 | + policy = { |
| 179 | + "Version": "2012-10-17", |
| 180 | + "Statement": [ |
| 181 | + { |
| 182 | + "Sid": "PublicReadGetObject", |
| 183 | + "Effect": "Allow", |
| 184 | + "Principal": "*", |
| 185 | + "Action": "s3:GetObject", |
| 186 | + "Resource": f"arn:aws:s3:::{WEBSITE_BUCKET_NAME}/*", |
| 187 | + } |
| 188 | + ], |
| 189 | + } |
| 190 | + s3.put_bucket_policy(Bucket=WEBSITE_BUCKET_NAME, Policy=json.dumps(policy)) |
| 191 | + |
| 192 | + # Upload the website files to the bucket |
| 193 | + subprocess.run(f"awslocal s3 sync web/build s3://{WEBSITE_BUCKET_NAME}", shell=True) |
| 194 | + |
| 195 | + # Enable static website hosting for the bucket |
| 196 | + s3.put_bucket_website( |
| 197 | + Bucket=WEBSITE_BUCKET_NAME, |
| 198 | + WebsiteConfiguration={ |
| 199 | + "ErrorDocument": {"Key": "index.html"}, |
| 200 | + "IndexDocument": {"Suffix": "index.html"}, |
| 201 | + }, |
| 202 | + ) |
| 203 | + |
| 204 | + # Print the URL of the static website |
| 205 | + print( |
| 206 | + f"S3 static website URL: http://{WEBSITE_BUCKET_NAME}.s3-website.localhost.localstack.cloud:4566" |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +def main(): |
| 211 | + create_sagemaker_endpoint() |
| 212 | + |
| 213 | + lambdaUrl = create_lambda() |
| 214 | + |
| 215 | + build_webapp(lambdaUrl) |
| 216 | + |
| 217 | + host_website() |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == "__main__": |
| 221 | + main() |
0 commit comments