Skip to content

Commit 9e6210c

Browse files
author
Joe Snell
committed
initial
0 parents  commit 9e6210c

File tree

9 files changed

+232
-0
lines changed

9 files changed

+232
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
.DS_Store

README.md

Whitespace-only changes.

python/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*/__pycache__/*
2+
*.pyc
3+
4+
requirements.txt
5+
build
6+
extension.zip

python/Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
boto3 = "*"
8+
9+
[packages]
10+
11+
[requires]
12+
python_version = "3.8"

python/Pipfile.lock

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/bin/build.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
[ -d build ] && rm -rf build
6+
[ -f extension.zip ] && rm extension.zip
7+
8+
pipenv lock -r > requirements.txt
9+
pip install -r requirements.txt -t build
10+
11+
chmod +x extensions/parameter-store-extension
12+
chmod +x parameter-store-extension/extension.py
13+
14+
mkdir -p build/opt/extensions
15+
16+
cp -R extensions build/opt/
17+
cp -R parameter-store-extension build/opt/
18+
19+
zip -r extension.zip build
20+
21+
aws lambda publish-layer-version \
22+
--layer-name "lambda-extensions_parameter-store" \
23+
--region "us-east-1" \
24+
--zip-file "fileb://extension.zip"

python/bin/deploy.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
bin/build.sh
6+
7+
aws lambda publish-layer-version \
8+
--layer-name "lambda-extensions_parameter-store" \
9+
--region "us-east-1" \
10+
--zip-file "fileb://extension.zip" \
11+
--compatible-runtimes python3.8
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: MIT-0
4+
5+
set -euo pipefail
6+
7+
OWN_FILENAME="$(basename $0)"
8+
LAMBDA_EXTENSION_NAME="$OWN_FILENAME" # (external) extension name has to match the filename
9+
10+
echo "${LAMBDA_EXTENSION_NAME} launching extension"
11+
exec "/opt/${LAMBDA_EXTENSION_NAME}/extension.py"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python3
2+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: MIT-0
4+
5+
import json
6+
import os
7+
import boto3
8+
import requests
9+
import signal
10+
import sys
11+
from pathlib import Path
12+
13+
14+
# global variables
15+
# extension name has to match the file's parent directory name)
16+
LAMBDA_EXTENSION_NAME = Path(__file__).parent.name
17+
18+
19+
# custom extension code
20+
def execute_custom_processing(event):
21+
# perform custom per-event processing here
22+
print(f"[{LAMBDA_EXTENSION_NAME}] Received event: {json.dumps(event)}", flush=True)
23+
24+
25+
# boiler plate code
26+
def handle_signal(signal, frame):
27+
# if needed pass this signal down to child processes
28+
print(f"[{LAMBDA_EXTENSION_NAME}] Received signal={signal}. Exiting.", flush=True)
29+
sys.exit(0)
30+
31+
32+
def register_extension():
33+
print(f"[{LAMBDA_EXTENSION_NAME}] Registering...", flush=True)
34+
headers = {
35+
'Lambda-Extension-Name': LAMBDA_EXTENSION_NAME,
36+
}
37+
payload = {
38+
'events': [
39+
'INVOKE',
40+
'SHUTDOWN'
41+
],
42+
}
43+
response = requests.post(
44+
url=f"http://{os.environ['AWS_LAMBDA_RUNTIME_API']}/2020-01-01/extension/register",
45+
json=payload,
46+
headers=headers
47+
)
48+
ext_id = response.headers['Lambda-Extension-Identifier']
49+
print(f"[{LAMBDA_EXTENSION_NAME}] Registered with ID: {ext_id}", flush=True)
50+
51+
return ext_id
52+
53+
54+
def process_events(ext_id, client):
55+
headers = {
56+
'Lambda-Extension-Identifier': ext_id
57+
}
58+
59+
while True:
60+
print(f"[{LAMBDA_EXTENSION_NAME}] Waiting for event...", flush=True)
61+
62+
response = requests.get(
63+
url=f"http://{os.environ['AWS_LAMBDA_RUNTIME_API']}/2020-01-01/extension/event/next",
64+
headers=headers,
65+
timeout=None
66+
)
67+
event = json.loads(response.text)
68+
if event['eventType'] == 'SHUTDOWN':
69+
print(f"[{LAMBDA_EXTENSION_NAME}] Received SHUTDOWN event. Exiting.", flush=True)
70+
sys.exit(0)
71+
else:
72+
execute_custom_processing(event)
73+
74+
client = None
75+
76+
def main():
77+
global client
78+
if client is None:
79+
client = boto3.client('ssm')
80+
81+
# handle signals
82+
signal.signal(signal.SIGINT, handle_signal)
83+
signal.signal(signal.SIGTERM, handle_signal)
84+
85+
# execute extensions logic
86+
extension_id = register_extension()
87+
process_events(extension_id, client)
88+
89+
90+
if __name__ == "__main__":
91+
main()

0 commit comments

Comments
 (0)