skip to Main Content

I have a task that need to be scheduled on aws lambda function. I wrote a SAM template as below and I see it works when deploying on aws environment (my function get triggered intervally).

But we want to do testing on dev environment first before deploying. I use sam local start-api [OPTIONS] to deploy our functions to dev environment. But the problem is that, every functions configured as rest API work, but the schedule task not. I’m not sure is it possible on local/dev environment or not. If not, please suggest a solution (is it possible?). Thank you

This is template:

aRestApi:
    ...
    ...
sendMonthlyReport:
    Type: AWS::Serverless::Function
    Properties:
      Handler: src.monthlyReport
      Runtime: nodejs16.x
      Events:
        ScheduledEvent:
          Type: Schedule
          Properties:
            Schedule: "cron(* * * * *)"

3

Answers


  1. Chosen as BEST ANSWER

    I used localstack for the demonstrate. LocalStack is a cloud service emulator that runs in a single container on your laptop or in your CI environment. You can see more detail in this link https://github.com/localstack/localstack


  2. I would use node-cron to set a scheduler on a node file to run.

    npm install –save node-cron

    var cron = require('node-cron');
    
    cron.schedule('* * * * *', () => {
        console.log('running a task every minute');
    });
    

    https://www.npmjs.com/package/node-cron

    You can also check for this DigitalOcean tutorial!

    Login or Signup to reply.
  3. If you search for local testing before deployment of lambda functions you will probably be fed this resource by the quote-unquote "google": https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html

    However, there are other ways to do it.

    I personally use the public docker image from aws.

    Here is an example of the docker image created for a specific use case of mine.

    FROM public.ecr.aws/lambda/python:3.8
    
    RUN yum -y install tar gzip zlib freetype-devel 
        gcc 
        ghostscript 
        lcms2-devel 
        libffi-devel 
        libimagequant-devel 
        .
        enter code here
        enter code here
        and some more dependencies .... 
        && yum clean all
    
    
    COPY requirements.txt ./
    
    
    RUN python3.8 -m pip install -r requirements.txt
    # Replace Pillow with Pillow-SIMD to take advantage of AVX2
    RUN pip uninstall -y pillow && CC="cc -mavx2" pip install -U --force-reinstall pillow-simd
    
    COPY <handler>.py ./<handler>.py
    
    # Set the CMD to your handler
    ENTRYPOINT [ "python3.8","app5.py" ]
    

    In your case, follow instructions for node and run the docker image locally. If it works, you can then continue with aws lambda creation/update.

    I see that you also have a cron job, why not use the cron job to invoke this lambda function separately and not define it in you SAML?

    There are a number of ways you can invoke a lambda function based on event.
    For example to invoke using cli: (For aws-cliv2)

    make sure you configure awscli

    # !/bin/bash
    export AWS_PROFILE=<your aws profile>
    export AWS_REGION=<aws region>
    
    aws lambda invoke --function-name <function name> 
    --cli-binary-format raw-in-base64-out 
    --log-type Tail 
    --payload <your json payload > 
    <output filename>
    

    Makes it loosely coupled.

    Then you can use carlo’s node cronjob suggestion to invoke is as many times you like, free of charge.

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