A developer has an AWS SAM application with a function that runs whenever an object is uploaded to an Amazon S3 bucket.
The function is one step in the team's order pipeline, and its handler reads the bucket and key from each event. Before deploying anything, the developer wants to run the function on a laptop with a realistic S3 upload event.
Which approach should the developer use?
Choose one answer.
AA. Run sam deploy to a test stack, then upload an object to the bucket and check the function's CloudWatch logs.
BB. sam local start-api runs a local HTTP endpoint for API Gateway-style events, so the developer can send requests to it.
CC. Run sam local generate-event s3 put to create a sample event, then sam local invoke with that event file.
DD. Invoke the function from the Lambda console's test feature, pointing it at a local copy of the code and a sample event.
Show the answer and explanation
Answer: C
A. Wrong: A test stack would work, but it deploys to AWS and uses cloud resources, which the developer wants to avoid before deploying. It does not test the function on the laptop.
B. Wrong: sam local start-api runs an HTTP endpoint for API Gateway-style events. An S3 notification is not an HTTP request, so the function would not receive a realistic S3 event.
D. Wrong: The Lambda console runs functions in AWS, not on a laptop, and it cannot run local code. It does not meet the need to test locally before deploying.
Explanation
The SAM CLI runs a function locally in a container. sam local generate-event creates a realistic sample event for services such as S3, and passing that event to sam local invoke tests the handler with a realistic payload. Local runs do not check IAM permissions, so the function should also be tested in AWS.
Want more on Testing and test automation? The 5 full-length practice sets have 12 more Testing and test automation questions, 390 in all, none of them in the free exam. One payment of $14.99, no subscription. See the practice sets →