Search code examples
jsonamazon-web-servicesamazon-s3aws-step-functionsaws-ssm

Is there a way to use AWS Step Functions Input to assembly command string on System Manager block?


I am creating a Step Function State machine to everytime an instance starts it copy a file from S3 to an specific folder inside this instance. The origin folder inside S3 bucket has a folder named with this instance ID. The instance ID I am passing as input for the System manager block, but I need to use it to create the command string that will be performed inside the EC2.

For example:

My input is: $.detail.instance-id (lets assume the following ID i-11223344556677889)

The Systems Manager API parameters are:

  "CloudWatchOutputConfig": {
    "CloudWatchLogGroupName": "myloggroup",
    "CloudWatchOutputEnabled": true
  },
  "DocumentName": "AWS-RunShellScript",
  "DocumentVersion": "$DEFAULT",
  "InstanceIds.$": "States.Array($)",
  "MaxConcurrency": "50",
  "MaxErrors": "10",
  "Parameters": {
    "commands": [
      {
        "runuser -l ec2-user -c \"aws s3 cp s3://my-bucket/**MY_INSTANCEID**/myfile.xyz /home/ec2-user/myfolder/myfile.xyz\""
      }
  },
  "TimeoutSeconds": 6000
}```

Summing up, I want to turn the line with the command replacing the MY_INSTANCEID by my input $.detail.instance-id, and perform the following command:

"runuser -l ec2-user -c "aws s3 cp s3://my-bucket/i-11223344556677889/myfile.xyz /home/ec2-user/myfolder/myfile.xyz""

Is there a way? I already tried to use the Fn::join withou success.

Thank you in advance, kind regards,

Winner


Solution

  • It was necessary to use State.Format inside the State.Array so the it worked, and State.Format inside the State.Array cannot have quotes:

      "CloudWatchOutputConfig": {
        "CloudWatchLogGroupName": "myloggroup",
        "CloudWatchOutputEnabled": true
      },
      "DocumentName": "AWS-RunShellScript",
      "DocumentVersion": "$DEFAULT",
      "InstanceIds.$": "States.Array($)",
      "MaxConcurrency": "50",
      "MaxErrors": "10",
      "Parameters": {
        "commands.$": "States.Array(States.Format('runuser -l ec2-user -c \"aws s3 cp s3://my-bucket/**MY_INSTANCEID**/myfile.xyz /home/ec2-user/myfolder/myfile.xyz\"', $))"
      },
      "TimeoutSeconds": 6000
    }```
    

    Was also necessary to use .$ after command.