Skip to content

Commit 25d6bde

Browse files
author
Ryan Miville
committed
eventbridge example
1 parent 61b297d commit 25d6bde

14 files changed

+287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
pull_request:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: erlef/setup-beam@v1
16+
with:
17+
otp-version: "26.0.2"
18+
gleam-version: "1.5.1"
19+
rebar3-version: "3"
20+
# elixir-version: "1.15.4"
21+
- run: gleam deps download
22+
- run: gleam test
23+
- run: gleam format --check src test

examples/eventbridge/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
node_modules
3+
*.beam
4+
*.ez
5+
/build
6+
erl_crash.dump
7+
8+
# sst
9+
.sst

examples/eventbridge/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# eventbridge
2+
3+
[![Package Version](https://img.shields.io/hexpm/v/eventbridge)](https://hex.pm/packages/eventbridge)
4+
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/eventbridge/)
5+
6+
```sh
7+
gleam add eventbridge@1
8+
```
9+
```gleam
10+
import eventbridge
11+
12+
pub fn main() {
13+
// TODO: An example of the project in use
14+
}
15+
```
16+
17+
Further documentation can be found at <https://hexdocs.pm/eventbridge>.
18+
19+
## Development
20+
21+
```sh
22+
gleam run # Run the project
23+
gleam test # Run the tests
24+
```

examples/eventbridge/bun.lockb

9.1 KB
Binary file not shown.

examples/eventbridge/gleam.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name = "eventbridge"
2+
version = "1.0.0"
3+
target = "javascript"
4+
5+
# Fill out these fields if you intend to generate HTML documentation or publish
6+
# your project to the Hex package manager.
7+
#
8+
# description = ""
9+
# licences = ["Apache-2.0"]
10+
# repository = { type = "github", user = "", repo = "" }
11+
# links = [{ title = "Website", href = "" }]
12+
#
13+
# For a full reference of all the available options, you can have a look at
14+
# https://gleam.run/writing-gleam/gleam-toml/.
15+
16+
[dependencies]
17+
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
18+
glambda = { path = "../.." }
19+
gleam_http = ">= 3.7.0 and < 4.0.0"
20+
gleam_javascript = ">= 0.13.0 and < 1.0.0"
21+
22+
23+
[dev-dependencies]
24+
gleeunit = ">= 1.0.0 and < 2.0.0"

examples/eventbridge/manifest.toml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "glambda", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_javascript", "gleam_stdlib"], source = "local", path = "../.." },
6+
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
7+
{ name = "gleam_javascript", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "F98328FCF573DA6F3A35D7F6CB3F9FF19FD5224CCBA9151FCBEAA0B983AF2F58" },
8+
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
9+
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
10+
]
11+
12+
[requirements]
13+
glambda = { path = "../.." }
14+
gleam_http = { version = ">= 3.7.0 and < 4.0.0" }
15+
gleam_javascript = { version = ">= 0.13.0 and < 1.0.0" }
16+
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
17+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }

examples/eventbridge/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "eventbridge",
3+
"type": "module",
4+
"scripts": {
5+
"dev": "gleam build && sst dev"
6+
},
7+
"devDependencies": {
8+
"@types/aws-lambda": "8.10.145",
9+
"@types/bun": "latest"
10+
},
11+
"peerDependencies": {
12+
"typescript": "^5.0.0"
13+
},
14+
"dependencies": {
15+
"sst": "3.2.48",
16+
"valibot": "^0.42.1",
17+
"zod": "^3.23.8"
18+
}
19+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import glambda.{type Context, type EventBridgeEvent}
2+
import gleam/io
3+
import gleam/javascript/promise.{type Promise}
4+
5+
pub fn handle_request(event: EventBridgeEvent, _ctx: Context) -> Promise(Nil) {
6+
io.debug(event)
7+
promise.resolve(Nil)
8+
}
9+
10+
pub fn handler(event, ctx) {
11+
glambda.eventbridge_handler(handle_request)(event, ctx)
12+
}

