{"id":2641,"date":"2018-01-14T11:15:59","date_gmt":"2018-01-14T17:15:59","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=2641"},"modified":"2018-01-14T11:15:59","modified_gmt":"2018-01-14T17:15:59","slug":"using-a-lambda-function-to-make-aml-real-time-predictions","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/2641","title":{"rendered":"Using a lambda function to make AML real time predictions"},"content":{"rendered":"<p>When you are making real time AML predictions against an endpoint, you can run the prediction code (<a href=\"https:\/\/github.com\/mooreds\/amazonmachinelearning-anintroduction\/blob\/master\/prediction\/predict.py\">sample code<\/a>) locally.\u00a0 However, leveraging AWS Lambda can let you build a system that accesses predictions without any servers.\u00a0 This system will likely be cheaper and scale better than running on your own servers, and you can also trigger predictions on a wide variety of events without writing any polling code.<\/p>\n<p>Here&#8217;s a scenario.\u00a0 You have a model that predicts income level based on user data, which you are going to use for marketing purposes.\u00a0 The user data is place on S3 by a different process at varying intervals. You want to process each record as it comes in and generate a prediction.\u00a0 (If you didn&#8217;t care about near real time processing, you could run a periodic batch AML job.\u00a0 This job could also be triggered by lambda.)<\/p>\n<p>So, the plan is to set up a lambda function to monitor the S3 location and whenever a new object is added, run the prediction.<\/p>\n<p>A record will obviously depend on what your model expects.\u00a0 For one of the the models I built for my AML video course, the record looks like this (<a href=\"https:\/\/github.com\/mooreds\/amazonmachinelearning-anintroduction\/blob\/master\/data\/adult.names\">data format and details<\/a>):<\/p>\n<p><code>22, Local-gov, 108435, Masters, 14, Married-civ-spouse, Prof-specialty, Husband, White, Male, 0, 0, 80, United-States<\/code><\/p>\n<p>You will need to enable a real time endpoint for your model.<\/p>\n<p>You also need to create IAM policies which allow access to cloudwatch logs, readonly access to s3, and access to the AML model, and associate all of these with an IAM role which your lambda function can assume.\u00a0 Here are the policies I have associated with my lambda function (the two <code>describe<\/code> policies can be found in my <a href=\"https:\/\/github.com\/mooreds\/amazonmachinelearning-anintroduction\">github repo<\/a>):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2645 aligncenter\" src=\"http:\/\/www.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/01\/iam-policy-lambda-prediction-v2.png\" alt=\"\" width=\"636\" height=\"357\" srcset=\"http:\/\/edit.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/01\/iam-policy-lambda-prediction-v2.png 636w, http:\/\/edit.mooreds.com\/wordpress\/wp-content\/uploads\/2018\/01\/iam-policy-lambda-prediction-v2-300x168.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>You then create a lambda function which will be triggered when a new file is added to S3.\u00a0 Here&#8217;s a <a href=\"https:\/\/github.com\/mooreds\/amazonmachinelearning-anintroduction\/blob\/master\/prediction-lambda\/adultincomev1predict.yaml\">SAML file which defines the trigger<\/a> (you&#8217;ll have to update the reference to the role you created and your bucket name and path).\u00a0 <a href=\"https:\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/deploying-lambda-apps.html#serverless_app\">More about SAML<\/a>.<\/p>\n<p>Then, you need to write the lambda function which will pull the file content from S3 and run a prediction. <a href=\"https:\/\/github.com\/mooreds\/amazonmachinelearning-anintroduction\/blob\/master\/prediction-lambda\/s3lambdapredict.py\">Here&#8217;s that code<\/a>.\u00a0 It&#8217;s similar to prediction code that you might run locally, except how it gets the value string.\u00a0 We read the values string from the S3 object on line 31.<\/p>\n<p>This code is <strong>prototype quality only<\/strong>.\u00a0 The sample code accesses the prediction and then writes to stdout.\u00a0 That is fine for sample code, but in a real world scenario you&#8217;d obviously want to take further actions.\u00a0 Perhaps have the lambda function update a database row, add another file to S3 or call an API. You&#8217;d also want to have some error handling in case the data in the S3 file wasn&#8217;t in the format the model expected.\u00a0 You&#8217;d also want to lock down the S3 access allowed (the policy above allows readonly access to all S3 resources, which is not a good idea for production code).<\/p>\n<p>Finally, S3 is one possible source of input, both others like SNS or Kinesis might be a better fit.\u00a0 You could also tie into the AWS API Gateway and leverage the features of that service, including authentication, throttling and billing.\u00a0 In that case, you&#8217;d want the lambda function to return the prediction value to the end user.<\/p>\n<p>Using AWS Lambda to access your real time prediction AML endpoint allows you to make predictions against single records in near real time without running any infrastructure of your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are making real time AML predictions against an endpoint, you can run the prediction code (sample code) locally.\u00a0 However, leveraging AWS Lambda can let you build a system [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80,79],"tags":[],"class_list":["post-2641","post","type-post","status-publish","format-standard","hentry","category-amazon-machine-learning","category-aws"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2641"}],"version-history":[{"count":6,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2641\/revisions"}],"predecessor-version":[{"id":2649,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2641\/revisions\/2649"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}