AWS CloudFormation/API Gateway 提示 '指定的资源标识符无效'。

8

我一直在尝试使用CloudFormation部署API Gateway,但是我的方法资源一直遇到相同的问题。堆栈部署失败并显示“指定的资源标识符无效”。

这是我在CloudFormation模板中的方法资源:

"UsersPut": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "ResourceId": "UsersResource",
            "RestApiId": "MyApi",
            "ApiKeyRequired": true,
            "AuthorizationType": "NONE",
            "HttpMethod": "PUT",
            "Integration": {
                "Type": "AWS_PROXY",
                "IntegrationHttpMethod": "POST",
                "Uri": {
                    "Fn::Join": ["", ["arn:aws:apigateway:", {
                        "Ref": "AWS::Region"
                    }, ":lambda:path/2015-03-31/functions/", {
                        "Fn::GetAtt": ["MyLambdaFunc", "Arn"]
                    }, "/invocations"]]
                }
            },
            "MethodResponses": [{
                "StatusCode": 200
            }]
        }
    }

有人能帮我弄清楚为什么这个堆栈部署一直失败吗?

更新:我忘了提到我还尝试使用引用来添加资源ID,但那也给我带来了同样的错误:

"UsersPut": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "ResourceId": {
                "Ref": "UsersResource"
            },
            "RestApiId": "MyApi",
            "ApiKeyRequired": true,
            "AuthorizationType": "NONE",
            "HttpMethod": "PUT",
            "Integration": {
                "Type": "AWS_PROXY",
                "IntegrationHttpMethod": "POST",
                "Uri": {
                    "Fn::Join": ["", ["arn:aws:apigateway:", {
                        "Ref": "AWS::Region"
                    }, ":lambda:path/2015-03-31/functions/", {
                        "Fn::GetAtt": ["MyLambdaFunc", "Arn"]
                    }, "/invocations"]]
                }
            },
            "MethodResponses": [{
                "StatusCode": 200
            }]
        }
    }

