Technical Articles
How to consume an AWS lambda Function from SAPPO
We might have more and more requirements integrating with AWS. One of the typical requests is to push message into AWS Simple Notification Service (SNS). One solution option would be using JAVA program in the mapping step or adapter module step. There will a huge amount of java libraries to be downloaded from AWS for doing it. Or if your project has enough budget, make procurement on third party adapters, like Advantco AWS solution.
Another solution option would be using AWS lambda Function and expose it has an API. Once the Function receives incoming message, it pushes the data into the relevant Topic.
I assume many SAP PO developers, who are less familiar with AWS development, are interested in how the entire solution has been implemented. Understand part of the processes are not within SAP PO. I was trying to add these steps into a separate file and uploaded in form of a PDF/Word attachment. However the Blog Post doesn’t support such kind of attachment and the only way is to put these steps inside the tutorial below. Hopefully it does not break the rule here.
Here is the tutorial describing everything built from scratch.
1 Prerequisite.
- PO system
- AWS account with enough authority
2 Solution Overview
2.1Process Steps
- Postman pushes the message out to SAP PO
- SAP PO sends the message to an AWS API Gateway
- The API Gateway triggered a lambda Function and the function populates data into a SNS topic
4. The Topic publishes the message to a Mobile registered via SMS
3 Development Steps
3.1 Create a SNS Topic
Go to AWS Management Console and type SNS in the filter below. Press enter button
Choose Topic option on the left
Click on the Create topic button
Fill in the topic name, description. For the access policy, to keep the process simple, just let every can publish and subscribe the topic as below
Click the button create topic to create the topic
This is the topic created. Then click button Create Subscription
In order to subscript the topic from mobile phone, input the SMS as protocol and your phone number as the endpoint as below
The subscription will be confirmed automatically.
Publish a Test message
This is the message received by the mobile
3.2 Create a Lambda Function
From the AWS Management Console, input lambda in the filter field and press enter button
Click create function button on the right hand
Note:
AWS lambda function supports quite a few different languages
In our tutorial, we will use Node.js 10.x
3.3 Create API Gateway
This is the Diagram after the Add button has been clicked
3.4 Develop Function
Copy and post the source into index.js
const AWS = require('aws-sdk');
AWS.config.loadFromPath('./config.json');
const sns = new AWS.SNS({apiVersion: '2010-03-31'});
exports.handler = async (event) => {
// TODO implement
const SNSTopicArn = event.queryStringParameters ? event.queryStringParameters.SNSTopic : null;
console.log("SNSTopicArn\n" + SNSTopicArn);
return await insertSNS(SNSTopicArn, event.body);
};
const insertSNS = async (Topic, payload) =>{
// Create publish parameters
let params = {
Message: payload, /* required */
TopicArn: Topic
};
// Create promise and SNS service object
var Promise = sns.publish(params).promise();
await Promise.then();
return sendRes(202,"SNS Success");
};
const sendRes = (status, body) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "text/html"
},
body: body
};
return response;
};
this is the view after code has been added
Create another file, named config.json in the same folder as below
Input the key id and key of your service account.
3.5 Change the default timeout
In the Basic settings tile, the default timeout of lambda function is 3 seconds. This is a bit too short. For our test, change it to 1 minute as below.
3.6 Get API Endpoint
Choose API Gateway linking to the lambda function created. The URL of endpoint will be displayed below. It will be used in the SAP PO REST adapter configuration.
4 SAP PO Configuration
Since we are not taking care of message structure conversion, the ESR part can be ignored.
4.1 Sender REST channel
Configure these two tabs only to make the configuration as simple as possible
For the rest of tabs, just remain default configuration.
4.2 Receiver REST channel
Input the URL from AWS API Gateway
The topic needs to be provided in form of parameter in the URL as below
https://<endpoint>?SNSTopic=<SNS ARN>
4.3 Integrated Configuration
The namespace and interfaces are dummy ones.
4.4 Activate the change list
5 Unit Test
5.1 POSTMAN
Push message from POSTMAN as below