Shell 将脚本作为用户数据传递到AWS EC2 spot实例

Shell 将脚本作为用户数据传递到AWS EC2 spot实例,shell,amazon-web-services,amazon-ec2,aws-cli,Shell,Amazon Web Services,Amazon Ec2,Aws Cli,我试图使用aws cli创建一个新的spot请求,但如果我将shell脚本作为用户数据传递给spot请求,则会出现错误 我试过这个 aws ec2 request-spot-instances --spot-price "0.04" --instance-count 1 --launch-specification "{\"UserData\": \"/srv/user-data.sh\",\"InstanceType\": \"m1.small\"}" 这给了我一个错误 In

我试图使用aws cli创建一个新的spot请求,但如果我将shell脚本作为用户数据传递给spot请求,则会出现错误

我试过这个

    aws ec2 request-spot-instances --spot-price "0.04" --instance-count 1 --launch-specification "{\"UserData\": \"/srv/user-data.sh\",\"InstanceType\": \"m1.small\"}"
这给了我一个错误

    Invalid BASE64 encoding of user data (400 response code)


我如何用名称和值标记我的spot请求?确切地说,aws希望为UserData值传递一个base64编码的字符串。我不知道为什么工具不能帮你

因此,不是字符串:

  /srv/user-data.sh
使用base64编码版本的字符串(使用在线编码器,我得到以下结果):

我猜最终的json应该是这样的:

   '{"UserData": "L3Nydi91c2VyLWRhdGEuc2g=","InstanceType": "m1.small"}'
创建标记非常简单。以下是指向“aws”CLI命令文档的链接:

您需要确定资源AMI id:

  aws ec2 create-tags --resources ami-78a54011 --tags Key=Name,Value=myname

是的,我是这样工作的

     test="$(cat /srv/user-data.sh | base64 -w 0)"

     aws ec2 request-spot-instances \"UserData\": \"$test\",\"InstanceType\": \"m1.small\"}"
这个问题主要是用计算机解决的

   -w, --wrap=COLS
          wrap encoded lines after COLS character (default 76).  Use 0 to disable line wrapping
用于标记aws实例

  aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instancesName

test=“$(cat/srv/user data.sh|base64)”aws ec2请求现货实例--现货价格“0.04”--实例计数1--启动规范“{\”UserData\”:\“$test\”,\“InstanceType\”:\“m1.small\”,但它在解析参数“--启动规范”时给我一个错误:无效的JSON:第1行第133列的无效控制字符(字符133@user310685更好的是,现在您只需要找出json无效的原因。回显创建的stringError解析参数“--launch specification”:无效的json:第1行第133列(char 133)处的无效控制字符json接收:{“UserData”:“iyevymlul2jhc2gkzwnobyaiagggaxqgd29ya3mgbw9zzxmiid4gl3nydi9tb3nlc25ld2luc3rh bmNlc3VzZXItZGF0YXdvcmtzLnR4dAo=”,“InstanceType:“m1.small”}@user310685在我看来像是完美的json。试着对上面的字符串进行硬编码。硬编码它可以工作..但我不能在传递它时将它放入变量中吗
  aws ec2 create-tags --resources $instanceId --tags Key=Name,Value=$instancesName