Introduction to testing with sam local start-lambda - Amazon Serverless Application Model
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Introduction to testing with sam local start-lambda

Use the Amazon SAM CLI subcommand sam local start-lambda to invoke your Lambda function through the Amazon CLI and SDKs. This command starts a local endpoint that emulates Lambda.

To use sam local start-lambda, install the Amazon SAM CLI by completing the following:

Before using sam local start-lambda, we recommend a basic understanding of the following:

Using sam local start-lambda

When you run sam local start-lambda, the Amazon SAM CLI assumes that your current working directory is your project’s root directory. The Amazon SAM CLI will first look for a template.[yaml|yml] file within a .aws-sam subfolder. If not found, the Amazon SAM CLI will look for a template.[yaml|yml] file within your current working directory.

To use sam local start-lambda
  1. From the root directory of your project, run the following:

    $ sam local start-lambda <options>
  2. The Amazon SAM CLI builds your Lambda functions in a local Docker container. It then outputs the local address to your HTTP server endpoint. The following is an example:

    $ sam local start-lambda Initializing the lambda functions containers. Local image is up-to-date Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64. Mounting /Users/.../sam-app/hello_world as /var/task:ro,delegated, inside runtime container Containers Initialization is done. Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint. 2023-04-13 07:25:43 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:3001 2023-04-13 07:25:43 Press CTRL+C to quit
  3. Use the Amazon CLI or SDKs to invoke your Lambda function locally.

    The following is an example using the Amazon CLI:

    $ aws lambda invoke --function-name "HelloWorldFunction" --endpoint-url "http://127.0.0.1:3001" --no-verify-ssl out.txt StatusCode: 200 (END)

    The following is an example using the Amazon SDK for Python:

    import boto3 from botocore.config import Config from botocore import UNSIGNED lambda_client = boto3.client('lambda', endpoint_url="http://127.0.0.1:3001", use_ssl=False, verify=False, config=Config(signature_version=UNSIGNED, read_timeout=1, retries={'max_attempts': 0} ) ) lambda_client.invoke(FunctionName="HelloWorldFunction")

Options

Specify a template

To specify a template for the Amazon SAM CLI to reference, use the --template option. The Amazon SAM CLI will load just that Amazon SAM template and the resources it points to. The following is an example:

$ sam local start-lambda --template myTemplate.yaml

For more information on Amazon SAM templates, see Amazon SAM template anatomy.

Best practices

If your application has a .aws-sam directory from running sam build, be sure to run sam build every time you update your function code. Then, run sam local start-lambda to locally test your updated function code.

Local testing is a great solution for quick development and testing before deploying to the cloud. However, local testing doesn’t validate everything, such as permissions between your resources in the cloud. As much as possible, test your applications in the cloud. We recommend using sam sync to speed up your cloud testing workflows.

Learn more

For a list of all sam local start-lambda options, see sam local start-lambda.