python-3.x - Cloudformation 的 AWS lambda 代码部署问题

标签 python-3.x amazon-web-services aws-lambda aws-cloudformation

我正在尝试通过cloudformation部署lambda函数(python 3),尽管堆栈创建成功,但部署后我遇到了代码对齐问题!

以下是部署前来自 cloudformation 模板的实际代码:

lambdaEbsFunction:
    Type: "AWS::Lambda::Function"
    Properties:
      Code:
        ZipFile: >
          #!/usr/bin/env python

          import boto3
          import json
          import logging
          from __future__ import print_function

          #setup simple logging for INFO.
          logger = logging.getLogger()
          logger.setLevel(logging.ERROR)

          #define the connection for EC2.
          ec2 = boto3.resource('ec2', region_name="us-east-2")
          def lambda_handler(event, context):
            def tag_them(instance, detail):
                tempTags=[]
                v={}

                for t in instance.tags:
                    #pull the name tag and add volume detail
                    if t['Key'] == 'Name':
                        v['Value'] = t['Value'] + " - " + str(detail)
                        v['Key'] = 'Name'
                        tempTags.append(v)
                    #append the wanted tags to EBS
                    elif t['Key'] == 'Owner':
                        print("[INFO]: Owner tag " + str(t))
                        tempTags.append(t)
                    elif t['Key'] == 'Environment':
                        print("[INFO]: Environment Tag " + str(t))
                        tempTags.append(t)
                    else:
                        print("[INFO]: Skip Tag - " + str(t))

                print("[INFO] " + str(tempTags))
                return(tempTags)

            base_instances = ec2.instances.filter(
                Filters = [

                            {
                                'Name': 'tag:Name',
                                'Values': ['Jenkins Server']
                            },

                        ]
            )

            for instance in base_instances:
                for vol in instance.volumes.all():
                    tag = vol.create_tags(Tags=tag_them(instance, vol.attachments[0]['Device']))
                    print("[INFO]: " + str(tag))

这是成功创建堆栈后控制台中的 lambda 函数!我在缩进或其他方面做错了什么吗?

enter image description here

最佳答案

根据这个SO答案https://stackoverflow.com/a/21699210/970247您应该使用 ZipFile: | 而不是 ZipFile: > 来正确保留换行符。

编辑: 按照评论中 Abi 的建议:最好将 Lambda 代码放在 S3 中,然后在模板中链接回它。这可能很麻烦,但幸运的是,有一种使用 AWS CLI 实现自动化的方法。您基本上可以引用计算机上的本地文件,并使用 aws package 该工具会自动将 lambda 代码上传到 S3 并生成带有适当链接的模板。 Here's the doc regarding that specific functionality

关于python-3.x - Cloudformation 的 AWS lambda 代码部署问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48690055/

相关文章:

node.js - aws-lambda-nodejs : How can I use callback function to return a value which I printed in aws-console?

amazon-web-services - 通过 ALB 和 Lambda 集成提供二进制响应

python - 类型错误 : cannot convert the series to <class 'float' >

python - 创建长时间运行的进程的机制

amazon-web-services - API 网关 : Mixing of REST APIs and HTTP APIs on the same domain name can only be accomplished through API Gateway's V2 DomainName interface error

ssl - Elastic Load Balancing 非终止 SSL 连接上的代理协议(protocol)

node.js - NodeJS 和 AWS Lambda 的异步等待问题

python - Left Outer 通过数据框上的键、系列上的索引将数据框与 Series 对象连接起来

python-3.x - 如何使用 systemd 服务运行 pygame 脚本?

amazon-web-services - 如何通过aws cli删除Route53中的DNS记录?