examples/eventbridge/src/producer.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { event } from "sst/event";
2+
import { bus } from "sst/aws/bus";
3+
4+
import { z } from "zod";
5+
import { Resource } from "sst";
6+
import { ZodValidator } from "sst/event/validator";
7+
8+
const defineEvent = event.builder({
9+
validator: ZodValidator,
10+
});
11+
12+
export const MyEvent = defineEvent(
13+
"app.myevent",
14+
z.object({
15+
foo: z.string(),
16+
}),
17+
);
18+
19+
export async function handler() {
20+
await bus.publish(Resource.Bus, MyEvent, { foo: "hello" });
21+
return {
22+
statusCode: 200,
23+
};
24+
}

examples/eventbridge/sst-env.d.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* This file is auto-generated by SST. Do not edit. */
2+
/* tslint:disable */
3+
/* eslint-disable */
4+
/* deno-fmt-ignore-file */
5+
import "sst"
6+
export {}
7+
declare module "sst" {
8+
export interface Resource {
9+
"Bus": {
10+
"arn": string
11+
"name": string
12+
"type": "sst.aws.Bus"
13+
}
14+
"Fn": {
15+
"name": string
16+
"type": "sst.aws.Function"
17+
"url": string
18+
}
19+
}
20+
}

examples/eventbridge/sst.config.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/// <reference path="./.sst/platform/config.d.ts" />
2+
3+
export default $config({
4+
app(input) {
5+
return {
6+
name: "eventbridge",
7+
removal: input?.stage === "production" ? "retain" : "remove",
8+
home: "aws",
9+
providers: {
10+
aws: {
11+
region: "us-east-1",
12+
profile: "personal",
13+
},
14+
},
15+
};
16+
},
17+
async run() {
18+
const bus = new sst.aws.Bus("Bus");
19+
20+
const fn = new sst.aws.Function("Fn", {
21+
handler: "build/dev/javascript/eventbridge/producer.handler",
22+
url: true,
23+
link: [bus],
24+
});
25+
bus.subscribe("build/dev/javascript/eventbridge/eventbridge.handler");
26+
27+
return {
28+
url: fn.url,
29+
};
30+
},
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import gleeunit
2+
3+
pub fn main() {
4+
gleeunit.main()
5+
}

examples/eventbridge/tsconfig.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
// Enable latest features
4+
"lib": ["ESNext", "DOM"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
22+
// Some stricter flags (disabled by default)
23+
"noUnusedLocals": false,
24+
"noUnusedParameters": false,
25+
"noPropertyAccessFromIndexSignature": false
26+
}
27+
}

src/glambda.gleam

+52
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,58 @@ pub type EventBridgeEvent {
193193
)
194194
}
195195

196+
// --- SQS --------------------------------------------------------------------
197+
198+
pub type SqsEvent {
199+
SqsEvent(records: List(SqsRecord))
200+
}
201+
202+
pub type SqsRecord {
203+
SqsRecord(
204+
message_id: String,
205+
receipt_handle: String,
206+
body: String,
207+
attributes: SqsRecordAttributes,
208+
message_attributes: Dict(String, SqsMessageAttribute),
209+
md5_of_body: String,
210+
event_source: String,
211+
event_source_arn: String,
212+
aws_region: String,
213+
)
214+
}
215+
216+
pub type SqsRecordAttributes {
217+
SqsRecordAttributes(
218+
aws_trace_header: Option(String),
219+
approximate_receive_count: String,
220+
sent_timestamp: String,
221+
sender_id: String,
222+
approximate_first_receive_timestamp: String,
223+
sequence_number: Option(String),
224+
message_group_id: Option(String),
225+
message_deduplication_id: Option(String),
226+
dead_letter_queue_source_arn: Option(String),
227+
)
228+
}
229+
230+
pub type SqsMessageAttribute {
231+
SqsMessageAttribute(
232+
string_value: Option(String),
233+
binary_value: Option(String),
234+
string_list_values: Option(List(String)),
235+
binary_list_values: Option(List(String)),
236+
data_type: String,
237+
)
238+
}
239+
240+
pub type SqsBatchResponse {
241+
SqsBatchResponse(batch_item_failures: List(SqsBatchItemFailure))
242+
}
243+
244+
pub type SqsBatchItemFailure {
245+
SqsBatchItemFailure(item_identifier: String)
246+
}
247+
196248
// --- Adapters ---------------------------------------------------------------
197249

198250
pub fn http_handler(

0 commit comments

Comments
 (0)