gpt4 book ai didi

amazon-web-services - 尝试运行 Lambda 函数来调用 ssm 并定义一个 EC2 标签以将其推送到多个实例上

转载 作者:行者123 更新时间:2023-12-04 08:52:15 24 4
gpt4 key购买 nike

尝试运行 Lambda 函数来调用 SSM 并定义一个 EC2 标签以使用以下脚本在多个实例上推送相同的标签。尝试执行时出现以下错误。我刚刚开始学习编写脚本并第一次使用 aws lambda。请帮我修复。

import boto3
ssm = boto3.client('ssm')
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
filters = (
Name = 'tag:Product',
Values = ['Essay']
)
instances = ('filters')
response = ssm.send_command(
InstanceIds=instances,
DocumentName='xxxxxxxxxxxxx',
DocumentVersion='$DEFAULT',
DocumentHash='916fdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdcdbe7940',
DocumentHashType='Sha256'
)
print(response)
错误 :
Response:
{
"errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 7)",
"errorType": "Runtime.UserCodeSyntaxError",
"stackTrace": [
" File \"/var/task/lambda_function.py\" Line 7\n Name = 'tag:Product',\n"
]
}

Request ID:
"8cb4cd39-b744-41da-befb-5f60b6e49fa4"

Function logs:
START RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 7)
Traceback (most recent call last):
  File "/var/task/lambda_function.py" Line 7
            Name = 'tag:Product',END RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4
REPORT RequestId: 8cb4cd39-b744-41da-befb-5f60b6e49fa4

最佳答案

有几个问题:
错误的缩进。
python中没有这样的东西

    filters = (
Name = 'tag:Product',
Values = ['Essay']
)
也许你的意思是字典?:
    filters = {
'Name':'tag:Product',
'Values': ['Essay']
}
InstanceIds=instances应该是字符串列表,而不是 'filters' 的文字字符串.
修复代码的最后一步如下:
import boto3

ssm = boto3.client('ssm')
ec2 = boto3.resource('ec2')

def lambda_handler(event, context):
filters = [{
'Name':'tag:Product',
'Values': ['Essay']
}]
instances = [instance.id for instance in ec2.instances.filter(Filters = filters)]

response = ssm.send_command(
InstanceIds=instances,
DocumentName='xxxxxxxxxxxxx',
DocumentVersion='$DEFAULT',
DocumentHash='916fdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdcdbe7940',
DocumentHashType='Sha256'
)
print(response)

关于amazon-web-services - 尝试运行 Lambda 函数来调用 ssm 并定义一个 EC2 标签以将其推送到多个实例上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64042035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com