|
| 1 | +# Custom Destinations in API Gateway |
| 2 | + |
| 3 | +###### Supported Versions: 10.7 and above |
| 4 | + |
| 5 | +# Overview of the tutorial |
| 6 | + |
| 7 | +API Gateway supports different destinations, out of the box, to which one can publish events, performance metrics and audit log data. Event type data provides information about API transactions. The performance data provides information on average response time, total request count, fault count, and so on, for the APIs that it hosts. Audit logs provide a record of system transactions, events, and occurrences in API Gateway. You must have the API Gateway's manage destination configurations functional privilege assigned to configure the following destinations to which the event types, performance metrics, and audit log data is published. |
| 8 | + |
| 9 | +API Gateway currently supports the below destinations: |
| 10 | + |
| 11 | +- API Gateway |
| 12 | +- API Portal |
| 13 | +- Transaction logger |
| 14 | +- CentraSite |
| 15 | +- Database |
| 16 | +- Digital Events |
| 17 | +- Elasticsearch |
| 18 | +- Email |
| 19 | +- SNMP |
| 20 | + |
| 21 | +In addition to the existing destinations, API Gateway now supports Custom Destinations using which the user can publish events, performance metrics and audit log data from API Gateway to destinations which are not supported out of the box. |
| 22 | + |
| 23 | +The tutorial shows how you can use Custom Destinations feature to push the data from API Gateway to Splunk and AWS Lambda function |
| 24 | + |
| 25 | +# Required knowledge |
| 26 | + |
| 27 | +The tutorial assumes that the reader has, |
| 28 | + |
| 29 | +- a basic understanding of API Gateway and its event types |
| 30 | +- a good knowledge on Splunk and AWS Lambda |
| 31 | +- read the custom destinations feature details from the API Gateway User Guide |
| 32 | + |
| 33 | +# Why Custom Destinations? |
| 34 | + |
| 35 | +API Gateway provides options to publish events and logs to preset destinations. But sometimes customer might require the data to be published to a different destination for further data processing and for generating various reports as per their business requirements. The custom destination feature offers solution to this requirement. |
| 36 | +You can configure custom destinations to publish either or all of the following: |
| 37 | + |
| 38 | +- Design time events such as audit logs of API Gateway modules |
| 39 | +- Error events and policy violation events of assets, and Performance metrics data. |
| 40 | +- Traffic monitoring payloads and alerts of an API |
| 41 | + |
| 42 | +# Prerequisite steps |
| 43 | + |
| 44 | +- Install API Gateway version 10.7 or above if the reader uses on-premise installation (Note: Custom Destination is supported in cloud as well). |
| 45 | + |
| 46 | +# Details |
| 47 | + |
| 48 | +API Gateway supports 4 custom destinations as of 10.7. |
| 49 | + |
| 50 | +1. External HTTP/HTTPs endpoint |
| 51 | +2. webMethods IS service |
| 52 | +3. AWS Lambda |
| 53 | +4. Messaging |
| 54 | + |
| 55 | +Custom Destination can be added for the following monitoring types under Traffic Monitoring. |
| 56 | + |
| 57 | +- Log Invocation |
| 58 | +- Monitor Performance |
| 59 | +- Monitor Level Agreement |
| 60 | +- Traffic Optimization |
| 61 | + |
| 62 | +In this section let us discuss in details about how we can configure API Gateway to send data to an External endpoint(Splunk) and AWS Lambda. |
| 63 | + |
| 64 | +## External endpoint |
| 65 | + |
| 66 | +This use case explains how to publish data to a REST endpoint(Splunk) using custom destination. The use case starts when you have data to be published and ends when you have successfully configured a REST endpoint URL as a destination to publish the data. Ensure you have the external endpoint URL to which you want to publish the data. |
| 67 | + |
| 68 | +### Step 1: Configure Splunk to receive events |
| 69 | + |
| 70 | +Follow [this link](https://docs.splunk.com/Documentation/Splunk/8.1.3/Data/UsetheHTTPEventCollector) to setup the HTTP Event Collector in Splunk. Once you're ready with the Token and Endpoint do a curl request to check whether the configuration is working properly using the below curl command. |
| 71 | + |
| 72 | +```c |
| 73 | +Request: curl -k http://<<host>>:<<port>>/services/collector/event -H "Authorization: Splunk <<Token>>" -d '{"event": {"name": "Developer"}}' |
| 74 | +Response: {"text": "Success", "code": 0} |
| 75 | + |
| 76 | +Request: curl -k http://<<host>>:<<port>>/services/collector/raw -H "Authorization: Splunk <<Token>>" -d '{"name": "Developer"}' |
| 77 | +Response: {"text": "Success", "code": 0} |
| 78 | +``` |
| 79 | +
|
| 80 | +Or the same can be validated using postman with the below details. |
| 81 | +
|
| 82 | +```java |
| 83 | +URL: http://<<host>>:<<port>>/services/collector/event |
| 84 | +Authorization: Basic authorization with Username: Splunk and Password: <<Token>> |
| 85 | +Method: POST |
| 86 | +Payload: {"event":"hello world"} |
| 87 | +``` |
| 88 | + |
| 89 | +Default Splunk URL: ***POST http://localhost:8088/services/collector/raw*** |
| 90 | + |
| 91 | +### Step 2: Create Custom Destination |
| 92 | + |
| 93 | +Login to API Gateway, expand the menu options icon available in the top right in the title bar, and select Administration. Click Destinations and select Custom destinations from the left navigation pane. Click + Add custom destination. |
| 94 | + |
| 95 | +#### Name: |
| 96 | + |
| 97 | +Provide a Unique Name for the Custom destination. Supported characters are [a-zA-Z0-9-_ ]. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +### Condition based publishing |
| 102 | + |
| 103 | +You can configure conditions based on which API Gateway filters events to publish to a configured destination. That is, only the events that satisfy your conditions are published to the given destination. For example, you can configure a condition to publish the error events of an application, say app1, to a destination; and another condition to publish the error events of another application, app2, to a second destination and so on. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +To configure a condition, you can use variables available in the variable framework, and specify a matching value based on which the condition must be validated. You can specify multiple conditions and configure whether the data to be published must satisfy all or any of the given conditions. The use cases in this section explain the process of configuring conditions. |
| 108 | + |
| 109 | +#### Conditions: |
| 110 | + |
| 111 | +Conditions are completely optional. One can configure multiple conditions and everything will be validated based on the Condition type. In this case, if the application name matches with **'Splunk HEC Application',** only then API Gateway will send the configured events to Splunk. |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +After configuring the condition variable and value click Add. |
| 116 | + |
| 117 | +#### Type - External endpoint: |
| 118 | + |
| 119 | +Select the Custom destination type as External endpoint and provide the necessary details like URL, Method, SSL, and Timeout. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +#### Request Processing: |
| 124 | + |
| 125 | +Under request processing click Headers and Add Authorization header. |
| 126 | + |
| 127 | +Header Name: Authorization |
| 128 | + |
| 129 | +Header Value: Encode ***"****Splunk <<SplunkToken>>"*** and configure it as Basic <<encoded value>>, then Click Add. |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +#### Events: |
| 134 | + |
| 135 | +Select the data that you want to publish to the configured destination. |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +After all the successful configuration click Add to add Custom destination. |
| 140 | + |
| 141 | +### Step 3: Create the API and add Custom Destination |
| 142 | + |
| 143 | +Login to API Gateway and create a *Petstore* API from the swagger definition URL http://petstore.swagger.io/v2/swagger.json alternatively you can use the existing API which is already available in API Gateway. Edit the API, go to *Policies → Traffic Monitoring →* Add *Log Invocation → Select Splunk HEC under Destination*. |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +After all the configuration click Save and Activate the API. |
| 148 | + |
| 149 | +### Step 4: Custom Destination in action |
| 150 | + |
| 151 | +Invoke the API using a REST client like Postman to generate Transactional Event. Deactivate and Activate the API to generate Audit Log data. |
| 152 | + |
| 153 | +Open Splunk and search for **sourcetype="httpevent",** the splunk search will look like this, |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +From the search response you can see the Audit log for Activate and Deactivate, and Transactional events published to Splunk. |
| 158 | + |
| 159 | +## Type 2: AWS Lambda |
| 160 | + |
| 161 | +AWS Lambda is a compute service used to run code without provisioning or managing server. You can write your application code in languages supported by AWS Lambda, and run within the AWS Lambda standard runtime environment and resources provided by Lambda. As mentioned earlier, customer can have their custom logic running in AWS for further data processing and for generating various reports as per their business requirements. API Gateway provides support to publish events to the Lambda functions through Custom Destination. |
| 162 | + |
| 163 | +### Step 1: Create AWS Lambda Function |
| 164 | + |
| 165 | +Follow [this link](https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html) to create a AWS Lambda Function. For this tutorial I have created a simple logging function in AWS Lambda with Function Name APIGatewayEvents. |
| 166 | + |
| 167 | +**AWS Lambda Function** |
| 168 | + |
| 169 | +```java |
| 170 | +public class APIGatewayEvents implements RequestHandler<Object, String> { |
| 171 | + @Override |
| 172 | + public String handleRequest(Object input, Context context) { |
| 173 | + context.getLogger().log("Input: " + input); |
| 174 | + return "Received data"; |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +### Step 2: AWS Lambda alias |
| 180 | + |
| 181 | +To invoke a Lambda function, we need to create a AWS account configuration in the API Gateway Administration section with the Access key ID, Secret access key and Region. This can be created by navigating to Administration → External accounts → AWS configuration. Configure the AWS account details here and use it as an alias in the Custom Destination. API Gateway supports configuration of multiple AWS accounts. |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | +Click Add to add the AWS account in API Gateway. |
| 186 | + |
| 187 | +### Step 3: Create Custom Destination |
| 188 | + |
| 189 | +Name, Conditions, Request Processing, and Events sections remains same for AWS Lambda also. |
| 190 | + |
| 191 | +#### Type - AWS Lambda: |
| 192 | + |
| 193 | +Select the Custom destination type as AWS and provide the necessary details like Function Name, Invocation Type, AWS Alias, and Client Configuration. |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +##### Function Name |
| 198 | + |
| 199 | +This is the AWS Lambda function name that you want to invoke during the API execution flow. |
| 200 | + |
| 201 | +##### Invocation Type |
| 202 | + |
| 203 | +Two types of invocation are supported - RequestResponse and Event. RequestResponse is synchronous and Event is asynchronous. |
| 204 | + |
| 205 | +##### AWS Alias |
| 206 | + |
| 207 | +AWS configuration for connecting to the AWS account which hosts the Lambda function. |
| 208 | + |
| 209 | +##### Client Configuration |
| 210 | + |
| 211 | +These are the configurations for the AWS Lambda client in API Gateway which are useful when making a connection to the AWS Lambda function. Select the client configuration from drop down or provide custom client configuration as AWS introduces. |
| 212 | + |
| 213 | +For AWS Client Configuration please refer[ https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/section-client-configuration.html](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/section-client-configuration.html) and[ https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/ClientConfiguration.html.](https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/ClientConfiguration.html) |
| 214 | + |
| 215 | +### Step 3: Create the API and add Custom Destination |
| 216 | + |
| 217 | +Login to API Gateway and create a *Petstore* API from the swagger definition URL http://petstore.swagger.io/v2/swagger.json alternatively you can use the existing API which is already available in API Gateway. Edit the API, go to *Policies → Traffic Monitoring →* Add *Log Invocation → Select AWS CD under Destination*. |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +After all the configuration click Save and Activate the API. |
| 222 | + |
| 223 | +### Step 4: Custom Destination in action |
| 224 | + |
| 225 | +Invoke the API using a REST client like Postman to generate Transactional Event. Deactivate and Activate the API to generate Audit Log data. |
| 226 | + |
| 227 | +Open AWS Lambda to check the events. Login to AWS console, then Services → Lambda → Functions → Choose the Lambda Function(APIGatewayEvents). |
| 228 | + |
| 229 | +In the function overview tab Click Monitor → View logs in CloudWatch. |
| 230 | + |
| 231 | + |
| 232 | + |
| 233 | +This will open a new Cloud Watch tab. In the Log streams select any one of the stream. It will list all the log statement along with the data published from API Gateway. |
| 234 | + |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +# References |
| 242 | + |
| 243 | +- https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html |
| 244 | +- https://docs.aws.amazon.com/lambda/latest/dg/welcome.html |
| 245 | +- https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/section-client-configuration.html |
| 246 | +- https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/ClientConfiguration.html |
| 247 | +- https://docs.aws.amazon.com/lambda/latest/dg/welcome.html |
| 248 | +- https://docs.splunk.com/Documentation/Splunk/8.1.3/Data/UsetheHTTPEventCollector |
| 249 | + |
| 250 | +# Learn more |
| 251 | + |
| 252 | +- For details on usage of *Invoke webMethods IS* policy in versions 10.2 and above, refer [Invoke webMethods IS policy in API Gateway 10.2](https://iwiki.eur.ad.sag/display/RNDWMGDM/Invoke+webMethods+IS+policy+in+API+Gateway+10.2) |
| 253 | +- For details on AMQP [AMQP 1.0 support in API Gateway](https://iwiki.eur.ad.sag/display/RNDWMGDM/AMQP+1.0+support+in+API+Gateway) |
0 commit comments