Link. the of my ApiGateway Resource to my Lambda Event

0

Please how do include a parent ID of my ApiGateway Resource to my Lambda Event.

i have some resource already define i just want to link Lambda Event.

      Events:
        Api:
          Type: Api
          Properties:
            Path: /
            Method: GET
            ParentId: !GetAtt AuthResources.Id
            RestApiId: !Ref ProptterAccountsAPI

and also link with a seperate Api Model below

AuthModel:
    Type: AWS::ApiGateway::Model
    DeletionPolicy: Delete
    Properties:
      RestApiId: !Ref SampleApi
      ContentType: application/json
      Name: AuthModel
      Schema:
        $schema: "http://json-schema.org/draft-04/schema#"
        title: Item
        type: object
        properties:
          itemId: {type: string}
          itemName: {type: string}
        required: [itemId, itemName]
2 Answers
1
Accepted Answer

The following is a sample, but I thought it could be used by simply setting the return value from "AWS::ApiGateway::RestApi" in "ParentId".
Also, try putting the name of the Lambda function in the "PathPart" field.
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-resource.html#cfn-apigateway-resource-parentid

  Api:
    Type: "AWS::ApiGateway::RestApi"
    Properties:
      Name: "api_cfn_test"
 
  Resource:
    Type: "AWS::ApiGateway::Resource"
    Properties:
      RestApiId: !Ref Api
      ParentId: !GetAtt Api.RootResourceId
      PathPart: !Sub "${FunctionName}"
    DependsOn:
      - Api 
profile picture
EXPERT
answered 8 months ago
  • Thank You, But i have a seprate Stack(Nested Stack) for my Api Gateway and Lambda i have already defined my reource eg[user/ member] i want the lambda even to point to the resource /user

  • I have made some modifications to CloudFormation that I have used in the past. In CloudFormation, set the URL in "AWS::ApiGateway::Resource".

       Api:
        Type: "AWS::ApiGateway::RestApi"
        Properties:
          Name: "api"
      Resource:
        Type: "AWS::ApiGateway::Resource"
        Properties:
          RestApiId: !Ref Api
          ParentId: !GetAtt Api.RootResourceId  
          PathPart: "user"
      LambdaPermission:
        Type: "AWS::Lambda::Permission"
        Properties:
          FunctionName: !Sub "${FunctionName}"
          Action: "lambda:InvokeFunction"
          Principal: "apigateway.amazonaws.com"
      ResourceMethod:
        Type: "AWS::ApiGateway::Method"
        Properties:
          RestApiId: !Ref Api
          ResourceId: !Ref Resource
          AuthorizationType: "None"
          HttpMethod: "POST"
          Integration:
            Type: "AWS"
            IntegrationHttpMethod: "POST"
            Uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${FunctionName}/invocations"
        DependsOn: "LambdaPermission"
    
1

Hi,

I'm assuming that you are using the Serverless Application Model (SAM). The resource EventSoure does not define a field ParentId for the type Api.

The CloudFormation resource AWS::ApiGateway::Resource does however.

If you are using SAM, the EventSource resource will automatically link your AWS Lambda function to the respective API Gateway method.

profile pictureAWS
EXPERT
answered 8 months ago
  • Thank You, But i have a seprate Stack(Nested Stack) for my Api Gateway and Lambda i have already defined my reource eg[user/ member] i want the lambda even to point to the resource /user

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions