1
   const dependencies: AmplifyDependentResourcesAttributes =
      AmplifyHelpers.addResourceDependency(
        this,
        amplifyResourceProps.category,
        amplifyResourceProps.resourceName,
        [
          {
            category: 'function',
            resourceName: 'usersimulation',
          },
          {
            category: 'function',
            resourceName: 'marketprocessor',
          }
        ]
      )



    const userSimulationLambdaArn = cdk.Fn.ref(
      dependencies.function.usersimulation.Arn
    )

    const marketMessageProcessorArn = cdk.Fn.ref(
      dependencies.function.marketprocessor.Arn
    )
    let sqs = new cdk.aws_sqs.Queue(this, 'sqs', {
      queueName: 'sqs_queue_' + cdk.Fn.ref('env'),
    })

    let sqs_evensource = new cdk.aws_lambda_event_sources.SqsEventSource(sqs, {
      batchSize: 5,
    })

    const userSimulationlambda = Function.fromFunctionAttributes(
      this,
      'userssimulationlambda',
      {
        functionArn: userSimulationLambdaArn,
        role: new cdk.aws_iam.Role(this, 'role1', {
          assumedBy: new cdk.aws_iam.ServicePrincipal('sqs.amazonaws.com'),
        }),
      }
    )

    const marketMessageProcessorLambda = Function.fromFunctionAttributes(
      this,
      'marketmessageprocessor',
      { functionArn: marketMessageProcessorArn, sameEnvironment: true }
    )

    userSimulationlambda.env['SQS_URL'] = sqs.queueUrl

What i have here are basically 2 lambdas.

One lambda called usersimulation sends messages to an SQS queue and marketmessageprocessor lambda processes these messages.

The problem here is:

How do i actually send the messages from lambda usersimulation?

I have try to pass the QueueUrl as an environment variable but this does not work.

I have try:

await sqs
  .sendMessageBatch({
    Entries: xmls.map((x, i) => ({
      Id: `id_${i}_${Date.now()}`,
      MessageBody: JSON.stringify(x),
    })),
    QueueUrl: process.env['SQS_URL']!,
  })
  .promise()

Its undefined, it says:

MissingRequiredParameter: Missing required key 'QueueUrl' in params

Whats the best way to send messages from an lambda to an sqs queue?

I am using here aws amplify and both functions are created with amplify function add

bill.gates
  • 14,145
  • 3
  • 19
  • 47
  • Here you can find what you are looking for https://stackoverflow.com/a/70832790/21263964 – Depa Feb 26 '23 at 09:27
  • You cannot modify imported resources. In this case, you cannot add an environment variable to an imported lambda function. – gshpychka Feb 28 '23 at 11:16

1 Answers1

1

It looks like the lambda code is correct, but I suspect the SQS_URL environment variable is undefined. You can log it to verify.

try making this change to your CDK code:

  userSimulationlambda.addEnvironment('SQS_URL', sqs.queueUrl);

EDIT ok, I'm new to CDK. Looks like you have an extra s in userssimulationlambda. If not an error, it's inconsistent from the other places you use the term. I still suspect the ENV var is undefined in the lambda. Log it (in the lambda) to know for certain. If you're getting a 'reference' to the lambda (via Function.fromFunctionAttributes), can you set its env var as you're doing, or would you need to do that where the function is actually created?

Edit 2 Also, I think env is describing the Lambda's environment (account, region) and not the environment variables. I think you need to get a Function and call that addEnvironment on it or include the SQS url as a key-value property when you make the Lambda Function.

Dave
  • 7,552
  • 4
  • 22
  • 26
  • Thank you, but `addEnvironment` does not exist. Its an `cdk.aws_lambda.IFunction`. Here: https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.IFunction.html#env – bill.gates Feb 25 '23 at 20:00
  • Reading the doc more. Don't you get a `Function` which implements `IFunction`, but also has the method I listed? Sorry, I don't have a CDK project handy to test. But ... I do think I'm on the right track here. Let us know what finally works. – Dave Feb 25 '23 at 20:36
  • Well with `Function.fromFunctionAttributes` i only get `IFunction` – bill.gates Feb 25 '23 at 20:42
  • step 1: log the environment var, see it's undefined and know you're on the right track. Step 2: properly set the environment variable. This answer appears to take things from where you left off in the example above: https://stackoverflow.com/questions/70825543/aws-cdk-update-existing-lambda-environment-variables – Dave Feb 25 '23 at 21:04
  • It seems that i have a different error now. It says that resource already exist. Whtats that now – bill.gates Feb 26 '23 at 11:46