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:

import boto3

endpoint_url = "http://localhost.localstack.cloud:4566"
# alternatively, to use HTTPS endpoint on port 443:
# endpoint_url = "https://localhost.localstack.cloud"

def main():
    client = boto3.client("lambda", endpoint_url=endpoint_url)
    result = client.list_functions()
    print(result)

if __name__ == "__main__":
    main()
client = boto3.client("lambda")
...

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

import os
client = boto3.client("lambda", endpoint_url=os.getenv("AWS_ENDPOINT_URL"))
...

Further Material

Last modified July 18, 2024: setup markdownlint (#1382) (f2ebb421e)