3

I am trying to create a new spot request using aws cli but it gives me an error if i pass a shell script as user-data to spot request

i tried this

    aws ec2 request-spot-instances --spot-price "0.04" --instance-count 1 --launch-specification "{\"UserData\": \"/srv/user-data.sh\",\"InstanceType\": \"m1.small\"}"

and this gives me an error

    Invalid BASE64 encoding of user data (400 response code)

and

how can i Tags my spot request with Name & Value

2 Answers 2

3

Exactly that, aws expects a base64 encoded string to be passed for the UserData value. Why the tools doesn't do this for you, I don't know.

So, instead of the string:

  /srv/user-data.sh

Use a base64 encoded version of the string (using an online encoder, I got the following):

 L3Nydi91c2VyLWRhdGEuc2g=

I'm guessing the final json it should look something like this:

   '{"UserData": "L3Nydi91c2VyLWRhdGEuc2g=","InstanceType": "m1.small"}'

Creating tags is pretty straight forward. Here's a link to the 'aws' CLI command documentation: http://docs.aws.amazon.com/cli/latest/reference/ec2/create-tags.html

You'll need to determine the resource AMI id:

  aws ec2 create-tags --resources ami-78a54011 --tags Key=Name,Value=myname
Sign up to request clarification or add additional context in comments.

8 Comments

test="$(cat /srv/user-data.sh| base64)" aws ec2 request-spot-instances --spot-price "0.04" --instance-count 1 --launch-specification "{\"UserData\": \"$test\",\"InstanceType\": \"m1.small\"}" but it gives me an error Error parsing parameter '--launch-specification': Invalid JSON: Invalid control character at: line 1 column 133 (char 133
@user310685 getting better, now you just need to figure why the json is invalid. Echo out the created string
Error parsing parameter '--launch-specification': Invalid JSON: Invalid control character at: line 1 column 133 (char 133) JSON received: {"UserData": "IyEvYmluL2Jhc2gKZWNobyAiaGkgaXQgd29ya3MgbW9zZXMiID4gL3Nydi9tb3Nlc25ld2luc3Rh bmNlc3VzZXItZGF0YXdvcmtzLnR4dAo=","InstanceType": "m1.small"}
@user310685 Looks like perfect json to me. Try hardcodeing that string above.
hardcoding it works.. but cant i take it into a variable as pass it
|
1

yes i got it to work like this

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

     aws ec2 request-spot-instances \"UserData\": \"$test\",\"InstanceType\": \"m1.small\"}"

the problem mainly solved with

   -w, --wrap=COLS
          wrap encoded lines after COLS character (default 76).  Use 0 to disable line wrapping

and for tagging the aws instances

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.