BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News AWS Introduces Remote Invoke Feature in AWS SAM CLI, Enabling Seamless Testing of Lambda Functions

AWS Introduces Remote Invoke Feature in AWS SAM CLI, Enabling Seamless Testing of Lambda Functions

This item in japanese

AWS recently announced the launch of the remote invoke feature in AWS Serverless Application Model (SAM) Command Line Interface (CLI) for AWS Lambda, allowing developers to quickly invoke their AWS Lambda functions deployed to the AWS cloud.

AWS SAM CLI is a developer tool that makes it easier to build, test, package, and deploy serverless applications. With the added remote invoke feature, developers can use the SAM CLI to test a Lambda function with a simple command sam remote invoke, which takes an event payload and Lambda logical id as input to invoke the Lambda function, then outputs the response. It supports multiple invocation modes such as response streaming, asynchronous invoke, dry run, and request response. With this feature, developers can speed up the build-deploy-test iteration loop along with the sam sync command.

According to the documentation, when a developer invokes a Lambda function with sam remote invoke, the AWS SAM CLI returns a response that contains the following:

  • Request metadata – Metadata associated with the request. This includes a request ID and request start time.
  • Lambda response – The response from your Lambda function after being invoked in the cloud.

In addition, a developer can use the --output option to configure the AWS SAM CLI output response in json or text.

sam remote invoke arn:aws:lambda:us-west-2:012345678910:function:sam-app-HelloWorldFunction-ohRFEn2RuAvp

Invoking Lambda Function                                                         

arn:aws:lambda:us-west-2:012345678910:function:sam-app-HelloWorldFunction-ohRFEn2RuAvp                                                                       

START RequestId: 9be48562-f991-4547-9f11-97f2f339b9f4 Version: $LATEST

END RequestId: 9be48562-f991-4547-9f11-97f2f339b9f4

REPORT RequestId: 9be48562-f991-4547-9f11-97f2f339b9f4  Duration: 5.45 ms       Billed Duration: 6 ms     Memory Size: 128 MB     Max Memory Used: 67 MB  Init Duration: 160.95 ms

{"statusCode":200,"body":"{\"message\":\"hello world\"}"}%

Eric Johnson, a Principal Developer Advocate at AWS, explains in an AWS Compute blog post how to test the potential sam remote invoke command:

To test the remote invoke feature, a small AWS SAM application comprises two AWS Lambda functions. The TranslateFunction takes a text string and translates it to the target language using the AI/ML service Amazon Translate. The StreamFunction generates data in a streaming format.

Using either of these approaches, AWS SAM will present the response from the Lambda function as if it was invoked through a service like Amazon API Gateway. However, AWS SAM also provides the option to obtain the raw response directly from the Python software development kit (SDK), boto3. This format includes additional details such as the invoked version, retry attempts, and other relevant information. To retrieve this output, execute the invocation with the extra parameter "--output" and set its value to "json."

Source: https://aws.amazon.com/blogs/compute/testing-aws-lambda-functions-with-aws-sam-remote-invoke/

The AWS SDKs provide various choices for invoking Lambda functions through the Lambda service, with AWS SAM utilizing boto3 for remote invocation. AWS SAM introduces the "--parameter" flag to leverage the SDK options for Lambda function invocation, which can be used multiple times.

Additionally, while the sam logs feature in AWS SAM assists in viewing aggregated logs and traces from Amazon CloudWatch and AWS X-Ray, the new remote invoke function focuses on providing contextual logs specific to individual function invocations, catering to developers' needs during testing.

Lastly, the remote invoke feature is available with SAM CLI version 1.88.0+; more details are available in the documentation.

About the Author

Rate this Article

Adoption
Style

BT