【问题标题】:JSON CloudFormation not well formed. Is my JSON file incorrect?JSON CloudFormation 格式不正确。我的 JSON 文件不正确吗?
【发布时间】:2020-11-04 00:01:39
【问题描述】:

我正在使用 CloudFormation 创建堆栈,但我目前在模板编写过程中苦苦挣扎。这是我的 JSON 模板:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Properties": {
                "Code": {
                    import json
                    import boto3

                    s3 = boto3.client('s3')

                    def lambda_handler(event, context): # Get bucket name from the S3 event
                    print(event)

                    bucket_name = event['detail']['requestParameters']['bucketName']

                    # Create a bucket policy
                    bucket_policy = json.dumps({
                            s3 = boto3.client('s3')

                            def lambda_handler(event, context): # Get bucket name from the S3 event
                            print(event)

                            bucket_name = event['detail']['requestParameters']['bucketName']

                            # Create a bucket policy
                            bucket_policy = json.dumps({
                                "Version": "2012-10-17",
                                "Statement": [{
                                        "Sid": "MustBeEncryptedAtRest",
                                        "Effect": "Deny",
                                        "Principal": "*",
                                        "Action": "s3:PutObject",
                                        "Resource": [
                                            "arn:aws:s3:::{}".format(bucket_name),
                                            "arn:aws:s3:::{}/*".format(bucket_name)
                                        ],
                                        "Condition": {
                                            "StringNotEquals": {
                                                "s3:x-amz-server-side-encryption": [
                                                    "AES256",
                                                    "aws:kms"
                                                ]
                                            }
                                        }
                                    },
                                    {
                                        "Sid": "MustBeEncryptedInTransit",
                                        "Effect": "Deny",
                                        "Principal": "*",
                                        "Action": "s3:*",
                                        "Resource": [
                                            "arn:aws:s3:::{}".format(bucket_name),
                                            "arn:aws:s3:::{}/*".format(bucket_name)
                                        ],
                                        "Condition": {
                                            "Bool": {
                                                "aws:SecureTransport": "false"
                                            }
                                        }
                                    }
                                ]
                            })
                        }
                        # Set the new policy s3.put_bucket_policy(Bucket = bucket_name, Policy = bucket_policy),
                        "Handler": lambda_handler,
                        "Role": ----
                        "Runtime": python3 .7
                    }
                }

但我在"Code": import json 行上收到错误消息。我什至使用了不同的 JSON 验证器,但我不明白为什么它根据亚马逊 CloudFormation 格式不正确。 有什么想法吗?

【问题讨论】:

  • 我没有使用过 CloudFormation,但这肯定不是有效的 JSON。对于有效的 JSON,Code 属性的整个值需要在字符串中。同样,您的 HandlerRuntime 属性也是可疑的。
  • 我已经尝试过了

标签: json amazon-web-services amazon-s3 aws-lambda amazon-cloudformation


【解决方案1】:

您不能像这样使用 Lambda 函数输入,而对于 JSON,它应该像下面的输入示例那样格式化。

"LambdaFunction": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Code": {
          "ZipFile": {
            "Fn::Join": [
              "\n",
              [
                "import json",
                "import boto3",
                "",
                "s3 = boto3.client('s3')",
                "",
                "def lambda_handler(event, context):",
                    "print(\"Do something\")"
              ]
            ]
          }
        },
        "Handler": "index.lambda_handler",
        "Role": {
          "Ref": "LambdaRole"
        },
        "Runtime": "python3.7"
      }
    }

请记住,CloudFormation 是 JSON 或 YAML,这意味着它必须解析,对于 Lambda,您可以指定针对您正在使用的语言格式化的函数,也可以指定一个 Zip 文件。

【讨论】:

  • 出于某种原因,我认为包装整个代码部分是解决方案,但无法弄清楚为什么它不起作用。谢谢你,克里斯,我永远可以依靠你
  • 没问题,很高兴为您提供帮助:)
  • 不管怎样,yaml 允许您使用更好的语法。您可以只输入ZipFile: | 并将您的代码按原样(当然要遵循适当的缩进)放在下面;见stackoverflow.com/a/21699210/5317869
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多