Skip to content

Commit 2b40c32

Browse files
committed
Refactor templates and examples layout
1 parent 7e899b2 commit 2b40c32

21 files changed

+391
-279
lines changed

AGENTS.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ The runtime is packaged as a Lambda Layer and is intended to be consumed directl
3030
- package_layer.sh
3131
- smoke_test.sh
3232
- docker/Dockerfile
33-
- runtime-tutorial/
33+
- examples/
3434
- function.sh
3535
- README.md
36-
- docs/
37-
- USAGE.md
38-
- DEVELOPMENT.md
36+
- template/
37+
- template.yaml
38+
- template-arm64.yaml
39+
- template-amd64.yaml
40+
- aws-setup.yaml
41+
- USAGE.md
42+
- DEVELOPMENT.md
3943
- dist/
4044
- Makefile
4145
- README.md
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,20 @@ The Docker build uses `curl-minimal` to keep dependencies small. If you need ful
2727

2828
This produces `dist/lambda-shell-runtime-<arch>.zip` with `bootstrap`, `bin/`, `aws-cli/`, and `lib/` at the zip
2929
root, plus a versioned artifact named `dist/lambda-shell-runtime-<arch>-<aws-cli-version>.zip`. The script also
30-
updates the `SemanticVersion` in `template.yaml`, `template-arm64.yaml`, and `template-amd64.yaml` to match the
31-
bundled AWS CLI v2 version.
30+
generates `dist/template-*.yaml` with `SemanticVersion` set to the bundled AWS CLI v2 version and `ContentUri`
31+
pointing at the packaged zips.
32+
The source `template*.yaml` files remain static; publishing uses the generated templates.
3233

3334
To package both architectures:
3435

3536
```sh
3637
ARCH=all ./scripts/package_layer.sh
3738
```
3839

40+
Template generation overrides:
41+
- `TEMPLATE_VERSION` (override the `SemanticVersion` written into `dist/template-*.yaml`; defaults to the bundled AWS CLI v2 version)
42+
- `TEMPLATE_OUTPUT_DIR` (output directory for generated templates; default: `dist`)
43+
3944
## Smoke test
4045

