Skip to content

Instantly share code, notes, and snippets.

View mikhailshilkov's full-sized avatar

Mikhail Shilkov mikhailshilkov

View GitHub Profile
// Create a Role giving our Lambda access.
let policy: aws.iam.PolicyDocument = { /* Redacted for brevity */ };
let role = new aws.iam.Role("lambda-role", {
assumeRolePolicy: JSON.stringify(policy),
});
let fullAccess = new aws.iam.RolePolicyAttachment("lambda-access", {
role: role,
policyArn: aws.iam.AWSLambdaFullAccess,
});
import * as aws from "@pulumi/aws";
// A DynamoDB table with a single primary key
let counterTable = new aws.dynamodb.Table("urls", {
name: "urls",
attributes: [
{ name: "name", type: "S" },
],
hashKey: "name",
readCapacity: 1,
let workflow wishlist = async {
let! matches =
wishlist.Wishes
|> List.map findMatchingGift
|> Async.Parallel
let gift = pickGift (List.concat matches)
let reservation = { Kid = wishlist.Kid; Product = gift }
do! reserve reservation
[<FunctionName("FindMatchingGift")>]
let FindMatchingGift([<ActivityTrigger>] wish) =
Activity.run findMatchingGiftActivity wish
[<FunctionName("PickGift")>]
let PickGift([<ActivityTrigger>] matches) =
Activity.run pickGiftActivity matches
[<FunctionName("Reserve")>]
let Reserve([<ActivityTrigger>] wish) =
let workflow wishlist = orchestrator {
let! matches =
wishlist.Wishes
|> List.map (Activity.call findMatchingGiftActivity)
|> Activity.all
let! gift = Activity.call pickGiftActivity (List.concat matches)
let reservation = { Kid = wishlist.Kid; Product = gift }
do! Activity.call reserveActivity reservation
let findMatchingGiftActivity = Activity.defineAsync "FindMatchingGift" findMatchingGift
let pickGiftActivity = Activity.define "PickGift" pickGift
let reserveActivity = Activity.defineAsync "Reserve" reserve
// 1. Simple sequencing of activities
let Run([<OrchestrationTrigger>] context: DurableOrchestrationContext) = task {
let! hello1 = context.CallActivityAsync<string>("E1_SayHello", "Tokyo")
let! hello2 = context.CallActivityAsync<string>("E1_SayHello", "Seattle")
let! hello3 = context.CallActivityAsync<string>("E1_SayHello", "London")
return [hello1; hello2; hello3]
}
// 2. Parallel calls snippet
let tasks = Array.map (fun f -> context.CallActivityAsync<int64>("E2_CopyFileToBlob", f)) files
// WishList -> Async<Reservation>
let workflow (wishlist: WishList) = async {
// 1. Find matches for each wish
let! matches =
wishlist.Wishes
|> List.map findMatchingGift
|> Async.Parallel
// 2. Pick one product from the combined list of matches
// Reservation -> Async<unit>
let reserve (reservation: Reservation) = async {
// Call Santa's Archive of Products
}
// Match list -> Product
let pickGift (candidates: Match list) =
candidates
|> List.sortByDescending (fun x -> x.Confidence)
|> List.head
|> (fun x -> x.Product)