aws-amplify / aws-amplify/amplify-cli

Can not using amplify-generated Lambda to poll SQS queue

Open
#10,332 3 comments 3 reactions 0 assignees View on GitHub
bug extensibility functions p2
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-cli/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-cli/blob/master/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

16.3

### Amplify CLI Version

8.0.3

### What operating system are you using?

mac

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No.

### Amplify Categories

custom

### Amplify Commands

push

### Describe the bug

![order-processing-system](https://user-images.githubusercontent.com/5106417/166259113-7cf3cd1f-f97f-46b5-9504-aa033f21f407.png)

I am unable to recreate the diagram above where the following take place:

1. An REST API + function is created with the `amplify add api` command`
2. A database is created with `amplify add storage`
3. A function is created with `amplify add function`
4. An SQS queue is created with `amplify add custom`

The services themselves work, however when importing the second function (the one polling the queue), into the CDK and adding the SQS queue as a subscribed event, the CLI fails to push.

Error message:

image

At first, I thought this was a simple permissions issue, until I observed the following:

This is my `custom-policies.json` file:
```js
[
{
"Action": [
"sqs:ReceiveMessage",
"sqs:ChangeMessageVisibility",
"sqs:GetQueueUrl",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
],
"Resource": ["arn:aws:sqs:us-east-1:*:orderProcessingQueue-${env}"]
}
]
```

When pushing, I can see the function's policy get correctly updated to show that those permissions are being applied and that the queue is being created. Further, I can verify that the Lambda is being attached to the Queue before erroring out.

For my `cdk-stack.ts` file, I have the following:

```js
import * as cdk from '@aws-cdk/core'
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper'
import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref'
import * as sqs from '@aws-cdk/aws-sqs'
import { SqsEventSource } from '@aws-cdk/aws-lambda-event-sources'
import * as lambda from '@aws-cdk/aws-lambda'
import * as iam from '@aws-cdk/aws-iam'

export class cdkStack extends cdk.Stack {
constructor(
scope: cdk.Construct,
id: string,
props?: cdk.StackProps,
amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps
) {
super(scope, id, props)
/* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
new cdk.CfnParameter(this, 'env', {
type: 'String',
description: 'Current Amplify CLI env name',
})

//create the sqs queue that matches our Amplify environment
const queue = new sqs.Queue(this, 'orderProcessingQueue', {
queueName: `orderProcessingQueue-${cdk.Fn.ref('env')}`,
})

//get access to our Amplify resource (Lambda function)
const retVal: AmplifyDependentResourcesAttributes =
AmplifyHelpers.addResourceDependency(
this,
amplifyResourceProps.category,
amplifyResourceProps.resourceName,
[{ category: 'function', resourceName: 'orderQueuePoller' }]
)

const lambdaRole = iam.Role.fromRoleArn(
this,
'lambdaRole',
`arn:aws:iam:::role/${cdk.Fn.ref(
retVal.function.orderQueuePoller.LambdaExecutionRole
)}`
)

lambdaRole.attachInlinePolicy(
new iam.Policy(this, 'receive-events', {
statements: [
new iam.PolicyStatement({
actions: ['sqs:ReceiveMessage'],
resources: [queue.queueArn],
}),
],
})
)

//create a CDK instance of our Amplify Lambda
const orderQueuePoller = lambda.Function.fromFunctionAttributes(
this,
'orderQueuePoller',
{
functionArn: cdk.Fn.ref(retVal.function.orderQueuePoller.Arn),
role: lambdaRole,
}
)

//add the queue as an event that the our Lambda will listen for
orderQueuePoller.addEventSource(new SqsEventSource(queue))
}
}
```

Note that I have to also add a policy to my function to poll events from SQS here as well to satisfy CDK's type definition.

### Expected behavior

I would expect the queue and Lambda to be connected so that the Lambda can receive events.

I believe it would be helpful for the roleARN to be exposed as the `AmplifyDependentResourcesAttributes` file.

### Reproduction steps

_in section describing the bug_

### GraphQL schema(s)

```graphql
# Put schemas below this line

```

### Log output

```
# Put your logs below this line

```

### Additional information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the cdk-stack.ts setup, custom-policies.json, and the generated AmplifyDependentResourcesAttributes used by amplify push. Reproduce the SQS event-source attachment with the Amplify Lambda and inspect the reported CloudFormation or CDK failure. Done means the queue and Lambda deploy together and the Lambda can receive SQS events.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
backend, cli, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.