2017-06-07 75 views
2

我一直在嘗試使用CloudFormation部署到API網關,但是,我經常遇到與我的方法資源相同的問題。 「部署的資源標識符無效」會導致堆棧部署失敗。AWS CloudFormation/API網關給出'指定的資源標識無效'

這裏是我的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" 

}

回答

2

ResourceId必須是對cloudformation資源的引用,而不是簡單的字符串。

例如

ResourceId: 
    Ref: UsersResource 
+0

是啊,對不起,我還沒有嘗試使用引用,但是這給了我同樣的錯誤。 – user3067870

+0

你是如何定義資源的?你能發佈更完整的CF模板嗎? –

+0

我已更新完整模板的問題,謝謝。 – user3067870

0

我計算過,居然有人過於參考其所需的RestApiId:

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