Python 停止在环境变量中使用lambda的实例时发生客户端错误
我在环境变量中使用Lambda提供实例id来停止实例时遇到了客户端错误,但在硬编码实例id时工作正常 Lambda函数:Python 停止在环境变量中使用lambda的实例时发生客户端错误,python,instance,boto3,Python,Instance,Boto3,我在环境变量中使用Lambda提供实例id来停止实例时遇到了客户端错误,但在硬编码实例id时工作正常 Lambda函数: instances = ['i-0d66b89b8c010560d'] import boto3 import os # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1' region
instances = ['i-0d66b89b8c010560d']
import boto3
import os
# Enter the region your instances are in. Include only the region without
specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
# instances = ['i-0d66b89b8c010560d']
def lambda_handler(event, context):
print 'stopping your instance'
instances = os.environ['INSTANCES_ID']
print instances
print type(instances)
instances = list(instances)
print type(instances)
ec2 = boto3.client('ec2', region_name=region)
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instances: ' + str(instances)
日志输出:
START RequestId: 5c965493-fd10-11e8-9c0f-09f0c600ad35 Version: $LATEST
stopping your instance
i-0d66b89b8c010560d
<type 'str'>
<type 'tuple'>
An error occurred (InvalidInstanceID.Malformed) when calling the
StopInstances operation: Invalid id: "i": ClientError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 18, in lambda_handler
ec2.stop_instances(InstanceIds=instances)
File "/var/runtime/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
ClientError: An error occurred (InvalidInstanceID.Malformed) when calling
the StopInstances operation: Invalid id: "i"
启动请求ID:5c965493-fd10-11e8-9c0f-09f0c600ad35版本:$LATEST
停止实例
i-0d66b89b8c010560d
调用时发生错误(InvalidInstanceID.Malformed)
StopInstances操作:无效id:“i”:ClientError
回溯(最近一次呼叫最后一次):
lambda_处理程序中的文件“/var/task/lambda_function.py”,第18行
ec2.stop_实例(instanceId=instances)
文件“/var/runtime/botocore/client.py”,第314行,在api调用中
返回self.\u make\u api\u调用(操作名称,kwargs)
文件“/var/runtime/botocore/client.py”,第612行,在make\u api\u调用中
引发错误\u类(解析的\u响应、操作\u名称)
ClientError:调用时发生错误(InvalidInstanceID.Malformed)
StopInstances操作:无效id:“i”
正如您在日志输出中看到的,您的“实例”是一个str,其值为i-0d66b89b8c010560d
。你可能不知道的是,str是可测试的。让列表“呼叫”的行为与您期望的不同。因此,当您调用此代码时:
instances = list(instances)
实际上,您正在这样做:
>>> instances = 'i-0d66b89b8c010560d'
>>> list(instances)
['i', '-', '0', 'd', '6', '6', 'b', '8', '9', 'b', '8', 'c', '0', '1', '0', '5', '6', '0', 'd']
看看会发生什么?您的str将转换为列表,其中str的每个索引都是列表中的索引
因此,解决办法是:
instances = ['i-0d66b89b8c010560d']
import boto3
import os
# Enter the region your instances are in. Include only the region without
specifying Availability Zone; e.g., 'us-east-1'
region = 'us-east-1'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
# instances = ['i-0d66b89b8c010560d']
def lambda_handler(event, context):
print 'stopping your instance'
instances = os.environ['INSTANCES_ID']
print instances
print type(instances)
instances = [instances]
print type(instances)
ec2 = boto3.client('ec2', region_name=region)
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instances: ' + str(instances)