diff --git a/generators/agolang/templates/sam/Makefile b/generators/agolang/templates/sam/Makefile index c7d8753..d69621f 100644 --- a/generators/agolang/templates/sam/Makefile +++ b/generators/agolang/templates/sam/Makefile @@ -5,6 +5,9 @@ build: gomodgen ### yeoman hook ### ## Don't touch this comment, the subgenerator needs it' +access: + chmod -R u+x ./scripts/ + clean: rm -rf ./bin ./vendor Gopkg.lock @@ -18,17 +21,17 @@ gomodgen: local-api: clean build sam local start-api -local-invoke: clean build - docker run --rm -v $(shell pwd)/bin:/var/task lambci/lambda:go1.x $(func) '$(shell cat $(event))' +local-invoke: clean build access + ./scripts/localinvoke.sh '$(func)' '$(event)' '$(network)' -sam-invoke: clean build - sam local invoke --event $(event) $(func)Function +sam-invoke: clean build access + ./scripts/samlocal.sh '$(func)' '$(event)' '$(network)' -local-debug: clean build - sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --docker-network $(network) --event $(event) $(func)Function +debug: clean build access + ./scripts/samdebug.sh '$(func)' '$(event)' '$(network)' -sam-debug: clean build - sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --docker-network $(network) --event $(event) $(func)Function +sam-debug: clean build access + ./scripts/samdebug.sh '$(func)' '$(event)' '$(network)' undeploy: aws cloudformation delete-stack --stack-name <%=projectName%> diff --git a/generators/agolang/templates/sam/README.md b/generators/agolang/templates/sam/README.md index 753edc2..a9b642c 100644 --- a/generators/agolang/templates/sam/README.md +++ b/generators/agolang/templates/sam/README.md @@ -3,34 +3,39 @@ ## CLI -### Running locally `make ... local-invoke|sam-invoke` +### Running locally Suppose we have added `rest` handler: ```bash - #Start local API in port 3000 - $ make local-api #Invoke function (Notice Rest starts with uppercase letter) - $ make func=Rest event=/rest/event.json local-invoke + $ make local-invoke func=rest event=/rest/event.json #Using SAM, Notice Function added after Rest here $ sam local invoke --event /rest/event.json RestFunction ``` -- `func` specified in capital case `Create` -- `event` path to the event file +There's also a way of starting the API and exposing it as an HTTP endpoint to be tested by POSTMAN + +```bash + #Start local API on port 3000 + $ make local-api +``` +- `func` specified in capital case `Create` +- `event` path to the event file, default is the event.json under the route/func specifed in `func` +- `network` docker network inside which sam will run or execute your container (lambda) ### Debug locally `make ... local-debug|sam-debug` Debugging is easier than you might think. Run the following command, it will create a debugging session on port `8997`, put the break points, attach your favourite IDE to the same port and boom! ```bash - $ make network=services_default func=Create event=create/event.json sam-debug + $ make debug network=services_default func=create event=create/event.json ``` - `func` specified in capital case `Create` - `event` path to the event file -- `network` is a docker network you need inside which you need to run your lambda as container +- `network` is a docker network inside which you need to run your lambda as a container ### Deploy project remotely diff --git a/generators/agolang/templates/serverless/Makefile b/generators/agolang/templates/serverless/Makefile index 801f657..544425a 100644 --- a/generators/agolang/templates/serverless/Makefile +++ b/generators/agolang/templates/serverless/Makefile @@ -8,6 +8,9 @@ build: gomodgen mkdir -p .serverless zip .serverless/<%=projectName%>.zip bin/* +access: + chmod -R u+x ./scripts/ + clean: rm -rf ./bin ./vendor Gopkg.lock @@ -24,14 +27,17 @@ sam: build local-api: sam sam local start-api +local-invoke: clean build access + ./scripts/localinvoke.sh '$(func)' '$(event)' '$(network)' + sam-invoke: sam - sam local invoke --event $(event) $(func) + ./scripts/samlocal.sh '$(func)' '$(event)' '$(network)' 'export' -sam-debug: sam - sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --docker-network $(network) --event $(event) $(func) +debug: sam access + ./scripts/samdebug.sh '$(func)' '$(event)' '$(network)' 'export' -local-invoke: build - docker run --rm -v $(shell pwd)/bin:/var/task lambci/lambda:go1.x $(func) '$(shell cat $(event))' +sam-debug: sam access + ./scripts/samdebug.sh '$(func)' '$(event)' '$(network)' 'export' undeploy: sls undeploy --verbosed diff --git a/generators/agolang/templates/serverless/README.md b/generators/agolang/templates/serverless/README.md index 60b2c46..0a11e53 100644 --- a/generators/agolang/templates/serverless/README.md +++ b/generators/agolang/templates/serverless/README.md @@ -3,28 +3,36 @@ ## CLI -### Running locally `make ... local-invoke|sam-invoke` +### Running locally -Not supported by Serverless framework itself, however we wired this up with `lambci/lambda`: +This is not supported by Serverless framework itself, however we wired this up with `lambci/lambda`: ```bash - #Start local API in port 3000 - $ make local-api #OR invoke function (using lambci/lambda) - $ make func=funcName event=/path/to/event.json local-invoke + $ make local-invoke func=funcName event=/path/to/event.json #OR invoke function using SAM [will export to SAM, build and invoke func] - $ make func=funcName event=/path/to/event.json sam-invoke + $ make sam-invoke func=funcName event=/path/to/event.json ``` -- `func` specified in capital case `Rest` -- `event` path to the event file +- `func` specified in capital case `Rest` +- `event` path to the event file +- `network` is a docker network you need inside which you need to run your lambda as container + +There's also a way of starting the API and exposing it as an HTTP endpoint to be tested by POSTMAN + +```bash + #Start local API on port 3000 + $ make local-api +``` -### Debug locally `make ... sam-debug` +### Debug locally `make debug|sam-debug` Debugging is easier than you might think. Run the following command, it will create a debugging session on port `8997`, put the break points, attach your favourite IDE to the same port and boom! ```bash - $ make network=services_default func=Create event=create/event.json sam-debug + $ make sam-debug network=services_default func=create event=create/event.json + #OR + $ make debug network=services_default func=create event=create/event.json ``` - `func` specified in capital case `Create` diff --git a/generators/app/templates/scripts/localinvoke.sh b/generators/app/templates/scripts/localinvoke.sh new file mode 100755 index 0000000..a79188f --- /dev/null +++ b/generators/app/templates/scripts/localinvoke.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +event="$1/event.json" +network="" +if [[ -z "$2" ]]; then + echo "No events passed, applying default from $event" +else + echo "Applying passed events" + event=$2 +fi + +if [[ -z "$3" ]]; then + echo "No docker network specified, applying default docker0" + echo "docker run --rm -v $PWD/bin:/var/task lambci/lambda:go1.x $1 '$(cat ${event})'" + docker run --rm -v $PWD/bin:/var/task lambci/lambda:go1.x $1 '$(cat $event)' +else + network="$3" + echo "Docker Network Specified: ${network}" + echo "docker run --rm -v $PWD/bin:/var/task --network ${network} lambci/lambda:go1.x $1 '$(cat ${event})'" + docker run --rm -v $PWD/bin:/var/task --network ${network} lambci/lambda:go1.x $1 '$(cat $event)' +fi \ No newline at end of file diff --git a/generators/app/templates/scripts/samdebug.sh b/generators/app/templates/scripts/samdebug.sh new file mode 100755 index 0000000..c4ad1f0 --- /dev/null +++ b/generators/app/templates/scripts/samdebug.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +func="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" +network="" +event="$1/event.json" + +if [[ -z "$2" ]]; then + echo "No events passed, applying default from ${event}" +else + echo "Applying passed events" + event=$2 +fi + +#handle exported sam naming diffs +if [[ -z "$4" ]]; then + func="${func}Function" +fi + +if [[ -z "$3" ]]; then + echo "No docker network specified, applying default docker0" + echo "sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --event ${event} ${func}" + sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --event ${event} ${func} +else + echo "Docker Network" + network="$3" + echo "sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --docker-network ${network} --event ${event} ${func}" + sam local invoke -d 8997 --debugger-path ./scripts/linux --debug --region us-east-1 --docker-network ${network} --event ${event} ${func} +fi \ No newline at end of file diff --git a/generators/app/templates/scripts/samlocal.sh b/generators/app/templates/scripts/samlocal.sh index a7c5d31..53ff7ee 100755 --- a/generators/app/templates/scripts/samlocal.sh +++ b/generators/app/templates/scripts/samlocal.sh @@ -1,21 +1,29 @@ #!/usr/bin/env bash -dir=$1 -func=$2 -debug=$3 +func="$(tr '[:lower:]' '[:upper:]' <<< ${1:0:1})${1:1}" +network="" +event="$1/event.json" -cd ${dir} -#build the function -make -cd .. -echo "Running SAM inside directory $(pwd)" -sls sam export --output template.yaml -func="$(tr '[:lower:]' '[:upper:]' <<< ${func:0:1})${func:1}" - -if [ ${debug} ]; then -echo "DEBUG MODE! \n sam local invoke ${func} -d 8997 -e ${func}/event.json --debugger-path ./scripts/linux/" -sam local invoke ${func} -d 8997 -e ${func}/event.json --debugger-path ./scripts/linux/ +if [[ -z "$2" ]]; then + echo "No events passed, applying default from ${event}" else -echo "sam local invoke ${func} --event ${func}/event.json" -sam local invoke ${func} --event ${func}/event.json + echo "Applying passed events" + event=$2 +fi + +#handle exported sam naming diffs +if [[ -z "$4" ]]; then + func="${func}Function" fi + + +if [[ -z "$3" ]]; then + echo "No docker network specified, applying default docker0" + echo "sam local invoke --debug --region us-east-1 --event ${event} ${func}" + sam local invoke --debug --region us-east-1 --event ${event} ${func} +else + echo "Docker Network" + network="$3" + echo "sam local invoke --debug --region us-east-1 --docker-network ${network} --event ${event} ${func}" + sam local invoke --debug --region us-east-1 --docker-network ${network} --event ${event} ${func} +fi \ No newline at end of file