powershell - Bash script passed to AWS EC2 Instance as User Data file fails to load on initial boot -
i have simple bash script i'm trying pass aws ec2 ubuntu instance via powershell script using aws library.
#!/bin/bash apt-get update apt-get upgrade
heres powershell script encodes file contents in base64 , calls cmdlet starts ec2 instance:
$filecontent = get-content $userdatatarget $filecontentbytes = [system.text.encoding]::utf8.getbytes($filecontent) $filecontentencoded = [system.convert]::tobase64string($filecontentbytes) $masterinstance = new-ec2instance -imageid ami-4c7a3924 -mincount 1 -maxcount 1 -keyname awskey -securitygroups $securitygroup -instancetype "t1.micro" -userdata $filecontentencoded
this snippet cloud init log:
cloud-init v. 0.7.5 running 'modules:final' @ fri, 02 oct 2015 21:05:24 +0000. 33.88 seconds. /bin/bash: apt-get update apt-get upgrade: no such file or directory 2015-10-02 21:05:24,294 - util.py[warning]: failed running /var/lib/cloud/instance/scripts/part-001 [127] 2015-10-02 21:05:24,298 - cc_scripts_user.py[warning]: failed run module scripts-user (scripts in /var/lib/cloud/instance/scripts) 2015-10-02 21:05:24,298 - util.py[warning]: running scripts-user (<module 'cloudinit.config.cc_scripts_user' '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
here's snippet of loaded user data script on ubuntu instance @ /var/lib/cloud/instance/scripts/part-001:
#!/bin/bash apt-get update apt-get upgrade
i have tried converting windows file linux using 010 editor , cygwin. i've tried replacing crlf bytes lf bytes. result same: entire bash script condensed 1 line, line breaks removed, , user data script fails load on initial boot.
update: i've included both code snippets i've used line break conversion. both vetted peer sources (so). reason, script still shows in linux instance without line break characters.
snippet 1
function convertto-linuxlineendings($path) { $oldbytes = [io.file]::readallbytes($path) if (!$oldbytes.length) { return; } [byte[]]$newbytes = @() [byte[]]::resize([ref]$newbytes, $oldbytes.length) $newlength = 0 ($i = 0; $i -lt $oldbytes.length - 1; $i++) { if (($oldbytes[$i] -eq [byte][char]"`r") -and ($oldbytes[$i + 1] -eq [byte][char]"`n")) { continue; } $newbytes[$newlength++] = $oldbytes[$i] } $newbytes[$newlength++] = $oldbytes[$oldbytes.length - 1] [byte[]]::resize([ref]$newbytes, $newlength) [io.file]::writeallbytes($path, $newbytes) } convertto-linuxlineendings($userdatatarget)
snippet 2
try{ # contents , replace line breaks u+000a $contents = [io.file]::readalltext($userdatasource) -replace "`r`n", "`n" # create utf-8 encoding without signature $utf8 = new-object system.text.utf8encoding $false # write text [io.file]::writealltext($userdatatarget, $contents, $utf8) } catch [exception] { echo $_.exception|format-list -force }
i can't give answer on powershell code needs like, in case helps can tell output powershell script needs like, need confirm you're ending in userdata when goes in template:
"userdata" : { "fn::base64" : { "fn::join" : ["", [ "#!/bin/bash\n\n", "apt-get update\n", "apt-get upgrade\n" ]]}}
Comments
Post a Comment