Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apigatewayv2-integrations: Add support for SQS-SendMessage integration #24785

Closed
1 of 2 tasks
DaWyz opened this issue Mar 25, 2023 · 10 comments · Fixed by #29646
Closed
1 of 2 tasks

apigatewayv2-integrations: Add support for SQS-SendMessage integration #24785

DaWyz opened this issue Mar 25, 2023 · 10 comments · Fixed by #29646
Labels
@aws-cdk/aws-apigatewayv2-integrations Related to AWS APIGatewayv2 Integrations effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@DaWyz
Copy link
Contributor

DaWyz commented Mar 25, 2023

Describe the feature

ApiGatewayv2 HTTP API integrates with SQS to redirect the request into a queue. It would be nice to have CDK implement this. See documentation on SQS-SendMessage mapping.

Use Case

I want an easy way to use an SQS Queue as a proxy for my HTTP API.

Proposed Solution

Implement the construct similarly to the lambda integration.

It would look like the following:

    const stack = new Stack();
    const api = new HttpApi(stack, 'HttpApi');
    const queue = DefaultQueue(stack, 'Queue');
    new HttpRoute(stack, 'LambdaProxyRoute', {
      httpApi: api,
      integration: new HttpSqsQueueIntegration('Integration', queue, {
        parameterMapping: new ParameterMapping()
          .appendHeader('header2', MappingValue.requestHeader('header1'))
          .removeHeader('header1'),
      }),
      routeKey: HttpRouteKey.with('/pets'),
    });

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.70.0

Environment details (OS name and version, etc.)

Ubuntu 20

@DaWyz DaWyz added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Mar 25, 2023
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2-integrations Related to AWS APIGatewayv2 Integrations label Mar 25, 2023
@pahud
Copy link
Contributor

pahud commented Mar 27, 2023

Yes this would be awesome! We'd be glad to review your PR when it is ready. Thank you.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Mar 27, 2023
@DaWyz
Copy link
Contributor Author

DaWyz commented Mar 27, 2023

@pahud , I just realized there are more than a single SQS integration type:

Should we:

  • build a single construct that covers all the cases or a construct per type?
  • for a single construct, should we cover all cases in this PR?
  • Do we want some validation on the Parameters?

@pahud
Copy link
Contributor

pahud commented Mar 27, 2023

Hi @DaWyz

This will need discussion with the core team maintainers, I would suggest you create a PR draft and describe your API design in the description and discuss with the maintainer in the PR draft before you finalize it.

@benm5678
Copy link

benm5678 commented May 9, 2023

In case it helps give a start, here's how I did it for the SendMessage:

Usage:

httpApi.addRoutes({
    path: `/sendMessage`,
    methods: [gw2.HttpMethod.POST],
    integration: new HttpSqsSendIntegration(this, 'MySQSSendIntegrationId', myQueue, {
      messageAttributes: '{ "test": { "DataType": "String", "StringValue": "val1" } }'
    }),
  });

Extended class:

import * as iam from 'aws-cdk-lib/aws-iam';
import * as gw2 from '@aws-cdk/aws-apigatewayv2-alpha';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import { Construct } from 'constructs';

export interface HttpSqsSendIntegrationProps {
  readonly messageBody?: string;
  readonly messageAttributes?: string;
}

export class HttpSqsSendIntegration extends gw2.HttpRouteIntegration {
  private readonly sqs: sqs.Queue;
  private readonly role: iam.Role;
  private readonly props?: HttpSqsSendIntegrationProps;
  bind(options: gw2.HttpRouteIntegrationBindOptions): gw2.HttpRouteIntegrationConfig {
    return {
      type: gw2.HttpIntegrationType.AWS_PROXY,
      subtype: gw2.HttpIntegrationSubtype.SQS_SEND_MESSAGE,
      payloadFormatVersion: gw2.PayloadFormatVersion.VERSION_1_0,
      parameterMapping: new gw2.ParameterMapping()
        .custom('QueueUrl', this.sqs.queueUrl)
        .custom('MessageBody', this.props?.messageBody ?? '$request.body')
        .custom('MessageAttributes', this.props?.messageAttributes ?? ''),
      credentials: gw2.IntegrationCredentials.fromRole(this.role),
    };
  }

  constructor(scope: Construct, id: string, sqs: sqs.Queue, props?: HttpSqsSendIntegrationProps) {
    super(id);
    this.sqs = sqs;
    this.props = props;
    this.role = new iam.Role(scope, "HTTP-API-Role", {
      assumedBy: new iam.ServicePrincipal("apigateway.amazonaws.com"),
      inlinePolicies: {
        'sqs-send': new iam.PolicyDocument({
          statements: [
            new iam.PolicyStatement({
              actions: ['sqs:SendMessage'],
              resources: [sqs.queueArn],
              effect: iam.Effect.ALLOW,
            }),
          ],
        }),
      },
    });
  }
}

@clutteralx
Copy link

Thank you for sharing your implementation @benm5678!
I have been getting this error when trying to add a route to a HttpApi using your HttpSqsSendIntegration:
firefox_jbkJtzWfeY

I haven't seen anyone else have this Mapping expression missing source or destination error.

@tomgelmersio
Copy link

@clutteralx This might be because, in my experience, the MessageAttributes cannot be an empty string. Try removing the mapping entirely if there is no value.

@clutteralx
Copy link

@clutteralx This might be because, in my experience, the MessageAttributes cannot be an empty string. Try removing the mapping entirely if there is no value.

That was the issue. It works perfectly now. Thanks so much 🎉

@nicklasweasel
Copy link

Is this on any roadmap? It would be nice to have CDK support for stuff you can do in the console

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

1 similar comment
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-apigatewayv2-integrations Related to AWS APIGatewayv2 Integrations effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants