1
votes

I have defined my lambda function in the cloudformation template. This lambda function takes bucket name using cloudformation Ref: BucketName. the problem i am facing is BucketName value is coming in next line and breaking the python code as python is space sensitive. is there any way to fix it?

      "ZipFile": {
        "Fn::Join": [
          "\n",
          [
            "import boto3",
            "import json",
            "def handler(event, context):",
            "    try:",
            "        s3BucketName=event['detail']['requestParameters']['bucketName']",
            "        if s3BucketName == '", {"Ref": "BucketName"},"':",
            "            return ",
2

2 Answers

0
votes

It's coming on the next line because of your "\n" before your array. It is putting a newline between every string in your array, including where you referenced the BucketName.

Wojciech is right; YAML would be easier for you in this case. But if you want to stick to JSON, you can get rid of the "\n" and replace it with an empty string. Then put the \n on every line where you really need the separator. e.g:

"import boto3\n",
"import json\n",
-1
votes

Use !Sub , and of course write in yaml, so it is easier to write. Here is a very good example in "MULTI-LINE STRINGS AND THE SUB SYNTAX" section: http://www.fischco.org/technica/2017/cloud-formation-sub/

the trick is to use "!Sub |" construction