【问题标题】:AWS lambda code deployment issue with clouformationCloudformation 的 AWS lambda 代码部署问题
【发布时间】:2018-07-19 07:01:28
【问题描述】:

我正在尝试通过 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 函数!我在缩进或其他方面做错了什么吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 另外,将代码保存在 S3 中并简单地参考 cloudformation 模板中的 s3 链接可能会更好
    • 谢谢 Laurent,会试试看的!
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 2015-10-19
    • 2019-01-09
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2017-06-12
    • 2021-11-20
    相关资源
    最近更新 更多