在Chalice应用程序中使用boto3启动EC2实例不起作用



我在使用boto3从Chalice部署的Lambda启动EC2实例时遇到问题。

相关代码如下:

resource = boto3.resource('ec2')
instance = resource.Instance(params['instance_id'])
if params['action'] == 'run':
try:
response = instance.start()
except BaseException as be:
logging.exception("Error: Failed to start instance" + str(be) )
raise ChaliceViewError("Internal error at server side")
else:
try:
response = instance.stop(Force=True)
except BaseException as be:
logging.exception("Error: Failed to stop instance" + str(be) )
raise ChaliceViewError("Internal error at server side")

请求似乎成功了。例如在2种情况下;start(("方法被称为boto3,其响应如下:{"Status":{"StartingInstances":[{"CurrentState":{"Code":0,"Name":"pending"},"InstanceId":"i-0129bb4079559e5bc","PreviousState":{"Code":80,"Name":"stopped"}}],"ResponseMetadata":{"RequestId":"d88a9fbc-f2f2-4c51-9629-30a63c7e753b","HTTPStatusCode":200,"HTTPHeaders":{"x-amzn-requestid":"d88a9fbc-f2f2-4c51-9629-30a63c7e753b","content-type":"text/xml;charset=UTF-8","content-length":"579","date":"Wed, 23 Sep 2020 16:59:40 GMT","server":"AmazonEC2"},"RetryAttempts":0}}}

另一个回应是:

{"Status":{"StartingInstances":[{"CurrentState":{"Code":0,"Name":"pending"},"InstanceId":"i-0129bb4079559e5bc","PreviousState":{"Code":80,"Name":"stopped"}}],"ResponseMetadata":{"RequestId":"2bde553a-87f1-4fe0-a13a-8b4db4c0dbbc","HTTPStatusCode":200,"HTTPHeaders":{"x-amzn-requestid":"2bde553a-87f1-4fe0-a13a-8b4db4c0dbbc","content-type":"text/xml;charset=UTF-8","content-length":"579","date":"Wed, 23 Sep 2020 17:07:58 GMT","server":"AmazonEC2"},"RetryAttempts":0}}}

然而,在这两种情况下,实例都没有启动,AWS控制台中的实例状态保持在"0";停止";。

当我在python控制台中尝试相同的代码片段时,它成功了,实例成功启动:

>>> import boto3
>>> resource = boto3.resource('ec2')
>>> instance = resource.Instance('i-0129bb4079559e5bc')
>>> response = instance.start()
>>> response
{'StartingInstances': [{'CurrentState': {'Code': 0, 'Name': 'pending'}, 'InstanceId': 'i-0129bb4079559e5bc', 'PreviousState': {'Code': 80, 'Name': 'stopped'}}], 'ResponseMetadata': {'RequestId': '535224cc-21d8-45fa-a4a2-0ac984cdfe9a', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '535224cc-21d8-45fa-a4a2-0ac984cdfe9a', 'content-type': 'text/xml;charset=UTF-8', 'content-length': '579', 'date': 'Wed, 23 Sep 2020 17:05:10 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}}

以前有人见过这种行为吗?有什么明显的东西我遗漏了吗?

我最终向AWS支持部门提出了这个问题。

我试图启动的机器已从另一个AWS帐户迁移,并使用KMS密钥对其备份EBS卷进行了加密。Lambda执行角色需要访问权限才能使用KMS密钥来启动EC2实例。

在AWS技术人员的建议下,我在KMS密钥政策中添加了以下声明:

{
"Sid": "Allow Lambda role use of the CMK",
"Effect": "Allow",
"Principal": {
"AWS": [
"<REPLACE WITH LAMBDA-EXECUTION-ROLE-ARN>"
]
},
"Action": [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey",
"kms:CreateGrant"
],
"Resource": "*"
}

一旦这一点到位,实例就成功启动了。

我有一个悬而未决的问题(如果我收到这个答案,我会更新它(是,如果Lambda没有权限,为什么boto3启动操作返回成功。

相关内容

  • 没有找到相关文章

最新更新