Skip to content

Commit 944e45a

Browse files
committed
🌅 New template for Rust extensions.
0 parents  commit 944e45a

File tree

13 files changed

+283
-0
lines changed

13 files changed

+283
-0
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [calavera]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Rust Build
2+
description: Setups a Rust project for AWS Lambda
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- uses: dtolnay/rust-toolchain@stable
8+
9+
- uses: korandoru/setup-zig@v1
10+
with:
11+
zig-version: 0.13.0
12+
13+
# Install Cargo Lambda from source until 1.5 is released
14+
#
15+
# - uses: cargo-bins/cargo-binstall@main
16+
# - name: Install cargo-lambda
17+
# shell: bash
18+
# run: cargo binstall -y cargo-lambda
19+
#
20+
- name: Install cargo-lambda
21+
shell: bash
22+
run: |
23+
git clone https://github.com/cargo-lambda/cargo-lambda.git && \
24+
cd cargo-lambda && \
25+
make install-local
26+
27+
- uses: Swatinem/rust-cache@v2

.github/workflows/test.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Test template
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".github/actions/**"
7+
- ".github/workflows/**"
8+
- "template/**"
9+
- "CargoLambda.toml"
10+
push:
11+
branches:
12+
- main
13+
paths:
14+
- ".github/actions/**"
15+
- ".github/workflows/**"
16+
- "template/**"
17+
- "CargoLambda.toml"
18+
workflow_dispatch:
19+
20+
jobs:
21+
test-events-extension:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: ./.github/actions/rust-setup
26+
27+
- name: Test events extension
28+
run: |
29+
mkdir -p tests && cd tests && \
30+
cargo lambda new --extension -y --template .. events_extension && \
31+
cd events_extension && \
32+
cargo test && \
33+
cargo lambda build --release --extension
34+
35+
test-logs-extension:
36+
runs-on: ubuntu-latest
37+
steps:
38+
- uses: actions/checkout@v4
39+
- uses: ./.github/actions/rust-setup
40+
41+
- name: Test logs extension without events
42+
run: |
43+
mkdir -p tests && cd tests && \
44+
cargo lambda new --extension --logs --template .. logs_extension && \
45+
cd logs_extension && \
46+
cargo test && \
47+
cargo lambda build --release --extension
48+
49+
- name: Test logs extension with events
50+
run: |
51+
mkdir -p tests && cd tests && \
52+
cargo lambda new --extension --logs --events --template .. logs_extension_events && \
53+
cd logs_extension_events && \
54+
cargo test && \
55+
cargo lambda build --release --extension
56+
57+
test-telemetry-extension:
58+
runs-on: ubuntu-latest
59+
steps:
60+
- uses: actions/checkout@v4
61+
- uses: ./.github/actions/rust-setup
62+
63+
- name: Test telemetry extension without events
64+
run: |
65+
mkdir -p tests && cd tests && \
66+
cargo lambda new --extension --telemetry --template .. telemetry_extension && \
67+
cd telemetry_extension && \
68+
cargo test && \
69+
cargo lambda build --release --extension
70+
71+
- name: Test telemetry extension with events
72+
run: |
73+
mkdir -p tests && cd tests && \
74+
cargo lambda new --extension --telemetry --events --template .. telemetry_extension_events && \
75+
cd telemetry_extension_events && \
76+
cargo test && \
77+
cargo lambda build --release --extension

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tests

CargoLambda.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[template]
2+
render_all_files = true
3+
4+
[template.ignore_conditional_files]
5+
"src/events_extension.rs" = { var = "events", match = false }
6+
"src/logs_extension.rs" = { var = "logs", match = false }
7+
"src/telemetry_extension.rs" = { var = "telemetry", match = false }

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2024 David Calavera <david.calavera@gmail.com>
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Introduction
2+
3+
This repository contains a template for a Rust project that implements an AWS Lambda extension.
4+
5+
# Prerequisites
6+
7+
- [Rust](https://www.rust-lang.org/tools/install)
8+
- [Cargo Lambda](https://www.cargo-lambda.info/guide/installation.html)
9+
10+
# Usage
11+
12+
This is the default extensions template that Cargo Lambda uses starting from version 1.5.0. This template doesn't work with older versions of Cargo Lambda.
13+
14+
To create a new project with this template you can simply run `cargo lambda new --extension PROJECT_NAME`. That will download this template and create a new project with the name `PROJECT_NAME`.
15+
16+
If you're making changes to this template, you can run `cargo lambda new --extension --template ./path/to/template PROJECT_NAME` to create a new project with the changes you made to the template.
17+
18+
If you want to create a new project with a specific branch of this repository, you can run `cargo lambda new --extension --template https://github.com/cargo-lambda/new-extensions-template/tree/BRANCH_NAME PROJECT_NAME`.
19+
20+
Read more about Cargo Lambda Templates in [the Cargo Lambda documentation](https://www.cargo-lambda.info/commands/new.html#templates).

template/Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "{{ project_name }}"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
lambda-extension = "{{ lambda_extension_version | default: "0.11.0" }}"
8+
serde = "1"
9+
tokio = { version = "1", features = ["macros"] }
10+
11+
{%- if binary_name %}
12+
[[bin]]
13+
name = "{{ binary_name }}"
14+
path = "src/main.rs"
15+
test = false
16+
{%- endif %}

template/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Introduction
2+
3+
{{ project_name }} is a Rust project that implements an AWS Lambda extension in Rust.
4+
5+
# Prerequisites
6+
7+
- [Rust](https://www.rust-lang.org/tools/install)
8+
- [Cargo Lambda](https://www.cargo-lambda.info/guide/installation.html)
9+
10+
# Building
11+
12+
To build the project for production, run `cargo lambda build --extension --release`. Remove the `--release` flag to build for development.
13+
14+
Read more about building your lambda extension in [the Cargo Lambda documentation](https://www.cargo-lambda.info/commands/build.html#extensions).
15+
16+
# Deploying
17+
18+
To deploy the project, run `cargo lambda deploy --extension`. This will upload the extension to your AWS account as an AWS Lambda Layer.
19+
20+
Read more about deploying your lambda extension in [the Cargo Lambda documentation](https://www.cargo-lambda.info/commands/deploy.html#extensions).

template/src/events_extension.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use lambda_extension::{tracing, LambdaEvent, NextEvent, Error};
2+
3+
/// Process events from the Lambda extension API.
4+
///
5+
/// This function is called when the Lambda extension receives an event from the Lambda API.
6+
pub(crate) async fn events_processor(event: LambdaEvent) -> Result<(), Error> {
7+
match event.next {
8+
NextEvent::Shutdown(event) => {
9+
tracing::info!(event_type = "shutdown", ?event, "shutting down function");
10+
}
11+
NextEvent::Invoke(event) => {
12+
tracing::info!(event_type = "invoke", ?event, "invoking function");
13+
}
14+
}
15+
Ok(())
16+
}

0 commit comments

Comments
 (0)