DevBabai
DevBabai

Reputation: 83

AWS lambda code deployment issue with clouformation

I am trying to deploy lambda function(python 3) through cloudformation, Even though the stack creates successfully, I am having issues with code alignment after deployment!

Here is the actual code from cloudformation template before deploying:

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))

Here is the lambda function in the console after successful stack creation! Is there something I am doing wrong here with indentation or some thing else?

enter image description here

Upvotes: 1

Views: 319

Answers (1)

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6339

According to this SO answer https://stackoverflow.com/a/21699210/970247 you should use a ZipFile: | instead of ZipFile: > to properly preserve your newlines.

EDIT: Following the suggestion from Abi in the comments: It's preferred to have your Lambda code inside S3 and just linking back to it in the template. This could be cumbersome but luckily there's a way of automating that using the AWS CLI. You can basically refer to a local file on your computer and using aws package the tool will automatically upload the lambda code to S3 and generate a template with the appropriate link. Here's the doc regarding that specific functionality.

Upvotes: 2

Related Questions