Documentation

Build in-house API as AWS Lambda

Article author
Admin
  • Updated

This example hosts ediFabric Native behind an AWS Lambda and API Gateway HTTP API. It translates X12 EDI to JSON and back, validates transaction sets, and generates acknowledgments using the C# bindings from edifabric-csharp-bindings.

The native library is a self-contained shared library. No EdiFabric.Api NuGet package (aka EdiNation InHouse) is required on the target machine beyond this Lambda host.

ediFabric Native API for AWS Lambda on GitHub

 

Prerequisites

Platform File
Windows (local) edifabric-x12-tools.dll
Linux (AWS Lambda) edifabric-x12-tools.so
macOS edifabric-x12-tools.dylib

Download ediFabric Native Library

Put the library in the repository root or in EdiFabric.Api.AWS, or set Configuration.LibraryPath / EDIFABRIC_X12_LIB. The project copies it next to the Lambda package on build when it is found in those folders. Deploy the Linux .so with the function.

  • X12 test file(s). If you don't have a test file, use one of ours - X12 HIPAAX12.

 

License

Set Configuration.ApiKey to your serial. The free-plan serial is:

bd96a836feca45cb91c86ee65d281f52
 

The free plan authorizes with set_serial only. Tokens (S3Cache) are available for the Enterprise license.

 

Setup

Rebuild the solution. If there are any build errors, contact us at https://support.edifabric.com/hc/en-us/requests/new for assistance.

The C# bindings live in EdiFabric.Api.AWS/Native (NativeMethods.cs and EdiFabricX12.cs), copied from the edifabric-csharp-bindings repository.

By default the API uses the online spec service (SetMap with "default": "<serial>"). To use local JSON models instead, place a map/map.json next to the Lambda project (see the bindings README for the map format).

 

Create a stack in AWS

The example uses AWS CloudFormation.

Detailed tutorial on how to create an AWS CloudFormation stack in the CloudFormation console. For this tutorial, we created a stack named "edinationtest".

  1. Download EdiNation Inhouse, unzip it, and navigate to folder edifabric-api-aws/EdiFabric.Api.AWS.
  2. Log in to your AWS account, go to CloudFormation, and select to create a new stack.
  3. Choose to upload the template file, and select file serverless.template from the folder you navigated to in the first step.
  4. Then use the default suggestions for the other tabs and finally, Create the stack.

    createlambda1.png

  5. The stack resources creation will fail because we haven't uploaded the code to an S3 bucket. Leave it as it is, we only need the stack for now.

 

Publish the AWS Lambda from Visual Studio

  1. Navigate to folder edifabric-api-aws and open solution EdiFabric.Api.AWS.sln in Visual Studio.
  2. Open file Configuration.cs and change ApiKey to your paid key or leave it to use the free key.

    apikey.png
  3. Rebuild the solution and ensure all package dependencies were installed correctly.
  4. Right-click on the project name and select "Publish to AWS Lambda".

    createlambda3.png

  5. Select the stack you created in the first step, create a new S3 bucket edinationtestbucket to publish to, and finally, click Publish.

    createlambda2.png

  6. Once published, copy the URL:

    createlambda3.png

 

Test the Function

  1. You can also find the URL in the AWS portal. Go to API Gateway and select "edinationtest":

    createlambda4.png

  2. Then go to Dashboard and copy it:

    createlambda5.png

  3. Download and open Postman. Let's test the X12 read operation first.
  4. Open a new tab in Postman, select the POST method and add the following request URL (full API reference):

    {Your API URL}/x12/read
  5. Go to the Body tab, select the "binary" content, and then browse to your text X12/HIPAA file.

    If you don't have a test file, use one of ours - X12 HIPAA, X12.

    createlambda7.png

  6. Finally, hit the Send button and inspect the response body:

    createazurefunction7.png

 

Cache tokens

By default ediFabric Native would request a token from the Authentication API every time. To avoid these calls altogether and work in a fully offline mode, implement a token cache.

Tokens are only available for the Enterprise plan

To implement the token cache, open the file EdiFunctions.cs and follow the instructions in the comments:

tokencache.png

 

Cache models

By default ediFabric Native would request a new JSON model from the Authentication API every time a new transaction needs to be parsed. To avoid these calls altogether and work in a fully offline mode, create a model map.

SetMap tells the engine where to find transaction-set models. Keys are message:version. Set default to your serial to resolve unmapped transaction sets through the online spec service, or leave it null and map everything locally.

{
  "default": null,
  "maps": {
    "837:005010X222A1": { "type": 1, "name": "837P.json", "location": "/opt/models" },
    "850:005010":       { "type": 1, "name": "850.json",  "location": "/opt/models" }
  }
}
 

All X12 transactions, such as 837P, 834, 850, etc. are represented as proprietary JSON. Download a standard model from EdiNation Spec Library, or a custom model from EdiNation Spec Builder. Create/modify models in OpenEDI format, upload them in EdiNation Spec Builder and download them as JSON for use in ediFabric Native.

To download a model in either EdiNation Spec Library or EdiNation Spec Builder, select the model first, then in the JSON view select the Download button in the top right corner.

Model Img

Choose to download as ediFabric Native. Models are JSON files, and must be included, together with the map, in the deployment package.

 

 

Share this:

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.