Python Boto3

How to use the Boto3 Python AWS SDK with LocalStack.

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of AWS services.

You can easily create a boto3 client that interacts with your LocalStack instance. The example below creates a boto3 client that lists all available Lambda functions:

  1. import boto3
  2. endpoint_url = "http://localhost.localstack.cloud:4566"
  3. # alternatively, to use HTTPS endpoint on port 443:
  4. # endpoint_url = "https://localhost.localstack.cloud"
  5. def main():
  6. client = boto3.client("lambda", endpoint_url=endpoint_url)
  7. result = client.list_functions()
  8. print(result)
  9. if __name__ == "__main__":
  10. main()

Note: If you’re connecting from within a Python Lambda function handler in LocalStack, you can create a default client without configuring the endpoint_url - LocalStack will automatically forward the invocations to the local API endpoints (available in Pro, see here for more details).

  1. client = boto3.client("lambda")
  2. ...

Alternatively, if you prefer to (or need to) set the endpoints directly, you can use the $LOCALSTACK_HOSTNAME environment variable which is available when executing user code (e.g., Lambda functions, ECS containers) in LocalStack:

  1. import os
  2. endpoint_url = f"http://{os.getenv("LOCALSTACK_HOSTNAME")}:{os.getenv("EDGE_PORT")}"
  3. client = boto3.client("lambda", endpoint_url=endpoint_url)
  4. ...

Further Material:

Last modified October 11, 2021: minor: rename folder Integrations->integrations (68c86019)