1
0
Fork 0
mirror of https://github.com/imjasonh/terraform-playground synced 2026-07-18 15:08:24 +00:00

demo using ESM with KV and R2

Signed-off-by: Jason Hall <jason@chainguard.dev>
This commit is contained in:
Jason Hall 2023-08-12 10:40:58 -04:00
parent 1e82289e81
commit f5ae8b5cb2
Failed to extract signature
2 changed files with 28 additions and 6 deletions

View file

@ -20,12 +20,24 @@ resource "cloudflare_worker_script" "script" {
name = var.name
content = file("script.js")
module = true
kv_namespace_binding {
name = "KV"
namespace_id = cloudflare_workers_kv_namespace.kv_ns.id
}
r2_bucket_binding {
name = "R2"
bucket_name = cloudflare_r2_bucket.bucket.name
}
}
resource "cloudflare_r2_bucket" "bucket" {
account_id = var.cloudflare_account_id
name = "${var.name}-bucket"
location = "ENAM"
}
/*
resource "cloudflare_worker_route" "route" {

View file

@ -1,6 +1,16 @@
addEventListener("fetch", (event) => {
console.log("Hello from the service worker!");
let value = KV.get("foo");
event.respondWith(new Response(value,
{ headers: { "content-type": "text/plain" } }));
});
export default {
async fetch(request, env, ctx) {
let value = await env.KV.get("foo");
await env.KV.put("foo", value + "r");
let obj = await env.R2.get("foo");
if (!obj) {
obj = "ba";
} else {
obj = await obj.text();
}
await env.R2.put("foo", obj + "r");
return new Response(`${value} / ${obj}`);
},
};