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

Adding support for FIFO queues along with message deduplication support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions fifo-queue-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Resources:
SQSQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: STAGE_NAME-QUEUE_NAME
FifoQueue: FIFO_TYPE
ContentBasedDeduplication: CONTENT_BASED_DEDUPLICATION

ProxyApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: STAGE_NAME-QUEUE_NAME-ApiGateway

APIGatewayResource:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Fn::GetAtt:
- "ProxyApi"
- "RootResourceId"
PathPart: "API_ENDPOINT"
RestApiId:
Ref: ProxyApi

SQSAPIMethod:
Type: AWS::ApiGateway::Method
DependsOn: SQSQueue
Properties:
RestApiId:
Ref: ProxyApi
ResourceId:
Ref: APIGatewayResource
HttpMethod: "POST"
MethodResponses:
-
StatusCode: "200"
ResponseParameters:
"method.response.header.Access-Control-Allow-Origin": true
AuthorizationType: "NONE"
Integration:
Type: AWS
Credentials: !Sub "${APIGatewaySQSIAM.Arn}"
RequestParameters:
"integration.request.header.Content-Type": "'application/x-www-form-urlencoded'"
IntegrationHttpMethod: POST
RequestTemplates:
"application/json": "Action=SendMessage&MessageBody=$input.body&MessageDeduplicationId=$input.params('MessageDeduplicationId')&MessageGroupId=$input.params('MessageGroupId')"
PassthroughBehavior: Never
IntegrationResponses:
-
StatusCode: "200"
ResponseParameters:
"method.response.header.Access-Control-Allow-Origin": "'*'"
ResponseTemplates:
"application/json": ""
Uri: !Sub arn:aws:apigateway:us-east-1:sqs:path/${AWS::AccountId}/${SQSQueue.QueueName}

APIGatewayDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: SQSAPIMethod
Properties:
RestApiId: !Sub "${ProxyApi}"
StageName: STAGE_NAME

APIGatewaySQSIAM:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action: sts:AssumeRole
Policies:
-
PolicyName: STAGE_NAME-QUEUE_NAME-policy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Resource: "*"
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- Effect: Allow
Resource:
- !Sub "${SQSQueue.Arn}"
Action:
- "sqs:SendMessage"

Outputs:
ApiGwUrl:
Value: !Sub "https://${ProxyApi}.execute-api.${AWS::Region}.amazonaws.com/STAGE_NAME/API_ENDPOINT"
28 changes: 24 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,48 @@ class ServerlessApiGWSqsPlugin {
}
beforeDeployResources() {
this.setCloudFormation()

const fifoQueueYaml = 'fifo-queue-template.yml';
const standardQueueYaml = 'standard-queue-template.yml';
const templateType = this.serverless.service.custom.apiGwSqs.fifoQueue ? fifoQueueYaml :standardQueueYaml;

return new Promise((resolve, reject) => {
fs.readFile(__dirname + '/' + 'template.yml', 'utf8', (err, contents) => {
fs.readFile(__dirname + '/' + templateType , 'utf8', (err, contents) => {
var stackName = this.getStackName(this.options.stage, this.serverless.service.service)
var apiEndpoint = this.serverless.service.custom.apiGwSqs.apiEndpoint
var queueName = this.serverless.service.custom.apiGwSqs.queueName
var fifoQueueType = this.serverless.service.custom.apiGwSqs.fifoQueue
var contentBasedDeduplication = this.serverless.service.custom.apiGwSqs.contentBasedDeduplication
var replaceStageName = new RegExp('STAGE_NAME', 'g');
var replaceApiEndpoint = new RegExp('API_ENDPOINT', 'g');
var replaceQueueName = new RegExp('QUEUE_NAME', 'g');
var replaceFifoType = new RegExp('FIFO_TYPE', 'g');
var replaceContentDeduplication = new RegExp('CONTENT_BASED_DEDUPLICATION')
contents = contents.replace(replaceStageName, this.options.stage);
contents = contents.replace(replaceApiEndpoint, apiEndpoint);
contents = contents.replace(replaceQueueName, queueName);
contents = contents.replace(replaceFifoType, fifoQueueType);
contents = contents.replace(replaceContentDeduplication, contentBasedDeduplication);

var params = {
Capabilities: [
'CAPABILITY_IAM'
],
StackName: stackName,
TemplateBody: contents,
};
if (queueName.includes(".")) {
console.log("[CodeRecipe ApiGW SQS Plugin] QueueName Error: Can only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length")
reject()

const fifoName = '.fifo';
if (fifoQueueType && !queueName.endsWith(fifoName)) {
console.log("[CodeRecipe ApiGW SQS Plugin] QueueName Error: Fifo Queues MUST end in '.fifo' eg 'testQueue.fifo'. Remember to only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length");
reject();
}

if (fifoQueueType === undefined && queueName.includes(".")) {
console.log("[CodeRecipe ApiGW SQS Plugin] QueueName Error: Can only include alphanumeric characters, hyphens, or underscores. 1 to 80 in length");
reject();
}

this.cloudformation.createStack(params, (err, data) => {
if (err) {
if(err.code == 'AlreadyExistsException') {
Expand Down
2 changes: 1 addition & 1 deletion template.yml → standard-queue-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Resources:
"integration.request.header.Content-Type": "'application/x-www-form-urlencoded'"
IntegrationHttpMethod: POST
RequestTemplates:
"application/json": "Action=SendMessage&MessageBody=$input.body"
"application/json": "Action=SendMessage&MessageBody=$input.body&MessageDeduplicationId=$input.params('MessageDeduplicationId')&MessageGroupId=$input.params('MessageGroupId')"
PassthroughBehavior: Never
IntegrationResponses:
-
Expand Down