Lambda function not working with cloud formation deploy - amazon-web-services

I am trying to build out an app with an api gateway and a node back end running on lambda.
The node backend is very basic with express and sequelize. It isn't hooked up to a database yet. I can run the node app locally, navigate to an end point and get the standard "hello world" response that I send back from that end point.
When I build and deploy the stack, it shows that the code deployed to an s3 bucket and that the api and lambda were updated. However, I get the following error when trying to access an end point -
502 Bad Gateway - "message": "Internal server error"
Again, there is no logic on the back end, just a text response sent back. I have no idea how to address this. It doesn't seem to be an API issue, although I don't know how to test that. If it is an issue with the lambda deploy, why does it work locally?
In addition to this, the api gateway created by the cloudformation file has a "dev" stage and a "Stage" stage. I am not sure how this happened or if it is a problem.
Below is the yaml file.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Storygraf backend API
Globals:
Function:
Timeout: 3
Resources:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: dev
ExpressLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://storygraf-backend/xxxx
Handler: lambda.handler
MemorySize: 512
Runtime: nodejs14.x
Timeout: 30
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId:
Ref: ExpressApi
Path: /
Method: ANY
ProxyApiGreedy:
Type: Api
Properties:
RestApiId:
Ref: ExpressApi
Path: /{proxy+}
Method: ANY

You should activate logs to be able to troubleshoot:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: dev
MethodSettings:
- ResourcePath: "/*"
HttpMethod: "*"
DataTraceEnabled: true
LoggingLevel: INFO # Or ERROR
OpenApiVersion: 3.0.1 # This will remove the stage Stage
Note about OpenApiVersion:
Note: Setting this property to any valid value will also remove the stage Stage that SAM creates.
Then copy your apiId:
Send your request to your API and go to CloudWatch, there should be a Log Group named API-Gateway-Execution-Logs_{apiId}/dev (you can paste your apiId in the search box to find it).
In there you can see detailed logs about the request and integration.
You most likely have an error like:
Execution failed due to configuration error: Malformed Lambda proxy response
Because lambda response should at least contain a statusCode field:
With the Lambda proxy integration, the Lambda function must return output of the following format:
statusCode: "...", // a valid HTTP status code
headers: {
custom-header: "..." // any API-specific custom header
},
body: "...", // a JSON string.
isBase64Encoded: true|false // for binary support } ```
See API Gateway developer guide and specifically:
Getting Started
Integration Response
Lambda Integration

Related

AWS API Gateway Responses via serverless framework

I'd like to be able to configure API Gateway Responses via serverless framework...
This is what my serverless.yml looks like:
#Deploy an api gateway with custom responses.
---
service: test-apigw
frameworkVersion: ">=3.20"
provider:
name: aws
stage: dev
region: us-east-1
stackName: ${self:service}
apiName: test-apigw
endpointType: REGIONAL
functions:
hello:
handler: handler.endpoint
events:
- http:
path: /hello-world
method: ANY
private: true
package:
individually: true
excludeDevDependencies: false
Now I read somewhere that one can configure resources in a serverless file, however by using CFN code I'd need the ARN or the Ref of an already deployed API Gateway or, declare the API Gateway with CFN code (which I want to avoid)...
Is there a way to configure these Gateway Responses (not integration responses!!) using the same api gateway that my serverless.yml is deploying? i.e. the one with name test-apigw?
It turns out the response I quoted actually works for me.
In the same serverless.yml file, put the GatewayResponses you want to configure.
resources:
Resources:
Unauthorized:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseTemplates:
"application/json": ${file(gateway-responses/responses.js):unauthorized}
ResponseType: UNAUTHORIZED
RestApiId: !Ref ApiGatewayRestApi
StatusCode: '401'
At least for version 3.20 of serverless framework, the created API name is ApiGatewayRestApi, this will only work of course, if you're only creating one API Gw in that file and I believe, the API Gateway has to be already deployed.
The correct way to do this is, as the official serverless documentation states, to create the API Gateway with Cloudformation code.

Getting unexpected results in API Gateway from cloudformation

