【问题标题】:AWS Lambda key errorAWS Lambda 密钥错误
【发布时间】:2018-11-11 08:50:45
【问题描述】:

我有 lambda 函数来创建新的 AMI 并附加到当前的自动缩放组。当我创建一个测试自定义测试用例并传递有效负载时,这个功能就完成了。当我从 SNS 触发此问题时会出现问题:

import json
import boto3
import time
import sys

asObj = boto3.client('autoscaling')
ec2Obj = boto3.client('ec2')

def lambda_handler(event, context):
    targetASG = event[‘Records’][0][‘Sns’][‘Message’]
    ASG = asObj.describe_auto_scaling_groups(AutoScalingGroupNames=[event['targetASG']])
    sourceInstanceId = ASG.get('AutoScalingGroups')[0]['Instances'][0]['InstanceId']
    Date=time.strftime("%d%m%y")
    Time=time.strftime("%H%M%S")
    amiName = "Automated_%s_%s" % (Date, Time)
    configName = "Automated_%s_%s" % (Date, Time)

    CreateNewImage = ec2Obj.create_image(
        InstanceId = sourceInstanceId,
        Name = amiName,
        Description = 'Automatically Created Image from Lambda Service',
        NoReboot = True)
    Image = []
    Image.append(CreateNewImage)
    def getCreatedID(Image):
        for i in Image:
            ID = i['ImageId']
            return ID
    AMINewID = getCreatedID(Image)
    CreateNewConfig = asObj.create_launch_configuration(
        LaunchConfigurationName = configName,
        ImageId = AMINewID,
        InstanceId = sourceInstanceId)

    updateASG = asObj.update_auto_scaling_group(
        AutoScalingGroupName = event['targetASG'],
        LaunchConfigurationName = configName)
    return 'A new AMI has been Created `%s` Updated ASG `%s` with new launch configuration `%s` which includes AMI `%s`.' % (amiName,event['targetASG'], configName, AMINewID)  

为了触发我的 lambda 函数,我使用 AWS SNS 主题并发布了一条消息 以下消息,消息类型:原始

{   "targetASG": "pre-production-xxx" }

但我收到此错误:

'targetASG':KeyError Traceback(最近一次调用最后一次):文件 “/var/task/lambda_function.py”,第 13 行,在 lambda_handler ASG = asObj.describe_auto_scaling_groups(AutoScalingGroupNames=[event['targetASG']]) KeyError: 'targetASG'

我该如何解决这个问题?

【问题讨论】:

    标签: python amazon-web-services aws-lambda boto3


    【解决方案1】:

    ASG = asObj.describe_auto_scaling_groups(AutoScalingGroupNames=[event['targetASG']])

    我认为targetASG 不是 SNS 事件的一部分。SNS 事件如下所示:

    { "Records": [ { "EventVersion": "1.0", "EventSubscriptionArn": "arn:aws:sns:EXAMPLE", "EventSource": "aws:sns", "Sns": { "SignatureVersion": "1", "Timestamp": "1970-01-01T00:00:00.000Z", "Signature": "EXAMPLE", "SigningCertUrl": "EXAMPLE", "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e", "Message": "Hello from SNS!", "MessageAttributes": { "Test": { "Type": "String", "Value": "TestString" }, "TestBinary": { "Type": "Binary", "Value": "TestBinary" } }, "Type": "Notification", "UnsubscribeUrl": "EXAMPLE", "TopicArn": "arn:aws:sns:EXAMPLE", "Subject": "TestInvoke" } } ] }

    您应该获取消息并从那里解析 targetASG 值,例如

    import json
    
    [...]
    targetASG = event[‘Records’][0][‘Sns’][‘Message’]
    myMessage = json.loads(targetASG)
    name = myMessage['targetASG']
    ASG = asObj.describe_auto_scaling_groups(AutoScalingGroupNames=[name])
    [...]
    

    【讨论】:

    • 是的,非常棒,所以当我从 SNS 触发 lambda 时,部署成功。我无法从该有效负载中获取当前的 ASG 名称,对吗?所以我继续这样的方法。所以我在部署成功时触发 lambda,所以我在函数上硬编码了 ASG 名称。有更好的方法吗?
    猜你喜欢
    • 2022-01-01
    • 2019-03-07
    • 2022-08-19
    • 2020-10-26
    • 1970-01-01
    • 2017-05-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多