Integration of Lambda, api gateway and event bridge where Target is set as sns topic for 5xx error.

0

I want to get the 5xx error of api gateway (REST Api - is used here) which has to be integrated with event bridge to set the rule regarding the event pattern for aws.apigateway. here, the api-gateway is used to invoke the lambda with the error code.

However I have given IAM role to lambda and inside the lambda trigger is Api Gateway, code for lambda is to generate the status code 503, which is here used to forcefully create the error of 5xx.

code of lambda used here is:

import json

def lambda_handler(event, context): # Simulate a 5xx error by returning a response with 5xx status code response = { 'statusCode': 503, # Use any 5xx status code you want to simulate 'body': json.dumps('Unavailable') }

return response

event pattern used was:

{ "source": ["aws.apigateway"], "detail-type": ["API Gateway Execution State Change"], "detail": { "response": { "status": ["500", "501", "502", "503", "504", "505"] },

the major concern is that we usually get no of errors in apigateway like 5xx, so to have the notification for the same, want to setup the whole.

  • Can you elaborate some more on the flow and exactly what you want to achieve? It is not clear to me from the question.

1 Answer
0

I just want to suggest a few troubleshooting. It seems like you want to capture 5xx errors from API Gateway using EventBridge and then send notifications via an SNS topic (If I get your question correctly).

Here's how you can achieve this:

  1. Lambda Function:

    You already have a Lambda function that generates a 503 error. Ensure that this Lambda function has the necessary permissions to be triggered by API Gateway.

  2. API Gateway Integration:

    Make sure your API Gateway is configured to integrate with your Lambda function for the respective resource/method that will generate the error.

  3. Create an SNS Topic:

    If you haven't already, create an SNS topic that will be used to send notifications.

  4. EventBridge Rule:

    • Go to the Amazon EventBridge service in the AWS Management Console.
    • Create a new rule:
      • Event Source: Choose "Event Source = AWS API Gateway".
      • Specific service(s): Select the API Gateway you want to monitor.
      • Event Type(s): Choose "ApiGateway" and "ApiCall".
      • Event pattern:
        {
          "detail": {
            "responseElements": {
              "httpStatus": ["5xx"]
            }
          }
        }
      • Targets: Add a target, and select the SNS topic you created earlier.
  5. Deploy the API Gateway:

    Make sure your API Gateway is deployed and the respective method/resource is accessible.

  6. Generate the Error:

    By accessing the API method that triggers the Lambda function and returns a 503 status code, generate the error condition.

  7. Check SNS for Notifications:

    • Go to the Amazon SNS service in the AWS Management Console.
    • Navigate to the topic you created.
    • You should see notifications related to the 5xx error that was triggered.

NOTE

  • The event pattern provided in step 4 filters the events to only capture responses with 5xx status codes. You can further customize the pattern if needed.
  • Ensure proper IAM roles and permissions are set up for Lambda, API Gateway, EventBridge, and SNS.
  • Make sure your Lambda function is returning the desired 503 status code.
  • Be aware of the potential cost implications of setting up and triggering these resources.

Always consider best practices for security and monitoring when implementing such integrations. This approach should allow you to capture 5xx errors from API Gateway and receive notifications via SNS when those errors occur.

profile picture
answered 8 months ago
  • ur answer is not working in practical

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions