Safely Develop AWS Lambdas In Localstack That Talk To S3

November - 2021

If you're developing a lambda function in Localstack that needs to talk to S3 (or another AWS service) you need to set a endpoint_url parameter during development. For example:

s3r = boto3.resource('s3', endpoint_url='http://localhost:4572')

But, that's only for development. Once you move to prod, the endpoint_url argument needs to be removed.

I deal with that like this:

import boto3
import json 
import os
import sys

    
def lambda_handler(event, context):

    if 'ENV' not in os.environ:
        print("ERROR: Required `ENV` environmental variable not defined. Process halted.")
        sys.exit()

    elif os.environ['ENV'] == 'dev':
        print("INFO: Starting in localstack dev environment")
        s3r = boto3.resource('s3', endpoint_url='http://localhost:4572')
        
    elif os.environ['ENV'] == 'prod':
        print("INFO: Starting in prod environment")
        s3r = boto3.resource('s3')
        
    else:
        print(f"ERROR: Received invalid `ENV` environmental variable: {os.environ['env']}. Process halted.")
        sys.exit()
        
    # Implement your code here

That code does the following:

  1. Make sure the ENV environmental variable is set. If it's not, the process is halted.
  2. Once the ENV is confirmed to be there it checks to confirm it's either dev or prod
  3. If it's dev or prod it sets s3r with or without the endpoint_url parameters as needed
  4. If the ENV variable isn't dev or prod it halts the process.

Basically, it makes things explicit to help prevent accidentally running against production (which would happen if you don't set the endpoint_url and use valid bucket/key names).

When developing locally, I run this in the terminal environment I'm going to test with:

export ENV=dev

And to setup the production Lambda function in AWS, go under the "Configuration" tab after making the function, choosing "Environment variables" and setting a Key of "ENV" to the Value "prod".

Obviously, this is Python, but the same technique will work with anything that supports environmental variables.

Happy coding.