aws-lambda - 模板包含错误。: 模板错误:Fn::GetAtt 实例引用 api 网关中未定义的资源 LambdaInvokeFunction

标签 aws-lambda yaml aws-cloudformation aws-api-gateway

我遇到了同样的错误。这是我的全部代码。任何人都可以解决它吗?

    
AWSTemplateFormatVersion: 2010-09-09
Description: My API Gateway and Lambda function

Parameters:
  ResourcesPrefix:
    Type: String
    Default: Nds-Cync
  EnvironmentType:
    Type: String
    Default: Prod
  ServiceName:
    Type: String
    Default: UserTourGuideApp
  RestApi:
    Type: String
    Default: ''
  StageName:
    Type: String
    Default: v1
  lambdaFunctionName:
    Type: String
    Default: proxy-lambda 

Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:          
      Description: AWS Lambda function
      FunctionName: !Ref lambdaFunctionName
      Handler: index.handler
      MemorySize: 256
      Role: !GetAtt LambdaIamRole.Arn
      Runtime: python3.7
      Timeout: 60
  apiGateway:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Name: !Sub '${ResourcesPrefix}-${EnvironmentType}-${ServiceName}'
      Description: Example API Gateway
      EndpointConfiguration:
        Types:
          - REGIONAL
  LambdaResource:
    Type: 'AWS::ApiGateway::Resource'
    Properties:
      ApiId: !Ref apiGateway
      ParentId: !GetAtt apiGateway.RootResourceId
      Path_Part: 'lambdaFunctionName'
    
  ANY:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      AuthorizationType: NONE
      ApiKeyRequired: false
      RestApiId: !Ref apiGateway
      ResourceId: !Ref LambdaResource
      HttpMethod: ANY
      Integration:
        Type: LAMBDA_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub 
          - >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
          - lambdaArn: !GetAtt lambdaFunction.Arn
      MethodResponses:
        - StatusCode: 200
          ResponseModels: { "application/json": "Empty" }
    
  Options:
    Type: "AWS::ApiGateway::Method"
    Properties:
      ResourceId: !Ref LambdaResource
      RestApiId: !Ref apiGateway
      AuthorizationType: NONE
      HttpMethod: OPTIONS
      Integration:
        Type: MOCK
        IntegrationResponses:
           - ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
              method.response.header.Access-Control-Allow-Origin: "'*'"
             StatusCode: '200'
             PassthroughBehavior: WHEN_NO_MATCH
        MethodResponses:
           - ResponseModels:
               application/json: Empty
             ResponseParameters:
               method.response.header.Access-Control-Allow-Headers: true
               method.response.header.Access-Control-Allow-Methods: true
               method.response.header.Access-Control-Allow-Origin: true
             StatusCode: '200'
      
  Any:
    Type: 'AWS::ApiGateway::Method'
    Properties:
      AuthorizationType: NONE
      RestApiId: !Ref apiGateway
      ResourceId: !Ref LambdaResource
      HttpMethod: ANY
      Integration:
        Type: LAMBDA_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub 
          - >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
          - lambdaArn: !GetAtt lambdaFunction.Arn
      MethodResponses:
        - StatusCode: 200
          ResponseModels: { "application/json": "Empty" }
  Options:
    Type: "AWS::ApiGateway::Method"
    Properties:
      ResourceId: !Ref LambdaResource
      RestApiId: !Ref apiGateway
      AuthorizationType: NONE
      HttpMethod: OPTIONS
      Integration:
        Type: MOCK
        IntegrationResponses:
           - ResponseParameters:
               method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
               method.response.header.Access-Control-Allow-Methods: "'POST,OPTIONS'"
               method.response.header.Access-Control-Allow-Origin: "'*'"
             ResponseTemplates:
              application/json: ''
             StatusCode: '200'
             PassthroughBehavior: WHEN_NO_MATCH
             RequestTemplates:
              application/json: '{"statusCode": 200}'
      MethodResponses:
        - ResponseModels:
             application/json: Empty
             ResponseParameters:
               method.response.header.Access-Control-Allow-Headers: true
               method.response.header.Access-Control-Allow-Methods: true
               method.response.header.Access-Control-Allow-Origin: true
             StatusCode: '200'

  apiDeployment:
    Type: AWS::ApiGateway::Deployment
    DependsOn:
      - LambdaResource
    Properties:
      RestApiId: !Ref apiGateway
      Stagename: !Ref StageName

  APIGatewayPermission:
    Type: 'AWS::Lambda::Permission'
    Properties:
      Action: 'lambda:InvokeFunction'
      FunctionName: 
        Fn::GetAtt:
          - lambdaFunctionName
          - Arn
      Principal: apigateway.amazonaws.com
    DependsOn:
      - apiDeployment

  lambdaIAMRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: 'Allow'
            Principal:
              Service:
                - 'lambda.amazonaws.com'
            Action:
              - 'sts:AssumeRole'
      Path: '/'
      Policies:
        - PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Effect: Allow
                Resource:
                  - !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${lambdaFunctionName}:*
          PolicyName: lambda
      Policies:
        - PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: 'Allow'
                Action: 'lambda:*'
                Resource: !GetAtt LambdaFunction.Arn
      Policies:
        - PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action: 'lambda:InvokeFunction'
                Resource: !GetAtt LambdaInvokeFunction.Arn

  lambdaLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: !Sub /aws/lambda/${lambdaFunctionName}
      RetentionInDays: 90
    

最佳答案

您的模板未定义任何LambdaInvokeFunction。也许应该是LambdaFunction

关于aws-lambda - 模板包含错误。: 模板错误:Fn::GetAtt 实例引用 api 网关中未定义的资源 LambdaInvokeFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70639245/

相关文章:

amazon-web-services - X-ray 给 AWS Lambda 函数增加了多少延迟

python - PyYAML 中 yaml.load 和 yaml.SafeLoader 的区别

aws-lambda - 特定用户的 IP 地址限制 - AWS/Cognito/IAM/API Gateway

java - 如何在 Java 中覆盖 Dropwizard/Jetty 参数以获得高性能?

azure - 如何将参数值与Azure yaml管道中的字符串进行比较

aws-cloudformation - 如何使用cloudformation更新 secret 字符串?

amazon-web-services - CloudFormation 堆栈卡在 'Create-In' 进度'

amazon-web-services - 有没有办法重命名cloudformation堆栈名称?

c# - 如何从 Amazon API Gateway 将参数从 POST 传递到 AWS Lambda

aws-lambda - 如何使用 cdk 将 sqs 队列 URL 传递给 lambda