Search code examples
amazon-web-servicesaws-lambdaaws-sam-cli

How to run a AWS lambda via SAM local that writes to an S3 bucket?


I'm looking at running some of our AWS lambdas locally via SAM, including a one that writes to an S3 bucket. Is there a way of getting S3 to run locally, or talk to an S3 bucket in the cloud and write to that while running the lamda locally?


Solution

  • Yes, the way I achieved this (after much difficulty, the documentation is scattered all over the place and focused on specific use cases that weren't mine) was with localstack.

    You need docker running, then:

    pip install localstack
    

    Then:

    localstack start
    

    Some documentation I've found suggested that you use http://localhost:4566 and indeed pointing a browser there shows something exists, but I got no luck pointing boto3 or another AWS client there, getting very cryptic error messages.

    The real port is 4572, try this out:

    aws --endpoint-url=http://localhost:4572 s3api create-bucket --bucket mybucket --region us-west-1
    

    And now if you have a lambda function like this:

    import boto3
    
    
    def lambda_handler(event, context):
        s3 = boto3.client('s3' ,
            endpoint_url='http://localhost:4572',
            use_ssl=False)
        s3.create_bucket(Bucket="test-bucket")
        #rest of your function here
    

    The final component (might depend on your Docker setup), is you may need to invoke it like this:

    sam local invoke  --docker-network host