4146
```sh
@@ -64,15 +69,15 @@ make test
6469
All shell scripts are written for POSIX `sh` and should pass `shellcheck`.
6570

6671
```sh
67-
shellcheck runtime/bootstrap scripts/*.sh runtime-tutorial/function.sh
72+
shellcheck runtime/bootstrap scripts/*.sh examples/function.sh
6873
```
6974

7075
## AWS configuration
7176

7277
Project defaults live in `scripts/aws_env.sh`. It pins the AWS region to `us-east-1` and sets default names
7378
for the S3 bucket, setup stack, SAR applications, and the S3 prefix used for SAR artifacts. These defaults
7479
are used by `make aws-check`, `make aws-setup`, and `make release`.
75-
`./scripts/package_layer.sh` keeps the SAR application names in the templates aligned with these defaults; rerun it after changing the app name settings.
80+
`./scripts/package_layer.sh` renders `dist/template-*.yaml` with SAR application names aligned with these defaults; rerun it after changing the app name settings.
7681
Set `ENV=dev` to switch to the dev defaults (the `*_DEV` values).
7782

7883
Override any of the defaults by exporting:
@@ -100,11 +105,10 @@ S3_BUCKET=your-bucket make release
100105

101106
It:
102107
- builds and packages both architectures
103-
- updates the templates' `SemanticVersion` to the bundled AWS CLI v2 version
108+
- generates `dist/template-*.yaml` with `SemanticVersion` set to the bundled AWS CLI v2 version
104109
- checks for an existing Git tag
105-
- if missing, commits the templates, tags the repo, and creates a GitHub release with versioned artifacts
106110
- publishes the SAR applications with `sam package`/`sam publish`
107-
- updates `template.yaml` with the arm64/amd64 ApplicationIds and publishes the wrapper application
111+
- renders the wrapper template with the arm64/amd64 ApplicationIds and publishes the wrapper application
108112
- uploads SAR artifacts under `S3_PREFIX/<version>/<arch>`
109113

110114
To check whether a release is needed without building anything, run:
@@ -189,7 +193,7 @@ To run in GitHub: Actions -> Release -> Run workflow.
189193

190194
Manual fallback:
191195
1. Ensure `ARCH=all ./scripts/package_layer.sh` has been run so the versioned artifacts are created.
192-
2. Tag the release in Git. Use the AWS CLI version as the tag to match the templates' `SemanticVersion`.
196+
2. Tag the release in Git. Use the AWS CLI version as the tag to match the generated templates' `SemanticVersion`.
193197

194198
## Publish to SAR
195199

@@ -208,17 +212,17 @@ To publish all three SAR applications (arm64, amd64, wrapper) in order:
208212
make publish-all
209213
```
210214

211-
`publish-all` requires the wrapper ApplicationIds to be populated in `template.yaml`.
215+
`make publish-wrapper` and `make publish-all` generate the wrapper template with the correct ApplicationIds automatically.
212216

213-
To publish the wrapper application after updating the ApplicationIds, run:
217+
To publish the wrapper application, run:
214218

215219
```sh
216220
make publish-wrapper
217221
```
218222

219-
If you want the raw SAM commands instead:
223+
If you want the raw SAM commands for the per-arch apps:
220224

221-
1. Build and package the layer (this updates the templates' `SemanticVersion` to match the bundled AWS CLI version):
225+
1. Build and package the layer (this generates `dist/template-*.yaml` with `SemanticVersion` set to the bundled AWS CLI version):
222226

223227
```sh
224228
./scripts/build_layer.sh
@@ -229,53 +233,35 @@ If you want the raw SAM commands instead:
229233

230234
```sh
231235
sam package \
232-
--template-file template-arm64.yaml \
236+
--template-file dist/template-arm64.yaml \
233237
--s3-bucket "$S3_BUCKET" \
234238
--s3-prefix "sar/<version>/arm64" \
235-
--output-template-file packaged-arm64.yaml
239+
--output-template-file dist/packaged-arm64.yaml
236240

237241
sam package \
238-
--template-file template-amd64.yaml \
242+
--template-file dist/template-amd64.yaml \
239243
--s3-bucket "$S3_BUCKET" \
240244
--s3-prefix "sar/<version>/amd64" \
241-
--output-template-file packaged-amd64.yaml
245+
--output-template-file dist/packaged-amd64.yaml
242246
```
243247

244248
3. Publish to SAR:
245249

246250
```sh
247-
sam publish --template packaged-arm64.yaml
248-
sam publish --template packaged-amd64.yaml
251+
sam publish --template dist/packaged-arm64.yaml
252+
sam publish --template dist/packaged-amd64.yaml
249253
```
250254

251-
4. Update the wrapper template with the architecture application IDs and publish it:
255+
4. Publish the wrapper application (it is rendered with the architecture application IDs):
252256

253257
```sh
254-
ARM64_APP_ID=$(aws serverlessrepo list-applications \
255-
--query "Applications[?Name=='lambda-shell-runtime-arm64'].ApplicationId | [0]" \
256-
--output text)
257-
AMD64_APP_ID=$(aws serverlessrepo list-applications \
258-
--query "Applications[?Name=='lambda-shell-runtime-amd64'].ApplicationId | [0]" \
259-
--output text)
260-
261-
sed -i.bak \
262-
-e "s|__APP_ID_ARM64__|$ARM64_APP_ID|g" \
263-
-e "s|__APP_ID_AMD64__|$AMD64_APP_ID|g" \
264-
template.yaml
265-
266-
sam package \
267-
--template-file template.yaml \
268-
--s3-bucket "$S3_BUCKET" \
269-
--s3-prefix "sar/<version>/wrapper" \
270-
--output-template-file packaged.yaml
271-
272-
sam publish --template packaged.yaml
258+
make publish-wrapper
273259
```
274260

275261
## AWS setup
276262

277263
Use `make aws-setup` to bootstrap the S3 bucket and create the SAR applications if they do not exist.
278-
CloudFormation cannot create SAR applications directly, so the target creates the bucket via `aws-setup.yaml`
264+
CloudFormation cannot create SAR applications directly, so the target creates the bucket via `template/aws-setup.yaml`
279265
and then runs `sam publish` to create the first SAR version if needed.
280266

281267
```sh

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
- `layer/opt`: staged layer contents for the host architecture
1818
- `scripts/`: build, package, and smoke test scripts
1919
- `docker/Dockerfile`: build image for Amazon Linux 2023 (arm64, x86_64)
20-
- `template.yaml`: SAR wrapper application (arm64 + amd64)
21-
- `template-arm64.yaml`, `template-amd64.yaml`: SAR application templates (SAM)
20+
- `template/template.yaml`: source SAR wrapper template (arm64 + amd64)
21+
- `template/template-arm64.yaml`, `template/template-amd64.yaml`: source SAR application templates (SAM)
22+
- `template/aws-setup.yaml`: CloudFormation stack for SAR setup
23+
- `dist/template-*.yaml`: generated publish/release templates (ignored in git)
24+
- `USAGE.md`: runtime contract and manual publishing
25+
- `DEVELOPMENT.md`: build and release workflow
2226
- `SAR_README.md`: SAR application README shown to end users
23-
- `runtime-tutorial/`: minimal handler example
24-
- `docs/`: usage and development notes
27+
- `examples/`: minimal handler example
2528

2629
## Build the layer
2730

@@ -39,7 +42,9 @@ The build image uses `curl-minimal` to keep dependencies small. If the AWS CLI d
3942

4043
The outputs are `dist/lambda-shell-runtime-arm64.zip` and `dist/lambda-shell-runtime-amd64.zip`, each containing
4144
`bootstrap`, `bin/`, `aws-cli/`, and `lib/` at the zip root (Lambda mounts them under `/opt`).
42-
`./scripts/package_layer.sh` also writes versioned artifacts named `dist/lambda-shell-runtime-<arch>-<aws-cli-version>.zip` and updates the `SemanticVersion` in both templates to match the bundled AWS CLI v2 version.
45+
`./scripts/package_layer.sh` also writes versioned artifacts named `dist/lambda-shell-runtime-<arch>-<aws-cli-version>.zip`
46+
and generates `dist/template-*.yaml` with `SemanticVersion` set to the bundled AWS CLI v2 version and `ContentUri` pointing
47+
at the packaged zips.
4348

4449
## Smoke test
4550

@@ -53,7 +58,7 @@ The outputs are `dist/lambda-shell-runtime-arm64.zip` and `dist/lambda-shell-run
5358
- Architecture: `arm64` or `x86_64`
5459
- Handler: `function.handler` (script stored as `function.sh` in your function package)
5560

56-
See [SAR_README.md](SAR_README.md) for end-user SAR installation and quick start, [docs/USAGE.md](docs/USAGE.md) for manual publishing and the runtime contract, [docs/DEVELOPMENT.md](docs/DEVELOPMENT.md) for build and release workflow, and [runtime-tutorial/README.md](runtime-tutorial/README.md) for a deployable example.
61+
See [SAR_README.md](SAR_README.md) for end-user SAR installation and quick start, [USAGE.md](USAGE.md) for manual publishing and the runtime contract, [DEVELOPMENT.md](DEVELOPMENT.md) for build and release workflow, and [examples/README.md](examples/README.md) for a deployable example.
5762

5863
## Publish to SAR
5964

@@ -66,22 +71,22 @@ Set `S3_BUCKET` to an S3 bucket in your account and run:
6671

6772
```sh
6873
sam package \
69-
--template-file template-arm64.yaml \
74+
--template-file dist/template-arm64.yaml \
7075
--s3-bucket "$S3_BUCKET" \
7176
--s3-prefix "sar" \
72-
--output-template-file packaged-arm64.yaml
77+
--output-template-file dist/packaged-arm64.yaml
7378

74-
sam publish --template packaged-arm64.yaml
79+
sam publish --template dist/packaged-arm64.yaml
7580

7681
sam package \
77-
--template-file template-amd64.yaml \
82+
--template-file dist/template-amd64.yaml \
7883
--s3-bucket "$S3_BUCKET" \
7984
--s3-prefix "sar" \
80-
--output-template-file packaged-amd64.yaml
85+
--output-template-file dist/packaged-amd64.yaml
8186

82-
sam publish --template packaged-amd64.yaml
87+
sam publish --template dist/packaged-amd64.yaml
8388
```
8489

8590
Each SAR application publishes a single layer output (`LayerVersionArn`) for its architecture.
86-
`./scripts/package_layer.sh` updates the templates' `SemanticVersion` to match the bundled AWS CLI v2 version; review and commit the change for each release.
87-
The wrapper application in `template.yaml` references both architecture-specific apps; `make release` and `make aws-setup` publish it after the per-arch apps.
91+
Generated templates live under `dist/` and are not committed; the source templates stay static.
92+
The wrapper application is rendered at publish time with the current per-arch ApplicationIds; `make release` and `make aws-setup` publish it after the per-arch apps.
File renamed without changes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Runtime Tutorial
1+
# Examples
22

33
This example mirrors the AWS custom runtime tutorial layout while using the runtime from the layer. It defines a
44
minimal handler function that reads JSON from the first argument and writes JSON to STDOUT. It also calls
@@ -16,7 +16,7 @@ The layer zips will be created at `dist/lambda-shell-runtime-arm64.zip` and `dis
1616
## Package the function
1717

1818
```sh
19-
cd runtime-tutorial
19+
cd examples
2020
zip -r ../dist/hello-function.zip function.sh
2121
```
2222

scripts/aws_check.sh

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set -eu
33

44
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
55
. "$root/scripts/aws_env.sh"
6+
. "$root/scripts/template_utils.sh"
67

78
require_cmd() {
89
if ! command -v "$1" >/dev/null 2>&1; then
@@ -243,25 +244,36 @@ resolve_version() {
243244
return 0
244245
fi
245246

246-
template_path="$root/template-$arch.yaml"
247-
if [ ! -f "$template_path" ]; then
248-
printf '%s\n' "Template not found at $template_path" >&2
249-
exit 1
247+
if [ -n "${RELEASE_VERSION:-}" ]; then
248+
printf '%s\n' "$RELEASE_VERSION"
249+
return 0
250250
fi
251-
version=$(awk -F': *' '/^[[:space:]]*SemanticVersion:/ {print $2; exit}' "$template_path")
252-
case "$version" in
253-
''|*[!0-9.]*|*.*.*.*)
254-
printf '%s\n' "Unable to parse SemanticVersion from $template_path: $version" >&2
255-
exit 1
256-
;;
257-
*.*.*)
258-
;;
259-
*)
260-
printf '%s\n' "Unable to parse SemanticVersion from $template_path: $version" >&2
261-
exit 1
262-
;;
263-
esac
264-
printf '%s\n' "$version"
251+
252+
if [ -n "${LSR_SAR_VERSION:-}" ]; then
253+
printf '%s\n' "$LSR_SAR_VERSION"
254+
return 0
255+
fi
256+
257+
if [ -n "${AWSCLI_VERSION:-}" ]; then
258+
printf '%s\n' "$AWSCLI_VERSION"
259+
return 0
260+
fi
261+
262+
template_path=$(template_output_path "$arch")
263+
if [ -f "$template_path" ]; then
264+
template_semantic_version "$template_path"
265+
return 0
266+
fi
267+
268+
template_path=$(template_source_path "$arch")
269+
if [ -f "$template_path" ]; then
270+
printf '%s\n' "Using version from source template: $template_path" >&2
271+
template_semantic_version "$template_path"
272+
return 0
273+
fi
274+
275+
printf '%s\n' "Template not found for $arch." >&2
276+
exit 1
265277
}
266278

267279
check_s3_rw() {

0 commit comments

Comments
 (0)