Skip to content

Commit 3922783

Browse files
committed
multi architecture support added in zip.sh
added example for creating arm64 image
1 parent 2843bc5 commit 3922783

File tree

9 files changed

+195
-59
lines changed

9 files changed

+195
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The Sumo Logic lambda extension is available as an AWS public Layer. The latest
1515

1616
- AWS_REGION - Replace with your AWS Lambda Region.
1717

18-
### Receive logs during AWS Lambda execution time
18+
### Receive logs during AWS Lambda execution time
1919
All the logs that are not sent to Sumo Logic during the Execution phase of the AWS Lambda, are sent during the shutdown phase instead. For more details on phases on the lifecycle and AWS Lambda phases please see the[ AWS documentation ](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html).
2020

2121
If you would like to always send logs during the execution phase however, you can add extra execution time via a sleep function at the end of lambda code, which will give your extension time to run and send logs to Sumo Logic. We recommend setting this to two seconds.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Define global args
2+
ARG FUNCTION_DIR="app"
3+
ARG RUNTIME_VERSION="3.9"
4+
ARG DISTRO_VERSION="3.12"
5+
6+
# Stage 1 - bundle base image + runtime
7+
# Grab a fresh copy of the ARM 64 based image and install GCC
8+
FROM arm64v8/python:${RUNTIME_VERSION}-alpine${DISTRO_VERSION} AS python-alpine
9+
# Install GCC (Alpine uses musl but we compile and link dependencies with GCC)
10+
RUN apk add --no-cache \
11+
libstdc++
12+
13+
# Stage 2 - build function and dependencies
14+
FROM python-alpine AS build-image
15+
# Install aws-lambda-cpp build dependencies
16+
RUN apk add --no-cache \
17+
build-base \
18+
libtool \
19+
autoconf \
20+
automake \
21+
libexecinfo-dev \
22+
make \
23+
cmake \
24+
libcurl
25+
# Include global args in this stage of the build
26+
ARG FUNCTION_DIR
27+
ARG RUNTIME_VERSION
28+
# Create function directory
29+
RUN mkdir -p ${FUNCTION_DIR}
30+
# Copy handler function
31+
COPY app/* ${FUNCTION_DIR}
32+
# Optional – Install the function's dependencies
33+
# RUN python${RUNTIME_VERSION} -m pip install -r requirements.txt --target ${FUNCTION_DIR}
34+
# Install Lambda Runtime Interface Client for Python
35+
RUN pip install awslambdaric --target ${FUNCTION_DIR}
36+
37+
# Stage 3 - final runtime image
38+
# Grab a fresh copy of the Python image
39+
FROM python-alpine
40+
# Include global arg in this stage of the build
41+
ARG FUNCTION_DIR
42+
# Set working directory to function root directory it also creates the directory and cd to that directory
43+
WORKDIR ${FUNCTION_DIR}
44+
# Copy in the built dependencies
45+
COPY --from=build-image ${FUNCTION_DIR} ./
46+
# (Optional) Add Lambda Runtime Interface Emulator and use a script in the ENTRYPOINT for simpler local runs
47+
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/bin/aws-lambda-rie
48+
# SumoLogic Lambda extension
49+
ADD sumologic-extension-arm64.tar.gz /opt/
50+
COPY entry.sh /
51+
RUN chmod 755 /usr/bin/aws-lambda-rie /entry.sh
52+
ENTRYPOINT [ "/entry.sh" ]
53+
CMD [ "app.handler" ]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import sys
2+
import time
3+
4+
5+
def handler(event, context):
6+
time.sleep(2)
7+
return 'Hello from AWS Lambda using Python' + sys.version + '!'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
docker build -t lambda/hello-world-python:3.9-alpine3.12 .
2+
3+
## Command to run container
4+
# docker run -p 9000:8080 lambda/hello-world-python:3.9-alpine3.12
5+
6+
## Command to test
7+
# curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
8+
9+
## Command to push image
10+
ACCOUNT_ID=956882708938
11+
aws ecr create-repository --repository-name hello-world-python-arm64 --image-scanning-configuration scanOnPush=true
12+
docker tag lambda/hello-world-python:3.9-alpine3.12 "${ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/"hello-world-python-arm64:latest
13+
aws ecr get-login-password | docker login --username AWS --password-stdin "${ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com"
14+
docker push "${ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/hello-world-python-arm64:latest"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
3+
exec /usr/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $1
4+
else
5+
exec /usr/local/bin/python -m awslambdaric $1
6+
fi
Binary file not shown.
Binary file not shown.

scripts/delete_old_layer_versions.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
AWS_REGIONS=(
2+
us-east-1
3+
us-east-2
4+
eu-north-1
5+
ap-south-1
6+
eu-west-3
7+
eu-west-2
8+
eu-south-1
9+
eu-west-1
10+
ap-northeast-2
11+
me-south-1
12+
ap-northeast-1
13+
sa-east-1
14+
ca-central-1
15+
ap-east-1
16+
ap-southeast-1
17+
ap-southeast-2
18+
eu-central-1
19+
us-west-1
20+
us-west-2
21+
)
22+
23+
24+
if [[ -z "${AWS_PROFILE}" ]]; then
25+
export AWS_PROFILE="personal"
26+
fi
27+
echo "Using AWS_PROFILE: ${AWS_PROFILE}"
28+
29+
binary_name="sumologic-extension"
30+
31+
ARCHITECTURES=(
32+
amd64
33+
arm64
34+
)
35+
layer_version=1
36+
for arch in "${ARCHITECTURES[@]}"; do
37+
38+
layer_name="${binary_name}-${arch}"
39+
40+
for region in "${AWS_REGIONS[@]}"; do
41+
echo "Layer Arn: arn:aws:lambda:${region}:<accountId>:layer:${layer_name}:${layer_version} deleted from Region ${region}"
42+
aws lambda delete-layer-version --layer-name ${layer_name} --version-number ${layer_version} --region ${region}
43+
done
44+
done

scripts/zip.sh

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,83 @@ fi
1414

1515
echo "Creating an binary executable using the go build command for Linux Systems."
1616
binary_name="sumologic-extension"
17-
extension_bin_dir="${TARGET_DIR}/extensions"
18-
extension_zip_dir="${TARGET_DIR}/zip"
1917

20-
mkdir -p ${extension_bin_dir}
21-
mkdir -p ${extension_zip_dir}
2218

23-
env GOOS=linux go build -o "${extension_bin_dir}/${binary_name}" "lambda-extensions/${binary_name}.go"
19+
ARCHITECTURES=(
20+
amd64
21+
arm64
22+
)
2423

25-
status=$?
26-
if [ $status -ne 0 ]; then
27-
echo "Binary Generation Failed"
28-
exit 1
29-
fi
30-
chmod +x "${extension_bin_dir}/${binary_name}"
31-
32-
echo "Creating the Zip file binary in extension folder."
33-
cd ${TARGET_DIR}
34-
zip -r "zip/${binary_name}.zip" extensions/
35-
status=$?
36-
if [ $status -ne 0 ]; then
37-
echo "Zip Generation Failed"
38-
exit 1
39-
fi
40-
cd ..
24+
for arch in "${ARCHITECTURES[@]}"; do
4125

42-
echo "Create lambda Layer from the new ZIP file in the provided AWS_PROFILE aws account."
43-
if [[ -z "${AWS_PROFILE}" ]]; then
44-
export AWS_PROFILE="personal"
45-
fi
26+
echo "Creating an binary executable for $arch"
27+
extension_bin_dir="${TARGET_DIR}/${arch}/extensions"
28+
extension_zip_dir="${TARGET_DIR}/${arch}/zip"
29+
mkdir -p "${extension_bin_dir}"
30+
mkdir -p "${extension_zip_dir}"
4631

47-
AWS_REGIONS=(
48-
us-east-1
49-
us-east-2
50-
eu-north-1
51-
ap-south-1
52-
eu-west-3
53-
eu-west-2
54-
eu-south-1
55-
eu-west-1
56-
ap-northeast-2
57-
me-south-1
58-
ap-northeast-1
59-
sa-east-1
60-
ca-central-1
61-
ap-east-1
62-
ap-southeast-1
63-
ap-southeast-2
64-
eu-central-1
65-
us-west-1
66-
us-west-2
67-
)
32+
env GOOS="linux" GOARCH="$arch" go build -o "${extension_bin_dir}/${binary_name}" "lambda-extensions/${binary_name}.go"
33+
34+
status=$?
35+
if [ $status -ne 0 ]; then
36+
echo "Binary Generation Failed"
37+
exit 1
38+
fi
39+
chmod +x "${extension_bin_dir}/${binary_name}"
40+
41+
echo "Creating the Zip file binary in extension folder."
42+
cd "${TARGET_DIR}/${arch}"
43+
zip -r "zip/${binary_name}.zip" extensions/
44+
tar -czvf "zip/${binary_name}-${arch}.tar.gz" extensions/
45+
status=$?
46+
if [ $status -ne 0 ]; then
47+
echo "Zip Generation Failed"
48+
exit 1
49+
fi
50+
cd -
51+
52+
echo "Create lambda Layer from the new ZIP file in the provided AWS_PROFILE aws account."
53+
if [[ -z "${AWS_PROFILE}" ]]; then
54+
export AWS_PROFILE="personal"
55+
fi
56+
57+
AWS_REGIONS=(
58+
us-east-1
59+
us-east-2
60+
eu-north-1
61+
ap-south-1
62+
eu-west-3
63+
eu-west-2
64+
eu-south-1
65+
eu-west-1
66+
ap-northeast-2
67+
me-south-1
68+
ap-northeast-1
69+
sa-east-1
70+
ca-central-1
71+
ap-east-1
72+
ap-southeast-1
73+
ap-southeast-2
74+
eu-central-1
75+
us-west-1
76+
us-west-2
77+
)
78+
79+
echo "Using AWS_PROFILE: ${AWS_PROFILE}"
6880

69-
echo "Using AWS_PROFILE: ${AWS_PROFILE}"
81+
# We have layer name as sumologic-extension. Please change name for local testing.
82+
layer_name="${binary_name}-${arch}"
7083

71-
# We have layer name as sumologic-extension. Please change name for local testing.
72-
layer_name=${binary_name}
84+
for region in "${AWS_REGIONS[@]}"; do
85+
layer_version=$(aws lambda publish-layer-version --layer-name ${layer_name} \
86+
--description "The SumoLogic Extension collects lambda logs and send it to Sumo Logic." \
87+
--license-info "Apache-2.0" --zip-file fileb://$(pwd)/${extension_zip_dir}/${binary_name}.zip \
88+
--profile ${AWS_PROFILE} --region ${region} --output text --query Version )
89+
echo "Layer Arn: arn:aws:lambda:${region}:<accountId>:layer:${layer_name}:${layer_version} deployed to Region ${region}"
7390

74-
for region in "${AWS_REGIONS[@]}"; do
75-
layer_version=$(aws lambda publish-layer-version --layer-name ${layer_name} \
76-
--description "The SumoLogic Extension collects lambda logs and send it to Sumo Logic." \
77-
--license-info "Apache-2.0" --zip-file fileb://$(pwd)/${TARGET_DIR}/zip/${layer_name}.zip \
78-
--profile ${AWS_PROFILE} --region ${region} --output text --query Version )
79-
echo "Layer Arn: arn:aws:lambda:${region}:<accountId>:layer:${layer_name}:${layer_version} deployed to Region ${region}"
91+
echo "Setting public permissions for layer version: ${layer_version}"
92+
aws lambda add-layer-version-permission --layer-name ${layer_name} --statement-id ${layer_name}-prod --version-number $layer_version --principal '*' --action lambda:GetLayerVersion --region ${region}
93+
# aws lambda add-layer-version-permission --layer-name ${layer_name} --statement-id ${layer_name}-dev --version-number ${layer_version} --principal '956882708938' --action lambda:GetLayerVersion --region ${region}
94+
done
8095

81-
echo "Setting public permissions for layer version: ${layer_version}"
82-
aws lambda add-layer-version-permission --layer-name ${layer_name} --statement-id ${layer_name}-prod --version-number $layer_version --principal '*' --action lambda:GetLayerVersion --region ${region}
83-
# aws lambda add-layer-version-permission --layer-name ${layer_name} --statement-id ${layer_name}-dev --version-number ${layer_version} --principal '956882708938' --action lambda:GetLayerVersion --region ${region}
8496
done

0 commit comments

Comments
 (0)