Search code examples
aws-lambdaaws-api-gatewayaws-sam-clisam

How to reference existing lambda in sam template


I am using SAM to create an ApiGateway rest resource. I wanted a post method to call the existing lambda function. How do I do it? I tried with the following template but it failed with error

"FAILED" Status: FAILED. Reason: Template format error: Unresolved resource dependencies [arn:aws:lambda:us-east-1:1111111111:function:LabBytesServer-AddAccountFunction-iasdsaddsdsads] in the Resources block of the template

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  MyApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: dev
      DefinitionBody:
        info:
          version: '1.0'
        paths:
          /myendpoint:
            post:
              responses: { }
              x-amazon-apigateway-integration:
                uri: !Join
                  - ""
                  - - "arn:aws:apigateway:"
                    - !Ref "AWS::Region"
                    - ":lambda:path/2015-03-31/functions/"
                    - !Ref arn:aws:lambda:us-east-1:111111111:function:LabBytesServer-AddAccountFunction-das2efadsdsafasd
                    - "/invocations"

                httpMethod: POST
                type: aws_proxy
      Tags:
        app: "labBytes"
        env: "dev"

Solution

  • Instead of using !Ref, can you try with !Sub and give the lambda function. Eg below:

                    uri: !Join
                      - ""
                      - - "arn:aws:apigateway:"
                        - !Ref "AWS::Region"
                        - ":lambda:path/2015-03-31/functions/"
                        - !Sub arn:aws:lambda:us-east-1:111111111:function:LabBytesServer-AddAccountFunction-das2efadsdsafasd
                        - "/invocations"