3

I can launch an ec2 instance without user data:

aws ec2 request-spot-instances --spot-price .01 --instance-count 1 --launch-specification '{ "ImageId": "ami-3275ee5b", "KeyName": "key", "InstanceType": "t1.micro"}'

But when I try it with userdata I get the following error:

aws ec2 request-spot-instances --spot-price .01 --instance-count 1 --launch-specification '{ "ImageId": "ami-3275ee5b", "KeyName": "key", "UserData": {"Fn::Base64" : { "Fn::Join" : ["", ["#!/bin/bash\n","touch /tmp/userdata_sucess\n"]]}}, "InstanceType": "t1.micro"}'

Invalid value ('OrderedDict([(u'Fn::Base64', OrderedDict([(u'Fn::Join', [u'', [u'#!/bin/bash\n', u'touch /tmp/userdata_sucess\n']])]))])') for param string:UserData of type string

I was following the example here

6

When you request spot instance, Amazon require userdatа to be in base64 format. Example:

aws ec2 request-spot-instances \
--spot-price 0.01 \
--instance-count 2 \
--launch-specification \
    "{ \
        \"ImageId\":\"ami-a6926dce\", \
        \"InstanceType\":\"m3.medium\", \
        \"KeyName\":\"test-key\", \
        \"SecurityGroups\": [\"test-sg\"], \
        \"UserData\":\"`base64 userdata.sh`\" \
    }"

More info at: Request spot instances

Example is from: Small Tip: How to use AWS CLI to start Spot instances with UserData

1

Dimitrov's would have worked like this with the newline wrapping args -w 0 added into the JSON since then there is no newline in the userdata embedded value.

aws ec2 request-spot-instances \
--spot-price 0.01 \
--instance-count 2 \
--launch-specification \
    "{ \
        \"ImageId\":\"ami-a6926dce\", \
        \"InstanceType\":\"m3.medium\", \
        \"KeyName\":\"test-key\", \
        \"SecurityGroups\": [\"test-sg\"], \
        \"UserData\":\"`base64 -w 0 userdata.sh`\" \
    }"
  • How do I modify EBS volume size (in integer)? – shantanuo Dec 3 '18 at 16:15
0

For me only the following command worked, but not the proposed by Dimitrov:

user="$(cat /scripts/userdata.sh | base64 -w 0)"

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.