Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python boto3请求“U点”实例方法不适用于;“用户数据”;_Python_Amazon Web Services_Amazon Ec2_Boto3 - Fatal编程技术网

Python boto3请求“U点”实例方法不适用于;“用户数据”;

Python boto3请求“U点”实例方法不适用于;“用户数据”;,python,amazon-web-services,amazon-ec2,boto3,Python,Amazon Web Services,Amazon Ec2,Boto3,我试图通过AWS Lambda发出AWS EC2 spot实例请求,并使用boto3调用EC2 API 现在我可以创建spot实例,但“UserData”参数不起作用 #!/usr/bin/env python # -*- coding: utf-8 -*- import boto3 import json import logging import base64 import os from pathlib import Path def request_spot_instance(acce

我试图通过AWS Lambda发出AWS EC2 spot实例请求,并使用boto3调用EC2 API

现在我可以创建spot实例,但“UserData”参数不起作用

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import boto3
import json
import logging
import base64
import os
from pathlib import Path

def request_spot_instance(access_key, secret_key):
    ec2_client = boto3.client('ec2',
    aws_access_key_id = access_key,
    aws_secret_access_key = secret_key,
    region_name = 'ap-northeast-1'
)
response = ec2_client.request_spot_instances(
    SpotPrice = '0.2',
    Type = 'one-time',
    LaunchSpecification = {
        'ImageId': 'ami-be4a24d9',
        'KeyName': 'my_key',
        'InstanceType':'c4.large',
        'UserData': base64.encodestring(u'#!/bin/sh \n touch foo.txt'.encode('utf-8')).decode('ascii'),
        'Placement':{},
        'SecurityGroupIds':[
            'sg-6bd2780c'
        ]
    }
)
return response


def lambda_handler(event, context):
    response = request_spot_instance(os.environ.get('AWS_ACCESS_KEY_ID'), os.environ.get('AWS_SECRET_ACCESS_KEY'))
    print(response)
    return event, context

if __name__ == "__main__":
    lambda_handler("","")
我将此代码用于Python2.7.11

请求方法的响应为:

{u'SpotInstanceRequests': [{u'Status': {u'UpdateTime': datetime.datetime(2016, 12, 23, 6, 11, 8, tzinfo=tzutc()), u'Code': 'pending-evaluation', u'Message': 'Your Spot request has been submitted for review, and is pending evaluation.'}, u'ProductDescription': 'Linux/UNIX', u'SpotInstanceRequestId': 'sir-cerib8cg', u'State': 'open', u'LaunchSpecification': {u'Placement': {u'AvailabilityZone': 'ap-northeast-1c'}, u'ImageId': 'ami-be4a24d9', u'KeyName': 'miz_private_key', u'SecurityGroups': [{u'GroupName': 'ssh only', u'GroupId': 'sg-6bd2780c'}], u'SubnetId': 'subnet-32c6626a', u'Monitoring': {u'Enabled': False}, u'InstanceType': 'c4.large'}, u'Type': 'one-time', u'CreateTime': datetime.datetime(2016, 12, 23, 6, 11, 8, tzinfo=tzutc()), u'SpotPrice': '0.200000'}], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '285867eb-9303-4a6d-83fa-5ccfdadd482f', 'HTTPHeaders': {'transfer-encoding': 'chunked', 'vary': 'Accept-Encoding', 'server': 'AmazonEC2', 'content-type': 'text/xml;charset=UTF-8', 'date': 'Fri, 23 Dec 2016 06:11:08 GMT'}}}
此响应不包括“UserData”,这似乎与手册中所写的有所不同。 我怀疑UserData参数不被接受

欢迎提出任何建议。

自行解决

用户数据外壳脚本是在根“/”目录下执行的,只是我找不到生成的文件

$ ls -al /foo.txt
-rw-r--r-- 1 root root 0 Dec 23 07:03 /foo.txt