1

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

4

1 回答 1

0

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.

于 2022-01-25T00:47:00.730 回答