harshinielath
Tera Expert

Hi ServiceNow community! Have you tried or would like to integrate ServiceNow with AWS Lambda? If so, this blog post is for you. This post explains the steps required to setup a REST Message on ServiceNow and also how to setup a simple Lambda function and API gateway on AWS.

 

AWS Lambda is an event-driven platform provided as a part of Amazon Web Services. It is basically a serverless computing service that executes code in response to events and can also manage the computing resources. So, this can be used to build small, on-demand applications or simple micro-services which can be respond to events. 

One use case I have personally used this is to call a small machine learning microservice with the new data adding to incident table. I created a ML model to predict Incident category through short description and used AWS Lambda to call the ML Prediction function that provides real-time predictions for Incident category when new incident with short description is added.

There are many other use cases of AWS Lambda such as loading results into DynamoDB or performing ETL on RedShift by creating triggers in SNow.

Steps to setup the integration: 

  1. The first step is to create a Lambda function in AWS.

Navigate to Lamba>Create Function

find_real_file.png

  1. In Function Code, you have options to code entry type as:
  • Edit Code inline
  • Upload a zip fie
  • Upload a file from S3

I have selected Edit code inline for now for simple demonstration

Then select the Runtime you want to run in and the handler information.

Handler should be of the definition lambda_function_file_name.method_name

So, for example, here, my python file name is lambda_function and method is lambda_handler.

find_real_file.png

  1. Then, type the code and analysis you wish to perform. In the example, I am retrieving the input from API call and returning the output after concatenating the string ‘something’ to it.

def lambda_handler(event, context):

    short_desc=event['params']['querystring']['token'];      //accessing the parameters from api call

    return 'something'+short_desc

4. Create an execution role with accessing permissions to Lambda and save the function.find_real_file.png

5.Now, select the API Gateway as the trigger and by default, ANY method is available in the Method Execution.find_real_file.png6. Add the GET method or any other method you wish to call the Lambda function through your API calls.find_real_file.png

 

7. Now for accessing the parameters from the API query/call, you need to set up the Integration Request for GET method as shown below:find_real_file.png

Note: The Body Mapping Template has to be defined and saved which passes all the parameters to the integration endpoint and can be accessed in the Lambda functions using ‘event’ variable.

 

8. Now, it is ready to invoke the url and trigger the respective Lambda function. The invoke url is found at Stages>GET method.

9. Lastly, click the API and Select Actions>Deploy API.

ServiceNow REST Message creation

1. Navigate to System Web Services>REST Message>New.find_real_file.png2.Fill in the form with a name and endpoint(url from aws lambda). If you want to send parameters, insert them in the endpoint like

https://eqtknizmg4.execute-api.us-east-1.amazonaws.com/start/myLambdaFunction?token=${token}

3. The GET method is added by default in the HTTP method and add the variables (in this case ‘token’) in the Variable Substitutions column and give a test value.find_real_file.png4.  Finally, test the Rest Message by clicking the Test Related Link and the output is in the Response field.find_real_file.png 

 

Comments
ARG645
Tera Guru

Very Helpful! By the way, what type of authentication we can use for this Integration, does it support Basic Authentication/ OAuth ?

harshinielath
Tera Expert

Thank you! I have tried Basic Authentication but the documentation says OAuth 2.0 is supported too.

sureshthota
Tera Explorer

This is outdated. Could you please help me with the lambda function creating records in servicenow tables. ex: incident, change, etc.

 

Version history
Last update:
‎07-12-2018 02:50 PM
Updated by: