Skip to content

Commit b3cf9a9

Browse files
committed
Document standalone activities with serverless workers
1 parent d38d63a commit b3cf9a9

1 file changed

Lines changed: 148 additions & 0 deletions

File tree

docs/develop/go/activities/standalone-activities.mdx

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This page covers the following:
4343
- [List Standalone Activities](#list-activities)
4444
- [Count Standalone Activities](#count-activities)
4545
- [Run Standalone Activities with Temporal Cloud](#run-standalone-activities-temporal-cloud)
46+
- [Move to production with Serverless Workers](#move-to-production-serverless-workers)
4647

4748
:::note
4849

@@ -437,3 +438,150 @@ export TEMPORAL_API_KEY=<your-api-key>
437438
```
438439

439440
Then run the Worker and starter code as shown in the earlier sections.
441+
442+
## Move to production with Serverless Workers {/* #move-to-production-serverless-workers */}
443+
444+
After you have run the Standalone Activity locally and configured its Temporal Cloud connection, you can deploy its
445+
Worker as a [Serverless Worker](/serverless-workers). Serverless Workers run on ephemeral, on-demand compute rather than
446+
as long-lived processes.
447+
448+
:::note
449+
450+
Serverless Workers are a separate Pre-release feature. To request access, create a
451+
[support ticket](/cloud/support#support-ticket) or contact your account team.
452+
453+
:::
454+
455+
### AWS Lambda {/* #serverless-worker-aws-lambda */}
456+
457+
AWS Lambda is the first Serverless Worker provider example. When a Standalone Activity Task is added to a Task Queue with
458+
no active poller, Temporal invokes the Lambda function configured for that Task Queue. The Worker connects, processes
459+
available Tasks, and shuts down before the Lambda invocation deadline.
460+
461+
The
462+
[Standalone Activity Lambda sample](https://github.com/temporalio/samples-go/tree/main/standalone-activity/lambda-worker)
463+
contains an Activity, an activity-only Lambda Worker, and a starter program:
464+
465+
```text
466+
standalone-activity/lambda-worker/
467+
├── activity.go
468+
├── worker/
469+
│ └── main.go
470+
└── starter/
471+
└── main.go
472+
```
473+
474+
#### Register the Activity with a Lambda Worker {/* #register-activity-lambda-worker */}
475+
476+
Use the Go SDK's `lambdaworker` package to run the Worker inside Lambda. Set the Worker Deployment name, Build ID, and
477+
Task Queue, then register the Activity.
478+
479+
<!--SNIPSTART samples-go-standalone-activity-lambda-worker-->
480+
[standalone-activity/lambda-worker/worker/main.go](https://github.com/temporalio/samples-go/blob/main/standalone-activity/lambda-worker/worker/main.go)
481+
```go
482+
package main
483+
484+
import (
485+
lambdaworker "go.temporal.io/sdk/contrib/aws/lambdaworker"
486+
"go.temporal.io/sdk/worker"
487+
488+
lambdaactivity "github.com/temporalio/samples-go/standalone-activity/lambda-worker"
489+
)
490+
491+
func main() {
492+
lambdaworker.RunWorker(worker.WorkerDeploymentVersion{
493+
DeploymentName: "standalone-activity-lambda",
494+
BuildID: "build-1",
495+
}, func(options *lambdaworker.Options) error {
496+
options.TaskQueue = "standalone-activity-lambda"
497+
options.RegisterActivity(lambdaactivity.Greet)
498+
return nil
499+
})
500+
}
501+
502+
```
503+
<!--SNIPEND-->
504+
505+
An activity-only Worker doesn't register a Workflow, so it doesn't need to set a Workflow
506+
[versioning behavior](/worker-versioning#versioning-behaviors). The `WorkerDeploymentVersion` is still required because
507+
Serverless Workers use Worker Deployment Versioning to route Tasks to the configured Lambda function.
508+
509+
#### Deploy the Worker to Lambda {/* #deploy-standalone-activity-lambda-worker */}
510+
511+
Follow [Deploy a Serverless Worker on AWS Lambda](/production-deployment/worker-deployments/serverless-workers/aws-lambda)
512+
to build and package the Worker, create the Lambda function, configure IAM, and create the Worker Deployment Version.
513+
514+
Run the guide's build commands from `standalone-activity/lambda-worker` so `./worker` resolves to the sample's Worker
515+
package. Use these values when you configure the Worker Deployment Version:
516+
517+
| Setting | Value |
518+
| --- | --- |
519+
| Worker Deployment name | `standalone-activity-lambda` |
520+
| Build ID | `build-1` |
521+
| Task Queue | `standalone-activity-lambda` |
522+
523+
The Worker Deployment name and Build ID must match the values passed to `lambdaworker.RunWorker`. After you create the
524+
version, [set it as current](/production-deployment/worker-deployments/serverless-workers/aws-lambda#set-current-version)
525+
so Temporal can route Tasks to it.
526+
527+
#### Execute the Standalone Activity {/* #execute-standalone-activity-lambda */}
528+
529+
Run the starter outside Lambda. It connects to Temporal and schedules the Standalone Activity on the same Task Queue as
530+
the Serverless Worker.
531+
532+
<!--SNIPSTART samples-go-standalone-activity-lambda-starter-->
533+
[standalone-activity/lambda-worker/starter/main.go](https://github.com/temporalio/samples-go/blob/main/standalone-activity/lambda-worker/starter/main.go)
534+
```go
535+
package main
536+
537+
import (
538+
"context"
539+
"log"
540+
"time"
541+
542+
"go.temporal.io/sdk/client"
543+
"go.temporal.io/sdk/contrib/envconfig"
544+
545+
lambdaactivity "github.com/temporalio/samples-go/standalone-activity/lambda-worker"
546+
)
547+
548+
func main() {
549+
temporalClient, err := client.Dial(envconfig.MustLoadDefaultClientOptions())
550+
if err != nil {
551+
log.Fatalln("Unable to create Temporal Client", err)
552+
}
553+
defer temporalClient.Close()
554+
555+
handle, err := temporalClient.ExecuteActivity(
556+
context.Background(),
557+
client.StartActivityOptions{
558+
ID: "standalone-activity-lambda-greeting",
559+
TaskQueue: "standalone-activity-lambda",
560+
ScheduleToCloseTimeout: time.Minute,
561+
},
562+
lambdaactivity.Greet,
563+
"Temporal",
564+
)
565+
if err != nil {
566+
log.Fatalln("Unable to execute Standalone Activity", err)
567+
}
568+
569+
var result string
570+
if err := handle.Get(context.Background(), &result); err != nil {
571+
log.Fatalln("Standalone Activity failed", err)
572+
}
573+
log.Println("Standalone Activity result:", result)
574+
}
575+
576+
```
577+
<!--SNIPEND-->
578+
579+
Configure the starter's Temporal Client connection using
580+
[environment configuration](/references/client-environment-configuration), then run it from the sample directory:
581+
582+
```bash
583+
go run ./starter
584+
```
585+
586+
When the starter schedules the Activity, Temporal invokes the Lambda function, and the Lambda Worker processes the
587+
Activity Task and returns the result.

0 commit comments

Comments
 (0)