Skip to content

Commit

Permalink
Make/Bash fixes for SAM and Serverless with documentation
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Soliman <msuliman@mum.edu>
  • Loading branch information
msolimans committed May 18, 2019
1 parent ee6b7e2 commit 5f54b1d
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 47 deletions.
19 changes: 11 additions & 8 deletions generators/agolang/templates/sam/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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%>
Expand Down
21 changes: 13 additions & 8 deletions generators/agolang/templates/sam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 11 additions & 5 deletions generators/agolang/templates/serverless/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
28 changes: 18 additions & 10 deletions generators/agolang/templates/serverless/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
21 changes: 21 additions & 0 deletions generators/app/templates/scripts/localinvoke.sh
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions generators/app/templates/scripts/samdebug.sh
Original file line number Diff line number Diff line change
@@ -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
40 changes: 24 additions & 16 deletions generators/app/templates/scripts/samlocal.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5f54b1d

Please sign in to comment.