Example, cron run every one minute, this cron get ...
# help
p
Example, cron run every one minute, this cron get some data from database and send to sqs.
f
Hi @Paulo Castellano, just to clarify, does the cron trigger a Lambda function and the Lambda function gets the data from db?
p
Yes, i have a background job to: 1. Cron get data from datatabase every one minute.
2. Send data to queue, que make process and store in database again.
I can run cron, but i don't know how cron send message to queue.
f
In your CDK code, if you have something like:
Copy code
const queue = new sst.Queue(this, "MyQueue");

const cron = new sst.Cron(this, "MyCron", {
  schedule: "rate(1 minute)",
  job: {
    handler: "src/lambda.main",
    environment: {
      QUEUE_URL: queue.sqsQueue.queueUrl
    }
  }
});
In your Lambda code, you can do something like this to read the data from DynamoDB and write to the queue:
Copy code
const data = await dynamodbClient.query(...).promise();

await sqs.sendMessage({
  MessageBody: JSON.stringify(data),
  QueueUrl: process.env.QUEUE_URL
}).promise();
Would this work for u?
p
@Frank thanks bro!
I think it's works... but i have a 403 code (access denied)\
my aws key permissions:
i need setup any?
f
My bad, this is not your aws key’s permission. It’s complaining the Lambda doens’t have permission to sendMessage to sqs
try this:
Copy code
const queue = new sst.Queue(this, "MyQueue");
const cron = new sst.Cron(this, "MyCron", {
  schedule: "rate(1 minute)",
  job: {
    handler: "src/lambda.main",
    environment: {
      QUEUE_URL: queue.sqsQueue.queueUrl
    },
    permissions: [queue],
  }
});
so add the
permissions: [queue]
line
p
great, thats works @Frank
how i can add a consumer to this job?
i add consumer here?
Copy code
job: {
        handler: "src/RunHttp.main",
        environment: {
          QUEUE_URL: queue.sqsQueue.queueUrl
        },
        permissions: [queue],
      }``
f
You can add a consumer to the queue like this:
Copy code
const queue = new sst.Queue(this, "MyQueue", {
  consumer: "src/consumerLambda.main",
});

const cron = new sst.Cron(this, "MyCron", {
  schedule: "rate(1 minute)",
  job: {
    handler: "src/lambda.main",
    environment: {
      QUEUE_URL: queue.sqsQueue.queueUrl
    },
    permissions: [queue],
  }
});
p
Great, works 🙂
Very thanks... in consumer how i can get the data?
how you found this information? Because i not found it on documentation...
f
Each construct has bite sized examples in their doc
And there are also some full length examples here https://serverless-stack.com/examples/index.html
p
yeah but this example is only for queue separately of cron...
i think you have large experience with sst / serverless.com and it make the diference ehhe
for consume data for postgres, i can use pgsql from node? you recomend it?
f
Yeah we need to write more use-case specific examples like this ie. cron > sqs > dynamodb
@Jay we need more higher level examples for use cases involving multiple constructs
p
if docs are open source, i can make a PR in github to help us.
f
@Paulo Castellano I don’t have much experience with postgres. It’s probably a better question for the community. I know some folks are using RDS.
j
Yeah, let's make a list of these common patterns and we can start writing example for them?
I started a discussion about it, https://github.com/serverless-stack/serverless-stack/discussions/379. Leave some comments with the ones we want to cover?
p
Great @Jay i will comment about this my case. cron -> sqs -> database
@Frank to deploy this code in multiples regions is simple?
f
You can specify the region thru the cli, ie.
sst deploy --region eu-west
Is that what you were asking?
p
I want to deploy in multiples regions simultaneous.. its possible to pass multi regions in cli?
f
No currently. You would have to deploy to each region separately.
Is you app running in multiple regions, and do they communicate with each other across region?
p
hm ok, i think its not a problem because i can create a script to publish in all regions
every region is separated (queue and crons)
i rewriting a uptime monitor becased in multiple regions
if people want monitor by 2 regions i have cron from 2 regions and sqs for 2 regions
not is a problem
f
ah got it
I guess that’d be easier if you are deploying through a CI/CD, git push and deploy to multiple regions simultaneously. A common pattern I see ppl do on Seed.
p
yeah, i use gitlab ci
but this is a second step, now i just validating if all works hehe
i need only do postgres works
only this is missing for my validation to be complete