From ebb510f0652c3bb571835a2c433172c153a89263 Mon Sep 17 00:00:00 2001 From: Artem_Blazhko Date: Sun, 23 May 2021 10:44:55 +0300 Subject: [PATCH] Add basic serverless config --- import-service/handler.js | 4 +- .../handlers/catalogBatchProcess.js | 3 ++ import-service/handlers/importFileParser.js | 17 +++++++ import-service/serverless.yml | 47 +++++++++++++++++++ product-service/db/connect.js | 17 ++++--- product-service/serverless.yml | 7 ++- 6 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 import-service/handlers/catalogBatchProcess.js diff --git a/import-service/handler.js b/import-service/handler.js index e942205..c1a44d6 100644 --- a/import-service/handler.js +++ b/import-service/handler.js @@ -1,8 +1,10 @@ 'use strict'; const importProductFile = require("./handlers/importProductsFile"); const importFileParser = require("./handlers/importFileParser"); +const catalogBatchProcess = require("./handlers/catalogBatchProcess"); module.exports = { importProductFile, - importFileParser + importFileParser, + catalogBatchProcess }; diff --git a/import-service/handlers/catalogBatchProcess.js b/import-service/handlers/catalogBatchProcess.js new file mode 100644 index 0000000..d2a5f3a --- /dev/null +++ b/import-service/handlers/catalogBatchProcess.js @@ -0,0 +1,3 @@ +module.exports = async event => { + +}; diff --git a/import-service/handlers/importFileParser.js b/import-service/handlers/importFileParser.js index 0bcf06a..cc30479 100644 --- a/import-service/handlers/importFileParser.js +++ b/import-service/handlers/importFileParser.js @@ -5,6 +5,8 @@ const { BUCKET } = require("../constants"); module.exports = async event => { try { const s3 = new AWS.S3({ region: "eu-west-1" }); + const sqs = new AWS.SQS(); + await Promise.all(event.Records.map(record => { const results = []; console.log("Record key: ", record.s3.object.key); @@ -24,6 +26,21 @@ module.exports = async event => { return new Promise((resolve, reject) => { s3.upload(parsedParams, async (err, data) => { console.log("Results: ", results); + await Promise.all(results.map(async product => { + try { + const param = { + MessageBody: JSON.stringify(product), + QueueUrl: process.env.SQS_URL, + }; + + console.log('SEND MESSAGE TO QUEUE', param); + await sqs.sendMessage(param).promise(); + console.log('SUCCESSFULLY SENT'); + } catch (e) { + console.log('FAILURE SENT', e); + } + })); + s3Stream.destroy(); if (err) { return reject(err); } return resolve(data); diff --git a/import-service/serverless.yml b/import-service/serverless.yml index b362fe7..b907721 100644 --- a/import-service/serverless.yml +++ b/import-service/serverless.yml @@ -18,6 +18,42 @@ provider: Action: "s3:*" Resource: - "arn:aws:s3:::import-service-s3-aws/*" + - Effect: "Allow" + Action: "sns:*" + Resource: + Ref: SNSTopic + +resources: + Resources: + CatalogItemsQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: product-queue + SNSTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: NEW_PRODUCT_NOTIFIER + SNSSubscription: + Type: AWS::SNS::Subscription + Properties: + Endpoint: artem_blazhko@epam.com + Protocol: email + TopicArn: + Ref: SNSTopic + FilterPolicy: + status: + - success + Outputs: + CatalogItemsQueueUrl: + Value: + Ref: CatalogItemsQueue + Export: + Name: CatalogItemsQueueUrl + CatalogItemsQueueArn: + Value: + Fn::GetAtt: [CatalogItemsQueue, Arn] + Export: + Name: CatalogItemsQueueArn functions: import-product-file: @@ -41,3 +77,14 @@ functions: rules: - prefix: uploaded/ existing: true + + catalog-batch-process: + handler: handler.catalogBatchProcess + events: + - sqs: + batchSize: 5 + arn: + Fn::GetAtt: + - CatalogItemsQueue + - Arn + diff --git a/product-service/db/connect.js b/product-service/db/connect.js index 5d863dc..14d2058 100644 --- a/product-service/db/connect.js +++ b/product-service/db/connect.js @@ -1,13 +1,18 @@ import { Client } from 'pg'; export const connectDb = async () => { - const { PG_HOST, PG_PORT, PG_DATABASE, PG_USERNAME, PG_PASSWORD } = process.env; + //const { PG_HOST, PG_PORT, PG_DATABASE, PG_USERNAME, PG_PASSWORD } = process.env; const client = new Client({ - host: PG_HOST, - port: PG_PORT, - database: PG_DATABASE, - user: PG_USERNAME, - password: PG_PASSWORD, + //host: PG_HOST, + //port: PG_PORT, + //database: PG_DATABASE, + //user: PG_USERNAME, + //password: PG_PASSWORD, + host: 'shop-database.cnmsbdj8de61.eu-west-1.rds.amazonaws.com', + port: 5432, + database: 'shop_database', + user: 'master', + password: '2GZU4OYVFEUVs2zaHXJF', application_name: "aws_shop", connectionTimeoutMillis: 5000, ssl: { diff --git a/product-service/serverless.yml b/product-service/serverless.yml index a123c65..8cf1112 100644 --- a/product-service/serverless.yml +++ b/product-service/serverless.yml @@ -36,7 +36,12 @@ custom: # - "/*" # you can define service wide environment variables here - + environment: + PG_HOST: shop-database.cnmsbdj8de61.eu-west-1.rds.amazonaws.com + PG_PORT: 5432 + PG_DATABASE: shop_database + PG_USERNAME: master + PG_PASSWORD: 2GZU4OYVFEUVs2zaHXJF functions: getProductsList: