Skip to content

Hot reloading for Python Lambda functions #195

Closed

Description

@tedbrandston

The python with packages example demonstrates using sam local with a bundle.zip. Hot-reloading still "works" in that if the bundle.zip changes it will reload the contents, but there's still a lengthy build time for each change.

If I simply delete the bundle.zip hot reload of the python files appears to work. I haven't tracked down why, but I assume it has to do with the default value from --static-dir pointing at where my functions live.

I'd be satisfied with putting logic into the template like CodeUri: !If [EnvRunningLocal, ., bundle.zip], but #194 documents how that's failing for me. I can imagine other ways to solve this, but I know very little about this project so I'm hesitant to ask for specific implementations. I'd just like to have some way of having hot reload work on python files even when the final target is a zip.

#featurerequest

Activity

  1. heitorlessa commented on Nov 13, 2017

    @heitorlessa
    Contributor

    Hey,

    I haven't used this example yet but I've been primarily using a similar Makefile and making use of "hard links" and "/dev" sub-folder with all dependencies installed in order to allow for hot-reloading, so I don't have to create a task runner for this.

    Cons is that at the moment it's only linking *.py files in the service folder and not taking into account sub-folders/python local packages - Something that can be addressed with some shell script.

    If that helps here's an excerpt of the Makefile until I do have more time to invest in this and test some edge cases:

    Makefile

    SERVICE ?= users
    VENV := ${SERVICE}/.venv
    VENV_LIBS := $(VENV)/lib/python3.6/site-packages
    
    # help credit (https://gist.github.com/prwhite/8168133)
    help: ## Show this help
    
    	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
    
    target: help
    
    	exit 0
    
    
    build-dev: _check_service_definition ## -> creates dev folder and install dependencies (requirements.txt) into dev/ folder
    
    	echo "[+] Installing dependencies into dev/"
    	mkdir -p ${SERVICE}/dev
    	pip install \
    		--isolated \
    		--disable-pip-version-check \
    		-Ur $(SERVICE)/requirements.txt -t ${SERVICE}/dev/
    	echo "[+] Copying source files ${SERVICE} to ${SERVICE}/dev/"	
    	rsync -r --verbose ${SERVICE}/ ${SERVICE}/dev/ \
    		--exclude 'template.yaml' \
    		--exclude 'requirements.txt' \
    		--exclude 'event.json' \
    		--exclude 'dev'
    	ln -f `pwd`/${SERVICE}/*.py ${SERVICE}/dev/
    
    _check_service_definition:
    
    	echo "[*] Checking whether service $(SERVICE) exists..."
    
    # SERVICE="<name_of_service>" must be passed as ARG for target or else fail
    ifndef SERVICE
    	echo "[!] SERVICE argument not defined...FAIL"
    	echo "[*] Try running 'make <target> SERVICE=<service_name>'"
    	exit 1
    endif
    
    ifeq ($(wildcard $(SERVICE)/.),)
    	echo "[!] $(SERVICE) folder doesn't exist"
    	exit 1
    endif
    
    ifeq ($(wildcard $(SERVICE)/requirements.txt),)
    	echo "[!] Pip requirements file missing from $(SERVICE) folder..."
    	exit 1
    endif
    ....

    SAM

        UsersFunction:
          Type: AWS::Serverless::Function
          Properties:
            CodeUri: ./users/dev/
            Handler: users.lambda_handler
            Runtime: python3.6
            Environment:
              Variables:
                TABLE_NAME: "users"
            Events:
              CatchAll:
                Type: Api
                Properties:
                  Path: /{proxy+}
                  Method: any

    Quick run

    Folder struct

    .
    ├── Makefile
    ├── setup.cfg
    ├── template.yaml
    └── users
        ├── dev
    • make build-dev SERVICE="users"
    • where SERVICE="" is equals to the service I created so I don't have to keep hardcoding stuff
  2. tedbrandston commented on Nov 15, 2017

    @tedbrandston
    Author

    hmm, that's interesting. I might try to set that up here, though at this point I'm not feeling too bothered by the 're-bundle then test' workflow. I feel like I've spent more time trying to configure this for 'efficient local testing' than I have actually building my application.

  3. heitorlessa commented on Dec 4, 2017

    @heitorlessa
    Contributor

    @tedbrandston I put that one up here: https://github.com/heitorlessa/sam-local-python-hot-reloading

    I still need to write some proper README but you'll get the idea quickly - If you simply try "make" you'll be given a common usage that should deal with most use cases.

    Once I worked on that Makefile I simply spend no more time in the configuration piece now as that even includes my local packages (e.g. vendor/something).

  4. sanathkr commented on May 31, 2018

    @sanathkr

    #435 started an excellent discussion/PR around this feature

  5. changed the title [-]Hot reloading is less useful when dealing with bundle.zip[/-] [+]Hot reloading for Python Lambda functions[/+] on May 31, 2018
  6. sriram-mv commented on Oct 5, 2020

    @sriram-mv
    Contributor

    @tedbrandston Is this the same case as of today? Are you running into similar issues today. I'm going to close this issue for now, but feel free to open a new issue with latest steps to reproduce this.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions