AWS Boto3 BASE64 encoding error thrown when invoking client.request_spot_instances method
<p>I am trying to submit a request for an EC2 SPOT instance using boto3 (Environment Python 3.5,Windows 7). 
I need to pass the <strong>UserData</strong> parameter for running initial scripts.</p>

<p>The error I get is 
  File "C:\Users...\Python\Python35\lib\site-packages\botocore\client.py", line 222, in _make_api_call
    raise ClientError(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when
calling the 
<strong><em>RequestSpotInstances operation: Invalid BASE64 encoding of user data Code</em></strong></p>

<p>I am following this documentation 
<a href="https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.request_spot_instances" rel="nofollow">https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.request_spot_instances</a></p>

<p><strong>If I take the UserData parameter out â everything works well.</strong> </p>

<p>I have tried different ways to pass the parameter but I end up with the same.similar errors.</p>

<p><strong>Boto 3 Script</strong></p>

<pre><code>    client = session.client('ec2')

    myparam = str(base64.b64encode(bytes('yum install -y php', 'utf-8')))

    response = client.request_spot_instances(
    SpotPrice='0.4',
    InstanceCount=1,
    Type='one-time',
    LaunchSpecification={
    'ImageId': 'ami-xxxxxx',
    'KeyName': 'xxxxx',
    'InstanceType': 't1.micro',
    'UserData': myparam,
    'Monitoring': {
    'Enabled': True
    }
    })
</code></pre>

<p>I think you shouldn't convert your base64 string to <code>str</code>. Are you using Python 3?</p>

<p>Replace:</p>

<pre><code>myparam = str(base64.b64encode(bytes('yum install -y php', 'utf-8')))
</code></pre>

<p>By:</p>

<pre><code>myparam = base64.b64encode(b'yum install -y php')
</code></pre>

python amazon-web-services amazon-ec2 base64 boto3