2016-09-29 64 views
0

我试图使用boto3(环境Python 3.5,Windows 7)提交EC2 SPOT实例的请求。 我需要通过UserData参数来运行初始脚本。AWS Boto3调用client.request_spot_instances方法时抛出的BASE64编码错误

我得到的错误是 文件 “C:\用户\ Python的\ Python35 \ LIB \站点包\ 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参数 - 一切正常。

我已经尝试了不同的方式来传递参数,但我最终以相同的错误。

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

回答

1

我想你不应该你的base64字符串转换为str。你在使用Python 3吗?

替换:

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

通过:

myparam = base64.b64encode(b'yum install -y php') 
+0

它给我一个错误的类型无效参数LaunchSpecification.UserData,值:b'fdfd”,类型:<类的字节'>,有效的类型: - 因为它期望一个字符串 – user1811107

+2

该文档不清楚类型。它说“字符串”。试试:'base64.b64encode(b'yum install -y php')。decode(“ascii”)'。 –

+0

工作正常!谢谢 - 我编辑它以使代码更具可读性,如base64.b64encode('yum install -y php'.encode())。decode(“ascii”)。 – user1811107

相关问题