admin管理员组

文章数量:1335361

How do I pass $$.Task.Token to an EC2 run-instances call from AWS Step Functions?

Should I pass it using "user-data" key in the json request?

It becomes more difficult since user-data key accepts ONLY base64 encoded string. The string $$.Task.Token is passed as it is and not its value.

Or is there any other way to pass the task token to an aws EC2 instance?

How do I pass $$.Task.Token to an EC2 run-instances call from AWS Step Functions?

Should I pass it using "user-data" key in the json request?

It becomes more difficult since user-data key accepts ONLY base64 encoded string. The string $$.Task.Token is passed as it is and not its value.

Or is there any other way to pass the task token to an aws EC2 instance?

Share Improve this question edited Dec 9, 2024 at 19:09 Rodrigue 3,6872 gold badges41 silver badges50 bronze badges asked Nov 20, 2024 at 4:39 ShanthiShanthi 6772 gold badges8 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There are always plenty of intrinsic functions for tasks like that. States.Base64Encode is your case. Here is the full code of the step function passing down TaskToken and waitingForCallback. The task token is passed along with the base64 string and will be initialized as an env variable TASK_TOKEN.

Don't fet to clean up the instance though.

{
  "Comment": "State Machine that launches EC2 with TASK_TOKEN as an environment variable",
  "StartAt": "PrepareAndLaunchEC2",
  "States": {
    "PrepareAndLaunchEC2": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:ec2:runInstances.waitForTaskToken",
      "Parameters": {
        "ImageId": "ami-0cdd6d7420844683b",
        "InstanceType": "t2.micro",
        "MinCount": 1,
        "MaxCount": 1,
        "UserData.$": "States.Base64Encode(States.Format('#!/bin/bash\n export TASK_TOKEN=\"{}\"\n# More commands here', $$.Task.Token))"
      },
      "End": true
    }
  }
}

本文标签: How do I pass the Task Token to an EC2 runinstances call from AWS Step FunctionsStack Overflow