Jayen
Jayen

Reputation: 6079

Is it possible to use AWS Lambda to process an HTTP response without making the corresponding HTTP request?

I have to make an HTTP request that takes a long time to receive a response. I don't want AWS Lambda to make this request as I will be charged for the time it is waiting for the response. Is there any way to use AWS Lambda to handle the response without being charged while waiting?

Upvotes: 2

Views: 567

Answers (1)

Chris
Chris

Reputation: 4810

Based on my comment, I would recommend hitting the long poll endpoint often as opposed to staying connected for long periods of time. You can use a CloudWatch Rule to trigger the lambda function every 5 minutes (or whatever interval you choose). You can give the Lambda a short timeout, say 5-10 seconds, which should prevent it from running for too long. I'm assuming the long poll endpoint will guarantee delivery at least once.

Here is some CloudFormation YAML to get you started on the setup. Far from complete, but should get you on the right track.

Description: Automatically hit long poll endpoint
Resources:

  #################################################
  # IAM Role for Lambda
  #################################################
  ROLELAMBDADEFAULT:
    Type: AWS::IAM::Role
    Properties:
      RoleName: your-lambda-default
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - edgelambda.amazonaws.com
            - lambda.amazonaws.com
          Action: sts:AssumeRole
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
      - arn:aws:iam::aws:policy/service-role/AWSLambdaRole
      Policies: []


  #################################################
  # Lambda function
  #################################################
  LFUNC:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: bucket-with-code
        S3Key: code.zip
      Description: Some function name
      FunctionName: my-function-name
      Handler: index.handler
      MemorySize: 256
      Role: !GetAtt ROLELAMBDADEFAULT.Arn
      #choose your runtime here
      Runtime: nodejs8.10
      Timeout: 6

  #################################################
  # Rule to trigger the lambda
  #################################################
  RULE1:
    Type: AWS::Events::Rule
    Properties:
      Name: custom-trigger
      Description: Trigger my lambda
      ScheduleExpression: rate(5 minutes)
      State: ENABLED
      Targets:
        - Arn: !GetAtt LFUNC.Arn
          Id: uniqueid1

Upvotes: 1

Related Questions