Open DaWyz opened 1 year ago
Yes this would be awesome! We'd be glad to review your PR when it is ready. Thank you.
@pahud , I just realized there are more than a single SQS integration type:
Should we:
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.
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,
}),
],
}),
},
});
}
}
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
:
I haven't seen anyone else have this Mapping expression missing source or destination
error.
@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 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 🎉
Is this on any roadmap? It would be nice to have CDK support for stuff you can do in the console
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:
Other Information
No response
Acknowledgements
CDK version used
2.70.0
Environment details (OS name and version, etc.)
Ubuntu 20