【AWS】Lambda+SQS로 사일로+풀 모델을 실현하기 위한 디스패처 구성 검토
계기

현재 분석
3. 사일로 + 풀 모델을 목표로 하고 싶다.
검증에서 목표로 하는 구성

구축Step
1. 요청 수신 SQS와 Dispatcher용 Lambda를 연결



index.js
exports.handler = async function(event, context) {
event.Records.forEach(record => {
const { body } = record;
console.log(body);
});
return {};
};




2. Dispatcher용 Lambda에서 CompanyA용 SQS로 메시지 저장


index.js
const aws = require('aws-sdk');
aws.config.region = 'ap-northeast-1';
const sqs = new aws.SQS({apiVersion: '2012-11-05'});
const QUEUE_URL = 'https://sqs.ap-northeast-1.amazonaws.com/XXXXXXXXXX/companyA.fifo';
exports.handler = async function(event, context) {
const recs = event.Records;
for (let rec of recs){
const body = rec.body;
console.log(body);
const params = {
MessageBody: body,
QueueUrl: QUEUE_URL,
MessageGroupId:rec.attributes.MessageGroupId,
DelaySeconds: 0
};
console.log(params);
const result = await sqs.sendMessage(params).promise();
console.log(result)
}
};



3. Dispatcher용 Lambda에서 수신 메시지에 따라 저장


index.js
const aws = require('aws-sdk');
aws.config.region = 'ap-northeast-1';
const sqs = new aws.SQS({apiVersion: '2012-11-05'});
const companyA_QUEUE_URL = 'https://sqs.ap-northeast-1.amazonaws.com/XXXXXXXXXX/companyA.fifo';
const companyOTHERS_QUEUE_URL = 'https://sqs.ap-northeast-1.amazonaws.com/XXXXXXXXXX/companyOTHER.fifo';
let queue_url = '';
exports.handler = async function(event, context) {
const recs = event.Records;
for (let rec of recs){
const body = rec.body;
console.log(body);
const body_parse = JSON.parse(body);
if(body_parse.company_code === '0001'){
queue_url = companyA_QUEUE_URL;
} else {
queue_url = companyOTHERS_QUEUE_URL;
}
const params = {
MessageBody: body,
QueueUrl: queue_url,
MessageGroupId:rec.attributes.MessageGroupId,
DelaySeconds: 0
};
console.log(params);
const result = await sqs.sendMessage(params).promise();
console.log(result)
}
};





참고 사이트
Reference
이 문제에 관하여(【AWS】Lambda+SQS로 사일로+풀 모델을 실현하기 위한 디스패처 구성 검토), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kei1-dev/items/c48c30cd8faf090742a3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)