vendredi 2 octobre 2015

AWS EC2 instance: Multiline bash script passed as User Data from Windows Powershell ends up as one line and failing with "File not found" error

I have a simple bash script I'm trying to pass to an AWS EC2 ubuntu instance via a Powershell script using the AWS Library.

#!/bin/bash

apt-get update
apt-get upgrade

Heres the powershell script that encodes the file contents in base64 and calls the cmdlet that starts the 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 is a snippet from the cloud init log:

Cloud-init v. 0.7.5 running 'modules:final' at Fri, 02 Oct 2015 21:05:24 +0000. Up 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 to 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' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed

Here's a snippet of the loaded user data script on the ubuntu instance at /var/lib/cloud/instance/scripts/part-001:

#!/bin/bash  apt-get update apt-get upgrade

I have tried converting the windows file to linux using 010 Editor and Cygwin. I've tried replacing the CRLF bytes with LF bytes. The result is the same: the entire bash script is condensed to 1 line, all line breaks are removed, and bash script load on initial boot.




Aucun commentaire:

Enregistrer un commentaire