5

I have a lambda that is created by deploying an AWS SAM stack. Now I have mentioned in the Events section that the lambda is to be triggered by an API, but the issue being that this creates a new API, instead of re-using the existing one in the account.

I can not find much details on re-using an existing API, for this purpose. Any pointers are appreciated!


My code:

Resources:
 Lamdba:
  Type: AWS::Serverless::Function
  Properties:
   Handler: 'index.myLambda'
   Events:
    MyAPI:
     Type: Api
     Properties:
      Path: '/pushMessage'
      Method: post
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    If you are creating a new API Gateway in a different stack and trying to import here, that could work with !ImportValue. But If you want to reference Id of an already existing API Gateway, that wont work yet. – Himakar Jul 06 '21 at 05:03
  • Any idea if there are plans to make this possible or any idea how I could reference as existing api gateway resource that was not created as part of an existing template? – rizan Dec 07 '21 at 20:48
  • @rizan I also want to reference an API Gateway I "lazily" created on the console quickly, but works perfectly, but alas I have run into the same issue. There appears to be no logical way to reference an existing API Gateway outside of the SAM because the SAM must be "self-contained" and create the entire stack including said Gateway. This is unfortunate, the best solution I could find was to export the SAM from the API Gateway and just incoporate it in the Lambda's SAM and delete the old API Gateway (hacky yes, but always start with SAM and no issues forward) – AutoM8R Oct 27 '22 at 20:16

1 Answers1

0

You can specify an Api resource in your template and import it into your CFN stack, then in your event, you can use it via RestApiId

Edit: SAM doesn’t support event source referencing an API defined outside the same template.

xhg
  • 1,850
  • 2
  • 21
  • 35
  • do you have an example of this? I am now trying to `RestApiId: Ref: "Fn::ImportValue": my-exported-apigateway` but this hits an error that the apigateway is not defined in the template – Gerard van den Bosch Feb 02 '23 at 02:45