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
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
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.
Outputs
To output the default API Gateway endpoint you need to follow this format:
https://{api-id}.execute-api.{region}.amazonaws.com/{stage}
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.