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

Add basic serverless config #4

Open
wants to merge 1 commit into
base: main
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
4 changes: 3 additions & 1 deletion import-service/handler.js
Original file line number Diff line number Diff line change
@@ -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
};
3 changes: 3 additions & 0 deletions import-service/handlers/catalogBatchProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = async event => {

};
17 changes: 17 additions & 0 deletions import-service/handlers/importFileParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions import-service/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
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:
Expand All @@ -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

17 changes: 11 additions & 6 deletions product-service/db/connect.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
7 changes: 6 additions & 1 deletion product-service/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down