这是完整的CloudFormation模板:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
    "LambdaDynamoDBRole": {
        "Type": "AWS::IAM::Role",
        "Properties": {
            "AssumeRolePolicyDocument": {
                "Version": "2012-10-17",
                "Statement": [{
                    "Effect": "Allow",
                    "Principal": {
                        "Service": [
                            "lambda.amazonaws.com"
                        ]
                    },
                    "Action": [
                        "sts:AssumeRole"
                    ]
                }]
            },
            "Path": "/",
            "Policies": [{
                "PolicyName": "DynamoReadWritePolicy",
                "PolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [{
                        "Sid": "1",
                        "Action": [
                            "dynamodb:DeleteItem",
                            "dynamodb:GetItem",
                            "dynamodb:PutItem",
                            "dynamodb:Query",
                            "dynamodb:Scan",
                            "dynamodb:UpdateItem"
                        ],
                        "Effect": "Allow",
                        "Resource": "*"
                    }, {
                        "Sid": "2",
                        "Resource": "*",
                        "Action": [
                            "logs:CreateLogGroup",
                            "logs:CreateLogStream",
                            "logs:PutLogEvents"
                        ],
                        "Effect": "Allow"
                    }]
                }
            }]
        }
    },
    "MyFirstLambdaFn": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "myfirstlambdafn",
                "S3Key": "lambda_handler.py.zip"
            },
            "Description": "",
            "FunctionName": "MyFirstLambdaFn",
            "Handler": "lambda_function.lambda_handler",
            "MemorySize": 512,
            "Role": {
                "Fn::GetAtt": [
                    "LambdaDynamoDBRole",
                    "Arn"
                ]
            },
            "Runtime": "python2.7",
            "Timeout": 3
        },
        "DependsOn": "LambdaDynamoDBRole"
    },
    "MySecondLambdaFn": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Code": {
                "S3Bucket": "mysecondlambdafn",
                "S3Key": "lambda_handler.py.zip"
            },
            "Description": "",
            "FunctionName": "MySecondLambdaFn",
            "Handler": "lambda_function.lambda_handler",
            "MemorySize": 512,
            "Role": {
                "Fn::GetAtt": [
                    "LambdaDynamoDBRole",
                    "Arn"
                ]
            },
            "Runtime": "python2.7",
            "Timeout": 3
        },
        "DependsOn": "LambdaDynamoDBRole"
    },
    "MyApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
            "Name": "Project Test API",
            "Description": "Project Test API",
            "FailOnWarnings": true
        }
    },
    "FirstUserPropertyModel": {
        "Type": "AWS::ApiGateway::Model",
        "Properties": {
            "ContentType": "application/json",
            "Name": "FirstUserPropertyModel",
            "RestApiId": {
                "Ref": "MyApi"
            },
            "Schema": {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "FirstUserPropertyModel",
                "type": "object",
                "properties": {
                    "Email": {
                        "type": "string"
                    }
                }
            }
        }
    },
    "SecondUserPropertyModel": {
        "Type": "AWS::ApiGateway::Model",
        "Properties": {
            "ContentType": "application/json",
            "Name": "SecondUserPropertyModel",
            "RestApiId": {
                "Ref": "MyApi"
            },
            "Schema": {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "SecondUserPropertyModel",
                "type": "object",
                "properties": {
                    "Name": {
                        "type": "string"
                    }
                }
            }
        }
    },
    "ErrorCfn": {
        "Type": "AWS::ApiGateway::Model",
        "Properties": {
            "ContentType": "application/json",
            "Name": "ErrorCfn",
            "RestApiId": {
                "Ref": "MyApi"
            },
            "Schema": {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "Error Schema",
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string"
                    }
                }
            }
        }
    },
    "UsersResource": {
        "Type": "AWS::ApiGateway::Resource",
        "Properties": {
            "RestApiId": {
                "Ref": "MyApi"
            },
            "ParentId": {
                "Fn::GetAtt": ["MyApi", "RootResourceId"]
            },
            "PathPart": "users"
        }
    },
    "UsersPost": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "ResourceId": {
                "Ref": "UsersResource"
            },
            "RestApiId": "MyApi",
            "ApiKeyRequired": true,
            "AuthorizationType": "NONE",
            "HttpMethod": "POST",
            "Integration": {
                "Type": "AWS_PROXY",
                "IntegrationHttpMethod": "POST",
                "Uri": {
                    "Fn::Join": ["", ["arn:aws:apigateway:", {
                        "Ref": "AWS::Region"
                    }, ":lambda:path/2015-03-31/functions/", {
                        "Fn::GetAtt": ["MyFirstLambdaFn", "Arn"]
                    }, "/invocations"]]
                }
            },
            "MethodResponses": [{
                "ResponseModels": {
                    "application/json": {
                        "Ref": "FirstUserPropertyModel"
                    }
                },
                "StatusCode": 200
            }, {
                "ResponseModels": {
                    "application/json": {
                        "Ref": "ErrorCfn"
                    }
                },
                "StatusCode": 404
            }, {
                "ResponseModels": {
                    "application/json": {
                        "Ref": "ErrorCfn"
                    }
                },
                "StatusCode": 500
            }]
        }
    },
    "UsersPut": {
        "Type": "AWS::ApiGateway::Method",
        "Properties": {
            "ResourceId": {
                "Ref": "UsersResource"
            },
            "RestApiId": "MyApi",
            "ApiKeyRequired": true,
            "AuthorizationType": "NONE",
            "HttpMethod": "PUT",
            "Integration": {
                "Type": "AWS_PROXY",
                "IntegrationHttpMethod": "POST",
                "Uri": {
                    "Fn::Join": ["", ["arn:aws:apigateway:", {
                        "Ref": "AWS::Region"
                    }, ":lambda:path/2015-03-31/functions/", {
                        "Fn::GetAtt": ["MySecondLambdaFn", "Arn"]
                    }, "/invocations"]]
                }
            },
            "MethodResponses": [{
                "ResponseModels": {
                    "application/json": {
                        "Ref": "SecondUserPropertyModel"
                    }
                },
                "StatusCode": 200
            }, {
                "ResponseModels": {
                    "application/json": {
                        "Ref": "ErrorCfn"
                    }
                },
                "StatusCode": 404
            }, {
                "ResponseModels": {
                    "application/json": {
                        "Ref": "ErrorCfn"
                    }
                },
                "StatusCode": 500
            }]
        }
    },
    "RestApiDeployment": {
        "Type": "AWS::ApiGateway::Deployment",
        "Properties": {
            "RestApiId": {
                "Ref": "MyApi"
            },
            "StageName": "Prod"
        },
        "DependsOn": ["UsersPost", "UsersPut"]
    }
},
"Description": "Project description"

}

4个回答

4

ResourceId必须是指向云构建资源的引用,而不是一个简单的字符串。

例如:

  ResourceId:
    Ref: UsersResource

是的,抱歉我也尝试使用引用,但是那个错误还是出现了。 - user3067870
你是如何定义你的资源的?你能发布一个更完整的CF模板吗? - jens walter
我已经更新了问题的完整模板,谢谢。 - user3067870

2

我发现实际上需要引用的是RestApiId:

            "RestApiId": {
                "Ref": "MyApi"
            },

1

当您创建API资源(1)时,将为路径/创建API的默认根资源(2)。要获取MyApi资源(1)根资源(2)的ID,请使用:

"ResourceId": {
  "Fn::GetAtt": [
    "MyApi",
    "RootResourceId"
  ]
}

(1) 栈资源 (2) API 资源


0

尝试

  {
    "$ref": "https://apigateway.amazonaws.com/restapis/YOUR_API_GATEWAY_IT/models/YOUR_MODEL_NAME"
  }

此外,您还可以通过将该模型的URL设置为$ref属性的值来引用在外部模型文件中定义的另一个模型模式:“$ref”:“https://apigateway.amazonaws.com/restapis/{restapi_id}/models/{model_name}”。

有关更多详情,请参见此处

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接