Skip to content

mitre/saf-lambda-function

 
 

Repository files navigation

saf-lambda-function

This code uses the Serverless Framework to deploy an AWS lambda function that, when triggered at a certain rate, will run the SAF CLI with the given input command (COMMAND_STRING) and can optionally upload results to an S3 bucket. This example is specifically relevant to running the command convert ionchannel2hdf.

Getting Started

(This is installed and kept up to date using npm, which is included with most versions of NodeJS.)

  1. Clone this repository: git clone https://github.com/mitre/saf-lambda-function.git -b ionChannel
  2. cd saf-lambda-function
  3. Install the latest dependencies: npm install
  4. Install the Serverless Framework: npm install -g serverless
  5. Configure your AWS credentials. Recommended method is to add a profile in the ~/.aws/credentials file and then export that profile:
export AWS_PROFILE=<your_creds_profile_name> # To ensure your access to AWS, run: aws s3 ls

Setting Up the Lambda Function

This lambda function uses environment variables to orchestrate its function. The required environment variables are OUTPUT_BUCKET and COMMAND_STRING. The bucket environment variable defines the source bucket for your input to the SAF CLI command, and the command string defines the SAF CLI function and its flags excluding the -i input and -o output flags which are handled by your input and output bucket and object configurations.

Additional Input and Output Configuration

Additional optional variables can be set to further configure the function. The table below shows each variable and the default behavior. The OUTPUT_BUCKET can be set as the location to upload results of the SAF CLI command. The OUTPUT_ENABLED variable can be set to false if the function should not upload results to an S3 bucket. The OUTPUT_PREFIX specifies a path within the OUTPUT_BUCKET to place the results of the SAF CLI call. The SERVICE_NAME will be the name of this lambda service when deployed.

ENVIRONMENT NAME Required Default Examples
COMMAND_STRING x none "convert ionchannel2hdf -a api-key -t your-team-name", See more here
OUTPUT_BUCKET x none "bucket-name"
OUTPUT_ENABLED true false
OUTPUT_PREFIX "results/" "output/", "results/hdf/", ""
OUTPUT_TIMEOUT 900 lambda timeout value in seconds
SERVICE_NAME "saf-lambda-function" "different-service-name"
IAM_ROLE_PATH none </role/path/> e.g., "/delegatedadmin/developer/"
IAM_ROLE_BOUNDARY none arn:aws:iam::${aws:accountId}:policy/permissions-boundary-policy, e.g., "arn:aws:iam::1234567890:policy/cms-cloud-admin/developer-boundary-policy"
SCHED_INTERVAL_MINUTES 1440 scheduled event trigger interval in minutes
  1. Set the required variables: OUTPUT_BUCKET and COMMAND_STRING.
  • Example:
export OUTPUT_BUCKET="bucket-name" export COMMAND_STRING="convert ionchannel2hdf -a api-key -t your-team-name"
  • NOTE: This version of the lambda function does not handle commands with input flags (i.e. "-i input-file.json").
  • NOTE: Do not include the output flag in the command string. Instead, set the output configuration variables.
  • NOTE: This action does not support view heimdall.
  • More examples can be found at SAF CLI Usage
  • You can ensure that the environment variables are set properly: env.
  1. Set any optional variables that you may want to change. If the default value for any of these variables suffices, it does not need to be set.

Test and Deploy your SAF CLI Lambda function

Test by invoking locally

  1. Run serverless invoke local --function saf to invoke the function locally. This will interact with your real OUTPUT_BUCKET. You can check the AWS Console for the results files.

Deploy the service

  1. serverless deploy --verbose. This may take several minutes.

Test by invoking via AWS

  1. When the service is deployed successfully, log into the AWS console, go to the "Lamda" interface, and check the logs under the "monitor" tab to see if the function ran at the desired time. Screenshot 2022-04-20 at 09-30-41 Functions - Lambda

  2. Check the output in your OUTPUT_BUCKET.Screenshot 2022-04-20 at 09-32-39 sls-attempt-three-emcrod - S3 bucket

Contributing

Please feel free to look through our issues, make a fork and submit PRs and improvements. We love hearing from our end-users and the community and will be happy to engage with you on suggestions, updates, fixes or new capabilities.

Issues and Support

Please feel free to contact us by opening an issue on the issue board, or, at saf@mitre.org should you have any suggestions, questions or issues.


NOTICE

© 2022 The MITRE Corporation.

Approved for Public Release; Distribution Unlimited. Case Number 18-3678.

NOTICE

MITRE hereby grants express written permission to use, reproduce, distribute, modify, and otherwise leverage this software to the extent permitted by the licensed terms provided in the LICENSE.md file included with this project.

NOTICE

This software was produced for the U. S. Government under Contract Number HHSM-500-2012-00008I, and is subject to Federal Acquisition Regulation Clause 52.227-14, Rights in Data-General.

No other use other than that granted to the U. S. Government, or to those acting on behalf of the U. S. Government under that Clause is authorized without the express written permission of The MITRE Corporation.

For further information, please contact The MITRE Corporation, Contracts Management Office, 7515 Colshire Drive, McLean, VA 22102-7539, (703) 983-6000.

Releases

No releases published

Packages

No packages published

Contributors 7