How to launch an amazon ec2 spot instance with UserData

amazon ec2

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

Best Answer

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