[{"data":1,"prerenderedAt":88},["ShallowReactive",2],{"pattern-lambda-microvms-sfn-callback-python-sam":3,"pattern-group-lambda-microvms-sfn-callback-python-sam":6},{"id":4,"title":5,"architectureURL":6,"cleanup":7,"contributors":13,"deploy":15,"description":20,"extension":21,"framework":22,"gitHub":23,"highlight":6,"introBox":27,"language":33,"level":34,"meta":35,"patternArch":36,"patternGroup":66,"patternGroupLabel":6,"resources":67,"stem":82,"testing":83,"videoId":6,"__hash__":87},"patterns\u002Fpatterns\u002Flambda-microvms-sfn-callback-python-sam.json","AWS Step Functions to AWS Lambda MicroVMs with a task token callback",null,{"headline":8,"text":9},"Cleanup",[10,11,12],"1. Delete the stack: \u003Ccode>sam delete --stack-name STACK_NAME --region $AWS_REGION\u003C\u002Fcode>. Enter \u003Ccode>y\u003C\u002Fcode> at each confirmation prompt.","2. Delete the artifact bucket and the local zip: \u003Ccode>aws s3 rb s3:\u002F\u002F$ARTIFACT_BUCKET --force --region $AWS_REGION\u003C\u002Fcode>, then \u003Ccode>rm worker.zip\u003C\u002Fcode>.","3. Confirm the stack has been deleted: \u003Ccode>aws cloudformation list-stacks --region $AWS_REGION --query \"StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus\"\u003C\u002Fcode>",[14],"content\u002Fcontributors\u002Fmanish-s.json",{"text":16},[17,18,19],"1. Set your Region and a name for a new Amazon S3 bucket that holds the worker code: \u003Ccode>export AWS_REGION=us-east-1\u003C\u002Fcode> and \u003Ccode>export ARTIFACT_BUCKET=sfn-microvms-callback-artifacts-$(aws sts get-caller-identity --query Account --output text)-$AWS_REGION\u003C\u002Fcode>","2. Create the bucket, then zip and upload the worker: \u003Ccode>aws s3 mb s3:\u002F\u002F$ARTIFACT_BUCKET --region $AWS_REGION\u003C\u002Fcode>, \u003Ccode>(cd src\u002Fworker && zip -j ..\u002F..\u002Fworker.zip Dockerfile app.py)\u003C\u002Fcode>, then \u003Ccode>aws s3 cp worker.zip s3:\u002F\u002F$ARTIFACT_BUCKET\u002Fworker.zip --region $AWS_REGION\u003C\u002Fcode>","3. Deploy the stack and enter the artifact bucket name for \u003Ccode>ArtifactBucket\u003C\u002Fcode> when prompted: \u003Ccode>sam deploy --guided\u003C\u002Fcode>","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.","json","AWS SAM",{"template":24},{"patternPath":25,"templateFile":26},"lambda-microvms-sfn-callback\u002Fpython\u002Fsam","template.yaml",{"headline":28,"text":29},"How it works",[30,31,32],"AWS Lambda MicroVMs run long or stateful work, for up to eight hours, in a Firecracker-isolated environment built from your own image. A MicroVM isn't invoked and doesn't return a response: it runs, and bills, until it's terminated or reaches its maximum duration. This pattern uses an AWS Step Functions workflow to get the job's result back and to make sure the MicroVM always stops, with no AWS Lambda function in between.","The workflow starts the MicroVM with `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`.","The workflow calls `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.","Python","300",{},{"icon1":37,"icon2":42,"icon3":45,"icon4":50,"line1":54,"line2":58,"line3":61,"line4":64},{"x":38,"y":39,"service":40,"label":41},12,45,"icon-internet","Client",{"x":39,"y":39,"service":43,"label":44},"sfn","AWS Step\nFunctions",{"x":46,"y":47,"service":48,"label":49},82,20,"lambda-microvms","AWS Lambda MicroVMs",{"x":46,"y":51,"service":52,"label":53},68,"dynamodb","Amazon DynamoDB",{"from":55,"to":56,"label":57},"icon1","icon2","Start",{"from":56,"to":59,"label":60},"icon3","Run, terminate",{"from":56,"to":62,"label":63},"icon4","Task token",{"from":59,"to":62,"label":65},"Get token","lambda-microvms-sfn-callback",{"headline":68,"bullets":69},"Additional resources",[70,73,76,79],{"text":71,"link":72},"AWS SDK service integrations in Step Functions","https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fsupported-services-awssdk.html",{"text":74,"link":75},"Wait for a callback with a task token","https:\u002F\u002Fdocs.aws.amazon.com\u002Fstep-functions\u002Flatest\u002Fdg\u002Fconnect-to-resource.html#connect-wait-token",{"text":77,"link":78},"AWS::Lambda::MicrovmImage CloudFormation resource","https:\u002F\u002Fdocs.aws.amazon.com\u002FAWSCloudFormation\u002Flatest\u002FTemplateReference\u002Faws-resource-lambda-microvmimage.html",{"text":80,"link":81},"AWS CLI reference for Lambda MicroVMs RunMicrovm","https:\u002F\u002Fawscli.amazonaws.com\u002Fv2\u002Fdocumentation\u002Fapi\u002Flatest\u002Freference\u002Flambda-microvms\u002Frun-microvm.html","patterns\u002Flambda-microvms-sfn-callback-python-sam",{"headline":84,"text":85},"Testing",[86],"Start executions with a successful job, a failing job and a hung job, then confirm each MicroVM is terminated. See the README for detailed instructions.","ru4BzUdhb-8FtdM0Y08DO3x5NFyKA5uHkXoQAIBFznw",1795189153522]