Skip to content

Create and deploy an API

Marco Bonvini edited this page Mar 27, 2017 · 1 revision

This page describes how to convert a simulation model exported as a Functional Mockup Unit (FMU) to an AWS Lambda function and expose it through a REST API via Apigateway.

The steps to follow are three

  1. Create and setup a folder for your APP
  2. Deploy the Lambda function
  3. Expose the Lambda function via a REST API

Create and setup a folder for your APP

First get the FMU of your model and put it inside a folder located in ./apps/<YOUR_APP>. If you don't have an FMU you can use one of the projects already in the ./apps folder, for example ./apps/hello_world.

Second, you need to add a JSON file called config.json in the application folder ./apps/<YOUR_APP>. The result should be ./apps/<YOUR_APP>/config.json.

You can use ./apps/config_template.json as template for the configuration file. You can also have a look at ./apps/hello_world/config.json to have an idea of how this file should look like.

If you add a readme.md file in the APP folder this file will be deployed as part of the Lambda function and it will be available through the REST API once created.

NOTE: Both the readme.md and config.json files are packaged with the application when the the Lambda function is Deployed and they are available at runtime.

Deploy the Lambda function

To create and deploy the AWS Lamnda function that runs simulation of your FMU model run the following command

make create_function APP_DIR=./apps/<YOUR_APP>

where APP_DIR is the path of the application folder (the one that contains the *.fmu and the config.json file) without the trailing slash, i.e. ./apps/hello_world and not ./apps/hello_world/.

This command performs the following actions

  1. creates an S3 bucket that will store your FMUs,
  2. copies the FMU from your APP_DIR folder to S3,
  3. creates IAM roles that allows the Lambda function to read data from S3 and write log files in Cloudwatch,
  4. creates the lambda function.

All the above steps are done in accordance with the settings specified in the file ./apps/<YOUR_APP>/config.json.

At this point the Lambda function exists and you can test it using the AWS console. Please not that after this step the Lambda function is not yet open to the public.

Expose the Lambda function via a REST API

To create a REST API endpoint that exposes the Lambda function execute the following command

make expose_function APP_DIR=./apps/<YOUR_APP>

This command performs the following actions

  1. creates the REST API endpoint and its resources using Apigateway,
  2. configures the Lambda function such that it can be executed from Apigateway,
  3. deploys the API and makes it available to the public.

Once the API has been deployed the command

make get_url APP_DIR=./apps/<YOUR_APP>

returns the public URL of the REST API endpoint. The URL has the following structure

https://<api_id>.execute-api.<region>.amazonaws.com/prod/<function_name>

Test the API

Once the API is public you can test it by making HTTP requests to it, for example

curl -X GET https://<api_id>.execute-api.<region>.amazonaws.com/prod/<function_name>

returns the XML model description file that is part of the FMU. Look at LambdaSim API for more information on the methods available in APIs created by LambdaSim.