I am somewhat new to writing yaml files and deploying them. I have one built from things that I have worked on in the past, but it is producing results that I don't understand and I believe that this is causing a CORS error when I used the API Gateway (It works using insomnia/postman).
Here is the YAML file.
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Storygraf backend API
Globals:
Function:
Timeout: 3
Resources:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
StageName: dev
ExpressLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
CodeUri: ./
Handler: lambda.handler
MemorySize: 512
Runtime: nodejs14.x
Timeout: 30
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /
Method: ANY
cors: true
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /{proxy+}
Method: ANY
cors: true
I don't really understand the ProxyAPIRoot and ProxyAPIGreedy settings, but I have run the deployment with and without that section and gotten the same results.
I am also attempting to create two stages on the API gateway - one for production and one for development but it occurs to me that I don't really understand the stages very well and the result that I am getting has a "dev" stage (which is correct) and a "Stage" stage, which is not correct.
Here are the resources and stages for the resulting gateway.
Why is there are "Stage" stage and not a "prod" stage?
What is the proxy? Is it causing my CORS problem?
You have to create separate SAM YAML files for different environments. You cannot specify two stages in the same YAML file. If you want to deploy the complete stack in one YAML file, I suggest you switch to pure Cloudformation template, then you would have a lot more flexibility on defining resources.
You can checkout this link and create multiple stages.
Regarding the /{proxy+} resource, it will capture all the requests, like /products, /users/1. Whereas, the / resource will only respond to requests to the endpoint root url, i.e / only.

Simple API gateway with Mock response defined in SAM?

