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

问题描述 投票:0回答:1

我正在尝试使用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:调用RequestSpotInstances操作时发生错误(InvalidParameterValue):用户数据的无效BASE64编码

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

如果我取出UserData参数 - 一切正常。

我尝试了不同的方法来传递参数,但我最终得到了相同的错误。

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
    }
    })
python amazon-web-services amazon-ec2 base64 boto3
1个回答
1
投票

我认为你不应该将你的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")
© www.soinside.com 2019 - 2024. All rights reserved.