/measure-transfer-lambda

This is a lambda service to transfer MAT measures to MADiE

Primary LanguageTypeScriptCreative Commons Zero v1.0 UniversalCC0-1.0

measure-transfer-lambda

Continuous Integration codecov

MeasureTransferLambda is an AWS lambda function that is being used to transfer a MAT measure to the MADiE. Lambda function reads the measure json file uploaded to S3 bucket(see ./template.yml AWS SAM template for whole stack) by MAT application and transfers it to MADiE application.

This project contains source code and supporting files that you can deploy with the SAM CLI. It includes the following files and folders.

  • lambda-function - Code for the application's Lambda function..zshrc
  • lambda-function/tests - Unit tests for the application code.
  • template.yaml - A template that defines the application's AWS resources.

Build and test locally

We need LocalStack up and running locally to deploy lambda function and test. Steps to install and run LocalStack can be found here. Easiest way to fire up LocalStack is by using docker-compose

Build your application with the sam build command.

measure-transfer-lambda$ sam build

If you receive an error related to cmake while running sam build you may need to manually install cmake in order to proceed. This can be done with homebrew brew install cmake.

The SAM CLI installs dependencies defined in lambda-function/package.json, compiles TypeScript with esbuild, creates a deployment package, and saves it in the .aws-sam/build folder.

You can use LocalStack to deploy this lambda function locally and test it by uploading the test measure provided in lambda-function/tests/fixtures/measure.json.

Command to deploy lambda function(make sure you have samlocal CLI installed in your local machine.):

measure-transfer-lambda$ samlocal deploy --guided

Deployment process is interactive. It will ask you bunch of questions and deploys the lambda along with all necessary permissions and S3 bucket.

Now trigger the lambda function by uploading the lambda-function/tests/fixtures/measure.json to S3 bucket using AWS put-object cli. Command to upload the test measure.json:

measure-transfer-lambda$ aws s3api put-object --bucket mat-madie-measures  --key measure.json --body ./lambda-function/tests/fixtures/measure.json  --endpoint-url http://localhost:4566 --region us-east-1

Here, --endpoint-url is the URL where LocalStack is running. Use AWS CloudWatch CLIs to inspect the logs generated by Lambda function.

Unit tests

measure-transfer-lambda$ cd lambda-function
hello-world$ npm install
hello-world$ npm run test

Cleanup

To delete the application that you created in LocalStack, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name measure-transfer-lambda

OR

Just kill the LocalStack process and start again. All the resources that were created as part of deployment will no longer be visible.