skip to Main Content

I am using cloudformation script yaml file to create resources on AWS dev account.

My goal is to create a lambda function with API gateway endpoint. The lambda function is created successfully and also the gateway within it. But when I tried to specify the API end point URL in the script, I am greeted with the following error.

Resource handler returned message: "Invalid API identifier specified 442645024664:test-api- 
gateway-ms (Service: ApiGateway, Status Code: 404, Request ID: 47e0c115-02c4-4dfd-b13a- 
b149c72807cf)" (RequestToken: a3fdecd7-41fd-cfef-f538-2da3bac33ec2, HandlerErrorCode: 
NotFound)

Below is my stack from template.yaml.

Parameters:
  MyApi:
  Type: String
  Description: "This is a demo API"
  AllowedValues: [ "dev-demo-lambda-api-poc" ]

Resources:
  MyDemoLambdaApiFunction:
  Type: AWS::Serverless::Function
  Properties:
    Description: >
    Currently does not support S3 upload event.
  Handler: app.lambda_handler
  Runtime: python3.11
  CodeUri: .
  MemorySize: 1028
  Events:
    MyDemoAPI:
      Type: Api
      Properties:
        Path: /test
        Method: GET
  Tracing: Active

Deployment:
  Type: AWS::ApiGateway::Deployment
  Properties:
    Description: Demo Lambda API Gateway deployment
    RestApiId: !Ref MyApi

Outputs:
  MyApiUrl:
  Description: API Gateway URL
  Value:
    Fn::Sub: https://dev-demo-lambda-api-poc

Setting aside the error, Iam bit confused here when creating the endpoint for the API. What’s the correct way of providing the end point URL in the CFN script?

I believe I should first Deploy this before I can have the end point?

Any help is much appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I tried the Outputs section as you mentioned, but still I am not seeing the end point as I expected. The end point I am seeing is something like

    https://hjkol19gpa.execute-api.us-east-1.amazonaws.com/api

    This is my CFN stack

    MyDemoLambdaApiFunction:
      Type: AWS::Serverless::Function
      Properties:
        Description: >
          Currently does not support S3 upload event.
        Handler: app.lambda_handler
        Runtime: python3.11
        CodeUri: .
        MemorySize: 1028
        Events:
          MyDemoAPI:
            Type: Api
            Properties:
              RestApiId: !Ref api
              Path: /gtl
              Method: GET
        Tracing: Active
    
    api:
      Type: AWS::Serverless::Api
      Properties:
        Name: "mytest"
        StageName: api
        TracingEnabled: true
        OpenApiVersion: 3.0.2
    
    Outputs:
      MyApiDefaultUrl:
      Description: API Gateway URL
      Value: !Sub "https://${api}.execute-api.${AWS::Region}.amazonaws.com/api"
    

    The stack ran successfully, but the URL is still the same as mentioned above. Shouldn't the URL now be

    https://mytest.execute-api.us-east-1.amazonaws.com/api ?

    Sorry if any mis-understanding here.


  2. Outputs

    To output the default API Gateway endpoint you need to follow this format:

    https://{api-id}.execute-api.{region}.amazonaws.com/{stage}

    Outputs:
      MyApiDefaultUrl:
        Description: API Gateway URL
        Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/<stageName>"
    
    # Where:
    # ${MyApiGateway} is the reference to you API Gateway resource
    # <stageName> is the Stage Name
    
    # If you have a separate AWS::ApiGateway::Stage
    Outputs:
      MyApiDefaultUrl:
      Description: My API Gateway Default URL
      Value: !Sub "https://${MyApiGateway}.execute-api.${AWS::Region}.amazonaws.com/${MyApiStage}"
    

    See: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-custom-domains.html


    Custom Domain Name

    To give your API a custom domain name, you need to use AWS::ApiGateway::DomainName.

    See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-domainname.html for the template.

    See Setting up custom domain names for REST APIs for the prerequisites.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search