Real-Time Prediction of Credit Card Fraud deployed on AWS using Spark and XGBoost (Part III)

Jie Zhang
4 min readMay 30, 2020

--

This is Part III of the project after we trained and deployed the model on AWS endpoint. Now we need to let the model in business. The way your client application talk with the endpoint is through an AWS Lambda function and API Gateway.

In the below article, we will create a Lambda Function, an API Gateway, and finally a client application to call the model and return us the result, Fraud or Not Fraud.

Create a Lambda Function to call the Endpoint

Let’s look at how we call endpoint from Lambda. From the AWS Lambda console, create a Lambda function from scratch. You can name it as creditcardprediction

During the creation, make sure you select a IAM role which has lambda “Action”: “sagemaker:InvokeEndpoint”

In the Designer Panel, we will create API Gateway as a trigger later, but for now, we need to add codes in Function Code panel.

We use the boto3 sagemaker-runtime.invoke_endpoint() to call the endpoint.

So the last step for the Lambda Function is to create an Environment Variables that explicitly point the endpoint we just created.

It is time for us to test it out. Let’s create a test case and hit run.

You should see the succeeded result at this point. If you failed for the test cases, take a look at your log or check out the result from Execution Results. Sometimes it is your input data format issue. You also need to check your endpoint is up and running. If you succeeded in the previous test in Jupyter Notebook after you deployed your endpoint but failed here, most likely is your lambda function issue. In the function code, try to understand each line of code and make libraries, Python version, and endpoint environment are all set up correctly.

Create an API Gateway

Below are the steps I created the API Gateway, think about the gateway is actually a trigger from client application that triggers Lambda function.

First of all, create a REST API gateway.

Name your gateway like creditcardprediction and endpoint type as Regional.

Create Resource from Action Drop Down list, giving it a name like creditcardprediction. When the resource is created, from the same drop-down list, choose Create Method to create a POST method.

For Integration type, choose Lambda Function.

For Lambda Function, enter the function you created.

Finally, from Action drop-down list, choose Deploy API. Given the name of Deployment Stage like ‘test’.

After you deployed your API, go to Stage and check out your Invoke URL.

the invoke URL that looks like:

https://{restapi_id}.execute-api.us-west 2.amazonaws.com/test/creditcardprediction

Cool, so we had API Gateway and Lambda Function, let’s have a Postman Canary client application to pass the parameter and return us the prediction in real-time.

Test with Postman

In the postman application, choose POST method and copy-paste in the invoke URL in the address bar. In the body and raw radio button, copy-paste the test data. You can see the result of the test data as “Fraud” or “Non-Fraud” in Response. (By the time I wrote this part I have deleted my endpoint so I can’t invoke my function and show you the result in the screenshot. But you should see the entire the AWS services works.)

So that’s how we can predict credit card transaction fraud or not in real-time. The article focus more on the end-to-end process and deployment. I will have a separate article to discuss the model selection and parameter tuning. Hope this help.

--

--