You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+55-1Lines changed: 55 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
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.
4
4
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.
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
+
60
102
### Caveats
61
103
62
104
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
79
121
80
122
- 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.
81
123
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
+
82
134
### ARNs
83
135
84
136
**us-east-1**
@@ -91,6 +143,8 @@ Bash behaves in ways unlike other programming languages. As such, there are some
0 commit comments