I have a simple API gateway definition in AWS.
It has a single Method for "/" with a "GET" HTTP type.
I've updated the "Integration Request" to be a "Mock" (return a response purely with API Gateway mappings ...).
There's nothing fancy, all requests will be passed through.
For the response, I've updated the default Integration Response ( response status 200, no regex, model etc. Default mapping).
It has an "application/json" Mapping template, and in here i have just added the static text "hello world" (creative, i know).
Now, the question is, can i define this in a SAM template ?
I can readily find plenty of exmaples to create a lambda function behind this and have it do the response and all sorts of other things.
I can run the SAM cli locally to do "sam deploy" giving the sam template, and have a whole new AWS Stack created in my account, with the API Gateway created, the lambda python or javascript or etc uploaded to s3 and created and linked in to the API (through implicit sam definition, no external oas/swagger looked at yet) and roles etc created.
And it all works beautifully.
But if I want to do the same thing, but simpler - with just API gateway only and a mock integration response (no lambda) - what would the SAM template look like for that ?
As of yet, i am at a loss.
I can see from the serverless model that a lot of the integrations are supported in sam (x-amazon-apigateway-integration response).
I can see what the oas version looks like by using "api-gateway getexport" via the aws cli.
But I can not see a SAM template with implicit API creation that has no lambda linked in, just a simple Mock and static integration response.
Any help appreciated !!
SAM is a extension of Cloudformation, so yeah, it is possible, if you create a sam application and add the bellow template.yaml file, and run sam deploy --guided you will create what you need. In this case you will be running a simple cloudformation template that can be deployed with the following command:
aws cloudformation deploy --stack-name mock-api --template-file template.yaml
The main idea of SAM is facilitate the deploy of Serverless aplications, creating a possibility of invoke Lambda functions locally, improve debugging and so on, while provisioning your resources.
As we can see in the AWS documentation:
A serverless application is a combination of Lambda functions, event sources, and other resources that work together to perform tasks. Note that a serverless application is more than just a Lambda function—it can include additional resources such as APIs, databases, and event source mappings
So, if you are not using Lambda and you want provision your resources, you can simply use Cloudformation or another IaC tool as Terraform.
template.yml example (of what you need):
AWSTemplateFormatVersion: '2010-09-09'
Description: AWS API Gateway with a Mock Integration
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
ApiKeySourceType: HEADER
Description: An API Gateway with a Mock Integration
EndpointConfiguration:
Types:
- EDGE
Name: mock-api
ApiGatewayResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId: !GetAtt ApiGatewayRestApi.RootResourceId
PathPart: 'mock'
RestApiId: !Ref ApiGatewayRestApi
ApiGatewayMethod:
Type: AWS::ApiGateway::Method
Properties:
ApiKeyRequired: false
AuthorizationType: NONE
HttpMethod: POST
Integration:
ConnectionType: INTERNET
IntegrationResponses:
- ResponseTemplates:
application/json: "{\"message\": \"OK mock response\"}"
SelectionPattern: '2\d{2}'
StatusCode: 200
- ResponseTemplates:
application/json: "{\"message\": \"Internal Server Error mock response\"}"
SelectionPattern: '5\d{2}'
StatusCode: 500
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json: "{\"statusCode\": $input.json('$.statusCode'), \"message\": $input.json('$.message')}"
Type: MOCK
TimeoutInMillis: 29000
MethodResponses:
- ResponseModels:
application/json: !Ref ApiGatewayModel
StatusCode: 200
- ResponseModels:
application/json: !Ref ApiGatewayModel
StatusCode: 500
OperationName: 'mock'
ResourceId: !Ref ApiGatewayResource
RestApiId: !Ref ApiGatewayRestApi
ApiGatewayModel:
Type: AWS::ApiGateway::Model
Properties:
ContentType: 'application/json'
RestApiId: !Ref ApiGatewayRestApi
Schema: {}
ApiGatewayStage:
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId: !Ref ApiGatewayDeployment
Description: Mock API Stage v0
RestApiId: !Ref ApiGatewayRestApi
StageName: 'v0'
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: ApiGatewayMethod
Properties:
Description: Mock API Deployment
RestApiId: !Ref ApiGatewayRestApi
To do this another way with SAM - i have
manually created a simple API gateway with a single GET and a mock response of static text in the integration response.
exported that with aws apigateway get-export
remove the servers and policy blocks (those i'll have in my sam template)
refer to the OAS spec in your SAM template in the API properties with DefinitionBody
upload the OAS spec
use SAM deploy
the SAM specifications.
For that approach thanks to valdeci for this answer, and Jens Eickmeyer for this post

API Gateway created by SAM runs locally but times out when deployed to API Gateway

Exactly as the title says. I have an API with an HTTP POST request. When I run the POST request with the API Running locally (with sam local start-api), it works perfectly fine. However, when I deploy it to and run it in API Gateway, I get a timeout error after 30 seconds, even though I set the max timeout to 300 seconds. My SAM file is as below:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
python3.8
Sample SAM Template for manipulation-api
Globals:
Function:
Timeout: 300
Resources:
rManipulationAPI:
Type: AWS::Serverless::Api
Properties:
Cors:
AllowMethods: "'GET, POST'"
AllowOrigin: "'*'"
Name: manipulation-api
StageName: dev
rManipulationFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: Manipulate
MemorySize: 10000
PackageType: Image
ImageUri: <My Docker Repo>
Events:
Paraphrase:
Type: Api
Properties:
Path: /manipulate
Method: POST
RestApiId: !Ref rManipulationAPI
Metadata:
Dockerfile: Dockerfile
DockerContext: ./src/manipulate
DockerTag: python3.8-v1
rHealthCheckFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: HealthCheck
PackageType: Image
ImageUri: <My Docker Repo>
Events:
HealthCheck:
Type: Api
Properties:
Path: /health
Method: GET
RestApiId: !Ref rManipulationAPI
Metadata:
Dockerfile: Dockerfile
DockerContext: ./src/health_check
DockerTag: python3.8-v1
Outputs:
ManipulationApi:
Value: !Sub "https://${rManipulationAPI}.execute-api.${AWS::Region}.amazonaws.com/dev/manipulate"
This is a limitation of the API Gateway service, the API Gateway will timeout after 30 seconds regardless of what your Lambda timeout is set to.
If you expect your Lambdas execution times consistently get close to or take longer than 30 seconds, you'll need to move to more of an asynchronous pattern.
A few common patterns:
Client Polling: You provide two endpoints: one for submitting a request that returns instantly (i.e. /submitRequest) with a request ID; and another that allows a customer get the status of the request and determine if it's done and get the final result (i.e. /getResult).
Callbacks: Provide an endpoint that allows users to submit requests but also allow them to provide a URL that the service invokes when the request is completed with the results. Or just provide an SNS topic that users can subscribe to to get the results of their request.
Regardless, API Gateway can't support 30+ second responses. In general, it's not a good practice to design APIs that have these long waits as well since many things can happen to the connection. It's better to offer an asynchronous solution to clients which will be much less error prone.

How do i uncheck "Use Lambda Proxy Integration" in an API Gateway method from with SAM?

I'm trying to work my way through AWS sam and setting up a new ApiGateway. I want the lambdas to run without the 'use lambda proxy integration' setting. I've been trying for ages now, and haven't made much progress.
A simplified version of my template.yaml
Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
#snip#
Events:
PostEvent:
Type: Api
Properties:
Path: /Some/Path
Method: Post
RestApiId:
Ref: MyApi
MyApi:
Type: AWS::Serverless::Api
Properties:
Name: Some-Api
StageName: Prod
As you can see, I haven't made any progress at all. I'm simply getting lost in the configuration here, and hoping that anyone can point me in the right direction
Sicne you have opted out of integration proxy, the request and response may need mapping.
Have you tried updating the integration response - mapping template, try below,
AWS::Serverless::Api
GatewayResponses