Skip to content

Commit 667766b

Browse files
Initial commit of examples.
1 parent 70db6e9 commit 667766b

File tree

50 files changed

+2491
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2491
-5
lines changed

.gitignore

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
2+
# Created by https://www.toptal.com/developers/gitignore/api/linux,windows,macos,visualstudiocode,intellij,go
3+
# Edit at https://www.toptal.com/developers/gitignore?templates=linux,windows,macos,visualstudiocode,intellij,go
4+
5+
### Go ###
6+
# Binaries for programs and plugins
7+
*.exe
8+
*.exe~
9+
*.dll
10+
*.so
11+
*.dylib
12+
13+
# Test binary, built with `go test -c`
14+
*.test
15+
16+
# Output of the go coverage tool, specifically when used with LiteIDE
17+
*.out
18+
19+
# Dependency directories (remove the comment below to include it)
20+
# vendor/
21+
22+
### Go Patch ###
23+
/vendor/
24+
/Godeps/
25+
26+
### Intellij ###
27+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
28+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
29+
30+
# User-specific stuff
31+
.idea/**/workspace.xml
32+
.idea/**/tasks.xml
33+
.idea/**/usage.statistics.xml
34+
.idea/**/dictionaries
35+
.idea/**/shelf
36+
37+
# Generated files
38+
.idea/**/contentModel.xml
39+
40+
# Sensitive or high-churn files
41+
.idea/**/dataSources/
42+
.idea/**/dataSources.ids
43+
.idea/**/dataSources.local.xml
44+
.idea/**/sqlDataSources.xml
45+
.idea/**/dynamic.xml
46+
.idea/**/uiDesigner.xml
47+
.idea/**/dbnavigator.xml
48+
49+
# Gradle
50+
.idea/**/gradle.xml
51+
.idea/**/libraries
52+
53+
# Gradle and Maven with auto-import
54+
# When using Gradle or Maven with auto-import, you should exclude module files,
55+
# since they will be recreated, and may cause churn. Uncomment if using
56+
# auto-import.
57+
# .idea/artifacts
58+
# .idea/compiler.xml
59+
# .idea/jarRepositories.xml
60+
# .idea/modules.xml
61+
# .idea/*.iml
62+
# .idea/modules
63+
# *.iml
64+
# *.ipr
65+
66+
# CMake
67+
cmake-build-*/
68+
69+
# Mongo Explorer plugin
70+
.idea/**/mongoSettings.xml
71+
72+
# File-based project format
73+
*.iws
74+
75+
# IntelliJ
76+
out/
77+
78+
# mpeltonen/sbt-idea plugin
79+
.idea_modules/
80+
81+
# JIRA plugin
82+
atlassian-ide-plugin.xml
83+
84+
# Cursive Clojure plugin
85+
.idea/replstate.xml
86+
87+
# Crashlytics plugin (for Android Studio and IntelliJ)
88+
com_crashlytics_export_strings.xml
89+
crashlytics.properties
90+
crashlytics-build.properties
91+
fabric.properties
92+
93+
# Editor-based Rest Client
94+
.idea/httpRequests
95+
96+
# Android studio 3.1+ serialized cache file
97+
.idea/caches/build_file_checksums.ser
98+
99+
### Intellij Patch ###
100+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
101+
102+
# *.iml
103+
# modules.xml
104+
# .idea/misc.xml
105+
# *.ipr
106+
107+
# Sonarlint plugin
108+
# https://plugins.jetbrains.com/plugin/7973-sonarlint
109+
.idea/**/sonarlint/
110+
111+
# SonarQube Plugin
112+
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
113+
.idea/**/sonarIssues.xml
114+
115+
# Markdown Navigator plugin
116+
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
117+
.idea/**/markdown-navigator.xml
118+
.idea/**/markdown-navigator-enh.xml
119+
.idea/**/markdown-navigator/
120+
121+
# Cache file creation bug
122+
# See https://youtrack.jetbrains.com/issue/JBR-2257
123+
.idea/$CACHE_FILE$
124+
125+
# CodeStream plugin
126+
# https://plugins.jetbrains.com/plugin/12206-codestream
127+
.idea/codestream.xml
128+
129+
### Linux ###
130+
*~
131+
132+
# temporary files which can be created if a process still has a handle open of a deleted file
133+
.fuse_hidden*
134+
135+
# KDE directory preferences
136+
.directory
137+
138+
# Linux trash folder which might appear on any partition or disk
139+
.Trash-*
140+
141+
# .nfs files are created when an open file is removed but is still being accessed
142+
.nfs*
143+
144+
### macOS ###
145+
# General
146+
.DS_Store
147+
.AppleDouble
148+
.LSOverride
149+
150+
# Icon must end with two \r
151+
Icon
152+
153+
# Thumbnails
154+
._*
155+
156+
# Files that might appear in the root of a volume
157+
.DocumentRevisions-V100
158+
.fseventsd
159+
.Spotlight-V100
160+
.TemporaryItems
161+
.Trashes
162+
.VolumeIcon.icns
163+
.com.apple.timemachine.donotpresent
164+
165+
# Directories potentially created on remote AFP share
166+
.AppleDB
167+
.AppleDesktop
168+
Network Trash Folder
169+
Temporary Items
170+
.apdisk
171+
172+
### VisualStudioCode ###
173+
.vscode/*
174+
!.vscode/settings.json
175+
!.vscode/tasks.json
176+
!.vscode/launch.json
177+
!.vscode/extensions.json
178+
*.code-workspace
179+
180+
### VisualStudioCode Patch ###
181+
# Ignore all local history of files
182+
.history
183+
184+
### Windows ###
185+
# Windows thumbnail cache files
186+
Thumbs.db
187+
Thumbs.db:encryptable
188+
ehthumbs.db
189+
ehthumbs_vista.db
190+
191+
# Dump file
192+
*.stackdump
193+
194+
# Folder config file
195+
[Dd]esktop.ini
196+
197+
# Recycle Bin used on file shares
198+
$RECYCLE.BIN/
199+
200+
# Windows Installer files
201+
*.cab
202+
*.msi
203+
*.msix
204+
*.msm
205+
*.msp
206+
207+
# Windows shortcuts
208+
*.lnk
209+
210+
# End of https://www.toptal.com/developers/gitignore/api/linux,windows,macos,visualstudiocode,intellij,go

README.md

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
## My Project
1+
## AWS Lambda Extensions
2+
### Sample projects
23

3-
TODO: Fill this README out!
4+
Extensions are a new way for tools to more easily integrate deeply into the Lambda execution environment to control and participate in Lambda’s lifecycle.
45

5-
Be sure to:
6+
You can use extensions to integrate your Lambda functions with your preferred monitoring, observability, security, and governance tools. You can choose from a broad set of tools provided by AWS Lambda partners or you can create your own Lambda extensions.
7+
8+
Extensions use the Extensions API, a new HTTP interface, to register for lifecycle events and get greater control during function initialization, invocation, and shutdown. They can also use environment variables to add options and tools to the runtime, or use wrapper scripts to customize the runtime startup behavior.
9+
10+
Note: an internal extension runs in the runtime process, and shares the same lifecycle as the runtime. An external extension runs as a separate process in the execution environment. The extension runs in parallel with the function's runtime. It is initialized before the function is invoked and continues to run after the function invocation is complete
11+
12+
For more information, see [Using AWS Lambda extensions](https://docs.aws.amazon.com/lambda/latest/dg/invocation-extensions.html).
13+
14+
In this repository you'll find a number of different sample projects and demos to help you get started with building your own extension. These include:
15+
16+
* [AWS AppConfig extension demo](awsappconfig-extension-demo/)
17+
* [Custom runtime extension demo](custom-runtime-extension-demo/)
18+
* [Extension in Go](go-example-extension/)
19+
* [Extension in Python](python-example-extension/)
20+
* [Inter-process communication extension in Go](go-example-ipc-extension/)
21+
* [Crash uploader extension in Go](go-example-crash-uploader-extension/)
22+
* [ElasticSearch extension in Python](python-example-elasticsearch-extension/)
23+
* [Lambda layer extension using SAM](go-example-extension-sam-layer/)
24+
* [Wrapper script in Bash](bash-wrapper-example/)
25+
* [Wrapper script in Python](python-wrapper-example/)
26+
* [Wrapper script in Ruby](ruby-wrapper-example/)
627

7-
* Change the title in this README
8-
* Edit your repository description on GitHub
928

1029
## Security
1130

bash-example-wrapper/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Example Wrapper Script in Bash
2+
The provided code sample demonstrates how to get a wrapper script written in Bash up and running.
3+
4+
There are two components to this sample:
5+
* `wrapper_script`: This is a Bash executable script that customizes the runtime startup by inserting additional parameters to the runtime process startup.
6+
* `lambda_function.py`: This is a sample Python file that includes a Lambda function handler that validates and demonstrates the use of the extra arguments that were included by the wrapper script.
7+
8+
## Customize the wrapper script
9+
You can modify or include other valid runtime parameters in the script as extra arguments.
10+
11+
```bash
12+
...
13+
# the extra options we want to pass to the interpreter
14+
extra_args=("-X" "importtime")
15+
...
16+
```
17+
18+
## Modify that the script access permissions
19+
You'll want to ensure that the script is executable by running the following command:
20+
21+
```bash
22+
$ chmod +x wrapper_script
23+
```
24+
25+
## Deploy a function to test is out
26+
Create a Lambda function for the Python runtime that includes both the `wrapper_script` and `lambda_function.py` using `lambda_function.lambda_handler` as the function handler.
27+
28+
Add an environment variable to your function's configuration with key `AWS_LAMBDA_EXEC_WRAPPER` and a value of `/var/task/wrapper_script` (if you've included the wrapper script alongside your function code).
29+
30+
## Invoke the function
31+
Invoke the function using a test eventand you should see the wrapper script in action reflected in the functions logs and invocation.
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: MIT-0
3+
4+
import json
5+
6+
def lambda_handler(event, context):
7+
# TODO implement
8+
return {
9+
'statusCode': 200,
10+
'body': json.dumps('Hello from Lambda!')
11+
}

bash-example-wrapper/wrapper_script

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: MIT-0
4+
5+
# the path to the interpreter and all of the originally intended arguments
6+
args=("$@")
7+
8+
# the extra options we want to pass to the interpreter
9+
extra_args=("-X" "importtime")
10+
11+
# insert the extra options
12+
args=("${args[@]:0:$#-1}" "${extra_args[@]}" "${args[@]: -1}")
13+
14+
# start the runtime with the extra options
15+
exec "${args[@]}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Example Crash Uploader Extension in Go
2+
3+
The provided code sample implements a sample extension that looks for core dumps in the execution environment and uploads them to an Amazon S3 bucket for later inspection and troubleshooting.
4+
5+
## Compile package and dependencies
6+
7+
To run this example, you will need to ensure that your build architecture matches that of the Lambda execution environment by compiling with `GOOS=linux` and `GOARCH=amd64` if you are not running in a Linux environment.
8+
9+
Building and saving package into a `bin/extensions` directory:
10+
```bash
11+
$ cd go-example-crash-uploader-extension
12+
$ GOOS=linux GOARCH=amd64 go build -o bin/extensions/go-example-crash-uploader-extension .
13+
$ chmod +x bin/extensions/go-example-crash-uploader-extension
14+
```
15+
16+
## Layer Setup Process
17+
The extensions .zip file should contain a root directory called `extensions/`, where the extension executables are located. In this sample project we must include the `go-example-crash-uploader-extension` binary.
18+
19+
Creating zip package for the extension:
20+
```bash
21+
$ cd bin
22+
$ zip -r extension.zip extensions/
23+
```
24+
25+
Ensure that you have aws-cli v2 for the commands below.
26+
Publish a new layer using the `extension.zip`. The output of the following command should provide you a layer arn.
27+
```bash
28+
aws lambda publish-layer-version \
29+
--layer-name "go-example-crash-uploader-extension" \
30+
--region <use your region> \
31+
--zip-file "fileb://extension.zip"
32+
```
33+
Note the LayerVersionArn that is produced in the output.
34+
eg. `"LayerVersionArn": "arn:aws:lambda:<region>:123456789012:layer:<layerName>:1"`
35+
36+
Add the newly created layer version to a Lambda function. Ensure to include environment variables like `BUCKET`="your-bucket-name".
37+
38+
39+
## Function Invocation and Extension Execution
40+
41+
When invoking the function, you should now see log messages from the example extension similar to the following:
42+
```
43+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX EXTENSION Name: go-example-crash-uploader-extension State: Ready Events: [INVOKE,SHUTDOWN]
44+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX START RequestId: 9ca08945-de9b-46ec-adc6-3fe9ef0d2e8d Version: $LATEST
45+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX [go-example-crash-uploader-extension] Registering...
46+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX [go-example-crash-uploader-extension] Register response: {
47+
"functionName": "my-function",
48+
"functionVersion": "$LATEST",
49+
"handler": "function.handler"
50+
}
51+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX [go-example-crash-uploader-extension] Waiting for event...
52+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX [go-example-crash-uploader-extension] Received event: {
53+
"eventType": "INVOKE",
54+
"deadlineMs": 1234567890123,
55+
"requestId": "9ca08945-de9b-46ec-adc6-3fe9ef0d2e8d",
56+
"invokedFunctionArn": "arn:aws:lambda:<region>:123456789012:function:my-function",
57+
"tracing": {
58+
"type": "X-Amzn-Trace-Id",
59+
"value": "XXXXXXXXXX"
60+
}
61+
}
62+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX [go-example-crash-uploader-extension] Waiting for event...
63+
...
64+
...
65+
Function logs...
66+
...
67+
...
68+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX END RequestId: 9ca08945-de9b-46ec-adc6-3fe9ef0d2e8d
69+
XXXX-XX-XXTXX:XX:XX.XXX-XX:XX REPORT RequestId: 9ca08945-de9b-46ec-adc6-3fe9ef0d2e8d Duration: 3.78 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 59 MB Init Duration: 264.75 ms
70+
```

0 commit comments

Comments
 (0)