简体   繁体   English

AWS EC2运行实例:base64编码的用户数据blob被忽略

[英]aws ec2 run-instances: base64 encoded user-data blob is ignored

My base64 encoded user-data is ignored while running aws ec2 run-instances command. 运行aws ec2 run-instances命令时,将忽略我的base64编码的用户数据。

Here is my user data: 这是我的用户数据:

$ cat user-data.sh 
#!/bin/bash
cat >> /var/tmp/user-data-testing <<EOF
this is test line added at $(date)
EOF

here is base64 blob of above script: 这是上面脚本的base64 blob:

IyEvYmluL2Jhc2gKY2F0ID4+IC92YXIvdG1wL3VzZXItZGF0YS10ZXN0aW5nIDw8RU9GCnRoaXMgaXMgdGVzdCBsaW5lIGFkZGVkIGF0ICQoZGF0ZSkKRU9GCg==

Now, My below command does read the user-data fine: 现在,我的以下命令确实读取了用户数据:

aws ec2 run-instances --image-id ami-8635a9b6 --instance-type t1.micro --placement AvailabilityZone=us-west-2a --security-groups quicklaunch-1 --key-name devops --user-data file://user-data.sh

I do see that file /var/tmp/user-data-testing is created. 我确实看到已创建文件/var/tmp/user-data-testing

However, when I try to pass-in user-data as a base64 encoded blob as below, then it gets ignored: 但是,当我尝试将用户数据作为base64编码的blob传入时,如下所示,它将被忽略:

aws ec2 run-instances --image-id ami-8635a9b6 --instance-type t1.micro --placement AvailabilityZone=us-west-2a --security-groups quicklaunch-1 --key-name devops --user-data IyEvYmluL2Jhc2gKY2F0ID4+IC92YXIvdG1wL3VzZXItZGF0YS10ZXN0aW5nIDw8RU9GCnRoaXMgaXMgdGVzdCBsaW5lIGFkZGVkIGF0ICQoZGF0ZSkKRU9GCg==

Now, I do not see the file /var/tmp/user-data-testing created. 现在,我看不到已创建文件/var/tmp/user-data-testing

Also, I know that my base64 blob is healthy as I can decode it fine: 另外,我知道我的base64 blob很健康,因为我可以对其进行解码:

$ base64 --decode <<< IyEvYmluL2Jhc2gKY2F0ID4+IC92YXIvdG1wL3VzZXItZGF0YS10ZXN0aW5nIDw8RU9GCnRoaXMgaXMgdGVzdCBsaW5lIGFkZGVkIGF0ICQoZGF0ZSkKRU9GCg==
#!/bin/bash
cat >> /var/tmp/user-data-testing <<EOF
this is test line added at $(date)
EOF

However, I do see that instance metadata has my user data in base64 format: 但是,我确实看到实例元数据具有base64格式的用户数据:

$ curl -L http://169.254.169.254/latest/user-data/
IyEvYmluL2Jhc2gKY2F0ID4+IC92YXIvdG1wL3VzZXItZGF0YS10ZXN0aW5nIDw8RU9GCnRoaXMgaXMgdGVzdCBsaW5lIGFkZGVkIGF0ICQoZGF0ZSkKRU9GCg==

So, what am I doing wrong in using base64 user-data blob? 那么,在使用base64用户数据blob时我做错了什么?

My instance meta-data is aware of it but seems like it is not really being executed (or decoded and executed) at the time of instance launch. 我的实例元数据已经知道了,但似乎在实例启动时并没有真正执行(或解码和执行)。

UPDATE: 更新:

If I pass the same base64 blob via AWS Console while launching the instance, It works. 如果在启动实例时通过AWS控制台传递相同的base64 blob,则它可以工作。 So seems like something is wrong in the way I am using it along with AWS-CLI . 因此,似乎我将其与AWS-CLI一起使用时出现了问题。

UPDATE: 更新:

I just tried the same base64 blob with my ruby code as below and it worked as well: 我只是用下面的红宝石代码尝试了相同的base64 blob,但效果也很好:

ec2 = Aws::EC2.new
resp = ec2.run_instances(
    min_count: 1,
    max_count: 1,
    image_id: 'ami-8635a9b6',
    instance_type: 't1.micro',
    placement: {
      availability_zone: 'us-west-2a'
    },
    security_groups: ['quicklaunch-1'],
    key_name: 'devops',
    user_data: 'IyEvYmluL2Jhc2gKY2F0ID4+IC92YXIvdG1wL3VzZXItZGF0YS10ZXN0aW5nIDw8RU9GCnRoaXMgaXMgdGVzdCBsaW5lIGFkZGVkIGF0ICQoZGF0ZSkKRU9GCg=='
)

So, then WTF is wrong my implementation of AWS-CLI ? 那么,那么WTF在我的AWS-CLI实施中是错误的吗?

It seems like awscli does the base64 encoding for you, so you should pass unencoded text to --user-data. 似乎awscli为您执行了base64编码,因此您应该将未编码的文本传递给--user-data。

Apparently the documentation is not very clear on this. 显然,文档对此并不十分清楚。 Check this link . 检查此链接

This syntax should then be: 该语法应为:

aws ec2 run-instances --image-id ami-8635a9b6 --user-data "echo TEST"

or 要么

aws ec2 run-instances --image-id ami-8635a9b6 --user-data file://path/to/file

Had the same issue, very frustrating to track down the problem, finally got it working. 遇到了同样的问题,很难找到问题所在,终于使它起作用了。 did not base64 encode did put script in file. 没有base64编码,没有将脚本放入文件中。

placing seems to be important worked for me only when --user-data file://path is placed at the end 仅当--user-data file:// path放在末尾时,放置对我来说似乎很重要

This format worked obviously change the some data to yours 这种格式的工作显然将一些数据更改为您的数据

aws ec2 run-instances --image-id amisomthing --count 1 --instance-type t1.micro --key-name keysomthing --security-group-ids somegroup --subnet-id somesubnetid --associate-public-ip-address --user-data file://someuserdata

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM