I'd like to use Pulumi to prototype a workflow sys...
# general
c
I'd like to use Pulumi to prototype a workflow system using SQS queues. In a lambda that reads off of one queue, I would like to write an SQS message to another queue. How can I write to an SQS queue from within a lambda? Note: not interested in using a step function.
t
Call aws sdk?
c
Haha, well that will work if nothing else. I was hoping there was a simpler way to work with Pulumi constructs. Particularly with the abstracted concepts in Pulumi's cloud-aws library.
t
Copy code
import * as aws from "@pulumi/aws";
bucket.onObjectCreated("postToSlack", async (e) => {
  // direct access to the aws-sdk through `aws.sdk`.
  const sqs = new aws.sdk.SQS();
  sqs.sendMessage(/*...*/);
});
(that's s3 bucket but whatever)
c
Thanks Mikhail! That's exactly what I was looking for
t
Well, good question about the cloud library
I think I asked it several months ago and there was nothing extra. But maybe things changed?
cc @lemon-spoon-91807
c
And @tall-librarian-49374 the method you described isn't working for me, as the pulumi/aws library isn't meant to be referenced from within a lambda function (in my case). Here's the end of the error. Happy to post the rest if helpful.
Copy code
Module './node_modules\\@pulumi\\aws\\index.js' is a 'deployment only' module. In general these cannot be captured inside a 'run time' function.
    error: an unhandled error occurred: Program exited with non-zero exit code: 1
t
Last code block
and the previeww image 🙂
c
As @tall-librarian-49374 pointed out, that’s how I have done it too. It’s pretty straightforward. I sent mine in batches so I used
sendMessageBatch
from the SQS client.
Copy code
// The `aws-sdk` is a transient dependency of pulumi-aws.
import { SQS } from 'aws-sdk`;
try {
  const outputSQS = new SQS();
  await outputSQS.sendMessageBatch({
    Entries: [{Id: "someUniqueIDForThisMessage", MessageBody: myMessageBody}],
    QueueUrl: outputSQSURL
  }).promise();
} catch (err) {
  console.log("Error sending a batch", err);
}
Although, I do think that creating an SQS client from the
aws.sdk
object should have serialized the right imports in your Lambda. @lemon-spoon-91807 would be able to comment on that.
c
Thanks @clever-sunset-76585. Here's what I am trying to do:
Copy code
import * as aws from '@pulumi/aws';
import * as serverless from '@pulumi/aws-serverless';

const stage1Queue = new aws.sqs.Queue('stage-1', { visibilityTimeoutSeconds: 360 });
const stage2Queue = new aws.sqs.Queue('stage-2', { visibilityTimeoutSeconds: 360 });

serverless.queue.subscribe('stage-1', stage1Queue, async (e: any) => {
    console.log('Starting stage 1...');
    // Do some things
    // ...

    // Send to next stage
    const sqs = new aws.sdk.SQS();
    sqs.sendMessage({
        QueueUrl: stage2Queue.id.get(),
        MessageBody: '...'
    });
}, { batchSize: 1 });

exports = {
    stage1QueueURL: stage2Queue.id,
    stage2QueueURL: stage2Queue.id
};
It looks like pulumi/aws-serverless does not allow sqs.sendMessage to be called. I will continue trying other ways to approach this.
l
How can I write to an SQS queue from within a lambda?
c
Have you tried to import the
SQS
client directly from the
aws-sdk
as in my code snippet?
l
i would just use the normal aws sdk for this
note: you can get the aws sdk directly either by doing what @clever-sunset-76585 mentions, or by doing:
aws.sdk
if
aws
is your imported
@pulumi/aws
We even show the example here:
Copy code
bucket.onObjectCreated("postToSlack", async (e) => {
  // direct access to the aws-sdk through `aws.sdk`.
  const sqs = new aws.sdk.SQS();
  sqs.sendMessage(/*...*/);
});
Oh, slack just caught up, and i see now that that is mentioend above
nice.
c
@lemon-spoon-91807 Thanks for jumping in! Please see the code I am trying to work with, just above ^ This is using new aws.sdk.SQS() / sqs.sendMessage, just as in your example. But it is failing from within the serverless.queue.subscribe context
l
Can you send a full example of what's in your lambda?
c
That is the full example (hope you can see message that has the code in question.
l
as a temp workaround, can you simply do a
await import('aws-sdk')
t
Isn't
aws-serverless
sorta legacy?
l
@tall-librarian-49374 Yes. I showed John how to do this without aws-servlerless