Skip to content

Commit 8f5a2ed

Browse files
authored
Add release workflow for lambda layer (#90)
*Description of changes:* Automate the lambda layer releasing. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent f5df349 commit 8f5a2ed

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
name: Release Lambda layer
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
aws_region:
7+
description: 'Deploy to aws regions'
8+
required: true
9+
default: 'us-east-1, us-east-2, us-west-1, us-west-2, ap-south-1, ap-northeast-3, ap-northeast-2, ap-southeast-1, ap-southeast-2, ap-northeast-1, ca-central-1, eu-central-1, eu-west-1, eu-west-2, eu-west-3, eu-north-1, sa-east-1, af-south-1, ap-east-1, ap-south-2, ap-southeast-3, ap-southeast-4, eu-central-2, eu-south-1, eu-south-2, il-central-1, me-central-1, me-south-1'
10+
11+
env:
12+
COMMERCIAL_REGIONS: us-east-1, us-east-2, us-west-1, us-west-2, ap-south-1, ap-northeast-3, ap-northeast-2, ap-southeast-1, ap-southeast-2, ap-northeast-1, ca-central-1, eu-central-1, eu-west-1, eu-west-2, eu-west-3, eu-north-1, sa-east-1
13+
# LAYER_NAME: AWSOpenTelemetryDistroJs
14+
LAYER_NAME: AWSOpenTelemetryDistroJsBeta
15+
16+
permissions:
17+
id-token: write
18+
contents: write
19+
20+
jobs:
21+
build-layer:
22+
runs-on: ubuntu-latest
23+
outputs:
24+
aws_regions_json: ${{ steps.set-matrix.outputs.aws_regions_json }}
25+
steps:
26+
- name: Set up regions matrix
27+
id: set-matrix
28+
run: |
29+
IFS=',' read -ra REGIONS <<< "${{ github.event.inputs.aws_region }}"
30+
MATRIX="["
31+
for region in "${REGIONS[@]}"; do
32+
trimmed_region=$(echo "$region" | xargs)
33+
MATRIX+="\"$trimmed_region\","
34+
done
35+
MATRIX="${MATRIX%,}]"
36+
echo ${MATRIX}
37+
echo "aws_regions_json=${MATRIX}" >> $GITHUB_OUTPUT
38+
- name: Checkout Repo @ SHA - ${{ github.sha }}
39+
uses: actions/checkout@v4
40+
- name: Setup Node
41+
uses: actions/setup-node@v4
42+
with:
43+
node-version: 20
44+
- name: NPM Clean Install
45+
# https://docs.npmjs.com/cli/v10/commands/npm-ci
46+
run: npm ci
47+
- name: Compile all NPM projects
48+
run: npm run compile
49+
- name: Build Lambda Layer
50+
run: npm run build-lambda
51+
- name: upload layer
52+
uses: actions/upload-artifact@v3
53+
with:
54+
name: layer.zip
55+
path: lambda-layer/packages/layer/build/layer.zip
56+
publish-prod:
57+
runs-on: ubuntu-latest
58+
needs: build-layer
59+
strategy:
60+
matrix:
61+
aws_region: ${{ fromJson(needs.build-layer.outputs.aws_regions_json) }}
62+
steps:
63+
- name: role arn
64+
env:
65+
COMMERCIAL_REGIONS: ${{ env.COMMERCIAL_REGIONS }}
66+
run: |
67+
COMMERCIAL_REGIONS_ARRAY=(${COMMERCIAL_REGIONS//,/ })
68+
FOUND=false
69+
for REGION in "${COMMERCIAL_REGIONS_ARRAY[@]}"; do
70+
if [[ "$REGION" == "${{ matrix.aws_region }}" ]]; then
71+
FOUND=true
72+
break
73+
fi
74+
done
75+
if [ "$FOUND" = true ]; then
76+
echo "Found ${{ matrix.aws_region }} in COMMERCIAL_REGIONS"
77+
SECRET_KEY="LAMBDA_LAYER_RELEASE"
78+
else
79+
echo "Not found ${{ matrix.aws_region }} in COMMERCIAL_REGIONS"
80+
SECRET_KEY="${{ matrix.aws_region }}_LAMBDA_LAYER_RELEASE"
81+
fi
82+
SECRET_KEY=${SECRET_KEY//-/_}
83+
echo "SECRET_KEY=${SECRET_KEY}" >> $GITHUB_ENV
84+
- uses: aws-actions/[email protected]
85+
with:
86+
role-to-assume: ${{ secrets[env.SECRET_KEY] }}
87+
role-duration-seconds: 1200
88+
aws-region: ${{ matrix.aws_region }}
89+
- name: Get s3 bucket name for release
90+
run: |
91+
echo BUCKET_NAME=nodejs-lambda-layer-${{ github.run_id }}-${{ matrix.aws_region }} | tee --append $GITHUB_ENV
92+
- name: download layer.zip
93+
uses: actions/download-artifact@v3
94+
with:
95+
name: layer.zip
96+
- name: publish
97+
run: |
98+
aws s3 mb s3://${{ env.BUCKET_NAME }}
99+
aws s3 cp layer.zip s3://${{ env.BUCKET_NAME }}
100+
layerARN=$(
101+
aws lambda publish-layer-version \
102+
--layer-name ${{ env.LAYER_NAME }} \
103+
--content S3Bucket=${{ env.BUCKET_NAME }},S3Key=layer.zip \
104+
--compatible-runtimes nodejs18.x nodejs20.x \
105+
--compatible-architectures "arm64" "x86_64" \
106+
--license-info "Apache-2.0" \
107+
--description "AWS Distro of OpenTelemetry Lambda Layer for NodeJs Runtime" \
108+
--query 'LayerVersionArn' \
109+
--output text
110+
)
111+
echo $layerARN
112+
echo "LAYER_ARN=${layerARN}" >> $GITHUB_ENV
113+
mkdir ${{ env.LAYER_NAME }}
114+
echo $layerARN > ${{ env.LAYER_NAME }}/${{ matrix.aws_region }}
115+
cat ${{ env.LAYER_NAME }}/${{ matrix.aws_region }}
116+
- name: public layer
117+
run: |
118+
layerVersion=$(
119+
aws lambda list-layer-versions \
120+
--layer-name ${{ env.LAYER_NAME }} \
121+
--query 'max_by(LayerVersions, &Version).Version'
122+
)
123+
aws lambda add-layer-version-permission \
124+
--layer-name ${{ env.LAYER_NAME }} \
125+
--version-number $layerVersion \
126+
--principal "*" \
127+
--statement-id publish \
128+
--action lambda:GetLayerVersion
129+
- name: upload layer arn artifact
130+
if: ${{ success() }}
131+
uses: actions/upload-artifact@v3
132+
with:
133+
name: ${{ env.LAYER_NAME }}
134+
path: ${{ env.LAYER_NAME }}/${{ matrix.aws_region }}
135+
- name: clean s3
136+
if: always()
137+
run: |
138+
aws s3 rb --force s3://${{ env.BUCKET_NAME }}
139+
generate-release-note:
140+
runs-on: ubuntu-latest
141+
needs: publish-prod
142+
steps:
143+
- name: Checkout Repo @ SHA - ${{ github.sha }}
144+
uses: actions/checkout@v4
145+
- uses: hashicorp/setup-terraform@v2
146+
- name: download layerARNs
147+
uses: actions/download-artifact@v3
148+
with:
149+
name: ${{ env.LAYER_NAME }}
150+
path: ${{ env.LAYER_NAME }}
151+
- name: show layerARNs
152+
run: |
153+
for file in ${{ env.LAYER_NAME }}/*
154+
do
155+
echo $file
156+
cat $file
157+
done
158+
- name: generate layer-note
159+
working-directory: ${{ env.LAYER_NAME }}
160+
run: |
161+
echo "| Region | Layer ARN |" >> ../layer-note
162+
echo "| ---- | ---- |" >> ../layer-note
163+
for file in *
164+
do
165+
read arn < $file
166+
echo "| " $file " | " $arn " |" >> ../layer-note
167+
done
168+
cd ..
169+
cat layer-note
170+
- name: generate tf layer
171+
working-directory: ${{ env.LAYER_NAME }}
172+
run: |
173+
echo "locals {" >> ../layer.tf
174+
echo " sdk_layer_arns = {" >> ../layer.tf
175+
for file in *
176+
do
177+
read arn < $file
178+
echo " \""$file"\" = \""$arn"\"" >> ../layer.tf
179+
done
180+
cd ..
181+
echo " }" >> layer.tf
182+
echo "}" >> layer.tf
183+
terraform fmt layer.tf
184+
cat layer.tf
185+
- name: upload layer tf file
186+
uses: actions/upload-artifact@v3
187+
with:
188+
name: layer.tf
189+
path: layer.tf

0 commit comments

Comments
 (0)