JSON云格式不正确。我的JSON文件不正确吗?
我正在使用CloudFormation创建一个堆栈,但我目前正在模板编写过程中苦苦挣扎。这是我的JSON模板:JSON云格式不正确。我的JSON文件不正确吗?,json,amazon-web-services,amazon-s3,aws-lambda,amazon-cloudformation,Json,Amazon Web Services,Amazon S3,Aws Lambda,Amazon Cloudformation,我正在使用CloudFormation创建一个堆栈,但我目前正在模板编写过程中苦苦挣扎。这是我的JSON模板: { "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "LambdaFunction": { "Type": "AWS::Lambda::Function&q
{
"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验证器,但我无法理解为什么根据amazon CloudFormation,它的格式不好。
有什么想法吗?您不能像这样使用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文件。我没有使用CloudFormation,但肯定是无效的JSON。对于有效的JSON,
code
属性的整个值必须是字符串。类似地,您的处理程序
和运行时
属性也是可疑的。我已经尝试过这样做了,但出于某种原因,我认为包装整个代码部分是解决方案,并且不明白为什么它不起作用。谢谢你,克里斯,我可以一直依靠你,没问题,很高兴能帮上忙:)为了它的价值,yaml允许你使用更好的语法。您只需将ZipFile:|
放在下面,然后按原样(当然是按照正确的缩进)将代码放在下面;看见