Skip to content

Commit a3c20ec

Browse files
author
Graham Krizek
committed
Add a writing scripts section to how-to
1 parent f497d8d commit a3c20ec

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Run Bash in [AWS Lambda](https://aws.amazon.com/lambda/) via [Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html). This Layer is 100% Bash and handles all communication with the Lambda API. This allows you to run full Bash scripts and commands inside of AWS Lambda. This Layer also includes common CLI tools used in Bash scripts.
44

5-
See the [How To](#how-to) section to understand how to use these layers. Also see the [example.sh](example.sh) file for an example of how to write a Bash script compatible with AWS Lambda.
5+
See the [How To](#how-to) section to understand how to use these layers. Also see the [example.sh](example.sh) file for an example of how to write a Bash script compatible with this Layer.
66

77
## How To
88

@@ -57,6 +57,48 @@ $ aws lambda update-function-configuration \
5757
--layers $ARN
5858
```
5959

60+
### Writing Scripts
61+
62+
Like any other Lambda function code, your main script's name must match the first part of your handler. Inside your main script, you must define a function that matches the second part of the handler. You must have `set -e` be the first line inside your function. Putting `#!/bin/bash` at the top of your file is not necessary. So if your Lambda handler is `index.handler`, your file and contents should look like:
63+
64+
```
65+
$ cat index.sh
66+
handler () {
67+
set -e
68+
...
69+
}
70+
```
71+
72+
The `event` data is sent to your function as the first parameter. To access it, you should use `$1`. So if you need the `event` data, you should set it to a variable. For example, `EVENT_DATA=$1`.
73+
74+
```
75+
handler () {
76+
set -e
77+
EVENT_DATA=$1
78+
}
79+
```
80+
81+
All the pre-installed tools are already in your `$PATH` so you can use them as expected. Any command output is automatically sent to CloudWatch, just like normal Lambda functions.
82+
83+
```
84+
handler () {
85+
set -e
86+
EVENT_DATA=$1
87+
aws s3 ls $(echo $EVENT_DATA | jq ."bucket")
88+
}
89+
```
90+
91+
If you need to send a response back, you should send the response to `stderr`. (see the [caveats](#CAVEATS) section for an explanation why) To send output to `stderr` you should use `>&2`. This will be picked up and returned from the Lambda function.
92+
93+
```
94+
handler () {
95+
set -e
96+
EVENT_DATA=$1
97+
aws s3 ls $(echo $EVENT_DATA | jq ."bucket")
98+
echo "{\"success\": true}" >&2
99+
}
100+
```
101+
60102
### Caveats
61103

62104
Bash behaves in ways unlike other programming languages. As such, there are some requirements on the user's end that must be done.
@@ -79,6 +121,16 @@ Bash behaves in ways unlike other programming languages. As such, there are some
79121

80122
- The AWS CLI appears to be much slower than most of the AWS SDKs. Take this into consideration when comparing Bash with another language and evaluating execution times.
81123

124+
- With this method there is no `context` in the function, only `event` data. The `event` data is sent to your function as the first parameter. So to access the `event` data, use `$1`, for example `EVENT_DATA=$1`. In order to give some details that were availabe in the `context`, I export a few additional variables.
125+
126+
`AWS_LAMBDA_REQUEST_ID` - AWS Lambda Request ID
127+
128+
`AWS_LAMBDA_DEADLINE_MS` - Time, in epoch, that your function must exit by
129+
130+
`AWS_LAMBDA_FUNCTION_ARN` - Full AWS Lambda function ARN
131+
132+
`AWS_LAMBDA_TRACE_ID` - The sampling decision, trace ID, and parent segment ID of AWS XRay
133+
82134
### ARNs
83135

84136
**us-east-1**
@@ -91,6 +143,8 @@ Bash behaves in ways unlike other programming languages. As such, there are some
91143
- `$ aws`
92144
- `$ git`
93145
- `$ jq`
146+
- `$ scp`
147+
- `$ sftp`
94148
- `$ ssh`
95149
- `$ unzip`
96150
- `$ wget`

0 commit comments

Comments
 (0)