调用 client.request_spot_instances 方法时抛出 AWS Boto3 BASE64 编码错误

AWS Boto3 BASE64 encoding error thrown when invoking client.request_spot_instances method

我正在尝试使用 boto3(环境 Python 3.5,Windows 7)提交对 EC2 SPOT 实例的请求。 我需要为 运行 初始脚本传递 UserData 参数。

我得到的错误是 文件 "C:\Users...\Python\Python35\lib\site-packages\botocore\client.py",第 222 行,在 _make_api_call 中 引发 ClientError(parsed_response, operation_name) botocore.exceptions.ClientError: 发生错误 (InvalidParameterValue) 打电话给 RequestSpotInstances 操作:用户数据代码 BASE64 编码无效

我正在关注此文档 https://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.request_spot_instances

如果我去掉 UserData 参数 – 一切正常。

我尝试了不同的方法来传递参数,但我最终遇到了 same.similar 错误。

Boto 3 脚本

    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
    }
    })

我认为您不应该将 base64 字符串转换为 str。您在使用 Python 3 吗?

替换:

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

作者:

myparam = base64.b64encode(b'yum install -y php').decode("ascii")