
Run a job on an AWS Lambda MicroVM from AWS Step Functions, get its result with a task token callback, and always terminate the MicroVM.
RunMicrovm through the AWS SDK integration and passes the execution input as the job in the run hook payload, with the execution ARN as the idempotency token. The next step stores its task token in an Amazon DynamoDB table under the MicroVM ID and waits with .waitForTaskToken. The worker reads the token, sends SendTaskHeartbeat every 20 seconds, and finishes with SendTaskSuccess or SendTaskFailure.TerminateMicrovm on every path: success, a reported failure, missed heartbeats or the maximum duration. The maximum duration is also set on the MicroVM, so the platform stops it if someone stops the execution by hand. Replace the example job, which counts prime numbers, with your own batch job, data processing step or agent run.git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-microvms-sfn-callback/python/sam1. Set your Region and a name for a new Amazon S3 bucket that holds the worker code: export AWS_REGION=us-east-1 and export ARTIFACT_BUCKET=sfn-microvms-callback-artifacts-$(aws sts get-caller-identity --query Account --output text)-$AWS_REGION2. Create the bucket, then zip and upload the worker: aws s3 mb s3://$ARTIFACT_BUCKET --region $AWS_REGION, (cd src/worker && zip -j ../../worker.zip Dockerfile app.py), then aws s3 cp worker.zip s3://$ARTIFACT_BUCKET/worker.zip --region $AWS_REGION3. Deploy the stack and enter the artifact bucket name for ArtifactBucket when prompted: sam deploy --guidedsam delete --stack-name STACK_NAME --region $AWS_REGION. Enter y at each confirmation prompt.aws s3 rb s3://$ARTIFACT_BUCKET --force --region $AWS_REGION, then rm worker.zip.aws cloudformation list-stacks --region $AWS_REGION --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"