์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- s3 log ์ ์ฅ
- nGrinder
- Codedeploy ์ค๋ฅ
- Entity
- aws ์ฟ ํฐ
- ํ๋ก๊ทธ๋๋จธ์ค ์ปฌ๋ฌ๋ง๋ถ
- private subnet ec2 ๋ก์ปฌ ์ ์
- ์๋ฒ ํฐ์ง๋ ๋์ปค ์ฌ์คํ
- s3 ์ด๋ฏธ์ง ์ ์ฅ
- prod docker-compose
- ์คํํ๋ ๋ฏธ์ค
- redis ์กฐํ
- ํ์ดํผ๋ฐ์ด์
- ์ ํจ์ค ์ค์ผ์ค๋ฌ
- redis ํ ์คํธ์ฝ๋
- ์๋ฐ
- ํ๋ก๊ทธ๋๋จธ์ค ํฉ์นํ์์๊ธ
- jvm ๋ฐ๋ฐ๋ฅ๊น์ง ํํค์น๊ธฐ
- AWS Certified Solutions Architect - Associate
- ํ๋ก๊ทธ๋๋จธ์ค
- docker-compose kafka
- aws saa ํฉ๊ฒฉ
- s3 ์ด๋ฏธ์ง ๋ค์ด๋ก๋
- ๋ค์ค ์ปจํ ์ด๋
- docker
- Kafka
- docker ps -a
- ์ ํจ์ค ๋น๋ ์ค๋ฅ
- JPA
- docker compose
- Today
- Total
๐๐ข๐๐ โ๐๐๐ ๐๐๐ก๐๐ ๐๐๐๐โง
[AWS] S3 + Lambda ๋ฅผ ํตํ ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง (macOS sharp ๋ชจ๋ ์ค๋ฅ, ๋ฌดํ ๋๋ค ํธ์ถ ์ค๋ฅ) ๋ณธ๋ฌธ
[AWS] S3 + Lambda ๋ฅผ ํตํ ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง (macOS sharp ๋ชจ๋ ์ค๋ฅ, ๋ฌดํ ๋๋ค ํธ์ถ ์ค๋ฅ)
๐คRyusun๐ค 2025. 3. 27. 19:23๋ฒํท ์์ฑ, ๋๋ค ์์ฑ, IAM ์ญํ ์์ฑ์ ๋ํ ์์ธํ ๋ด์ฉ์ ๋ค๋ฅธ ์ฌ์ดํธ์์ ๋ ์ ์ค๋ช ๋์ด ์์ผ๋ฏ๋ก, ์ฐธ๊ณ ํ์๋ฉด ๋๋ค.
https://oliveyoung.tech/2023-05-19/aws-lambda-resize/
AWS Lambda Image Resize ๋์ ๊ธฐ | ์ฌ๋ฆฌ๋ธ์ ํ ํฌ๋ธ๋ก๊ทธ
์ ๊ท ์ํ ํ๋ก์ ํธ์์ AWS Lambda ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง ์ ์ฉํ๊ธฐ
oliveyoung.tech
ํธ๋ฆฌ๊ฑฐ์ ์ ๋์ฌ๋ images/origin/ ์ผ๋ก ์ค์ ํ๋ค.

๋๋ค ์ฝ๋(node.js)
// dependencies
const AWS = require('aws-sdk'); // AWS SDK ๋ถ๋ฌ์ค๊ธฐ
const sharp = require('sharp'); // ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ถ๋ฌ์ค๊ธฐ
// S3 ํด๋ผ์ด์ธํธ ์์ฑ
const s3 = new AWS.S3();
exports.handler = async (event, context, callback) => {
// S3 ๋ฒํท ๋ฐ ๊ฐ์ฒด ํค ์ถ์ถ
const srcBucket = event.Records[0].s3.bucket.name;
const srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
const dstBucket = event.Records[0].s3.bucket.name;
// ์ด๋ฏธ์ง ํ์ฅ์ ํ์ธ
const typeMatch = srcKey.match(/\.([^.]*)$/);
if (!typeMatch) {
console.log("Could not determine the image type.");
return;
}
const imageType = typeMatch[1].toLowerCase();
if (imageType != "jpg" && imageType != "png") {
console.log(`Unsupported image type: ${imageType}`);
return;
}
// ๋ฆฌ์ฌ์ด์ฆ๋ ์ด๋ฏธ์ง ์ ์ฅ ๊ฒฝ๋ก ์ค์
const resizedKey = 'images/resized/' + srcKey.split('/').pop();
// ์๋ณธ ์ด๋ฏธ์ง S3์์ ๊ฐ์ ธ์ค๊ธฐ
try {
const params = { Bucket: srcBucket, Key: srcKey };
var origimage = await s3.getObject(params).promise();
const originalImageSize = origimage.Body.length;
console.log(`Original image size: ${originalImageSize} bytes`);
} catch (error) {
console.log(error);
return;
}
const width = 200; // ๋ฆฌ์ฌ์ด์ฆ ๋๋น ์ค์
try {
// ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง
var buffer = await sharp(origimage.Body).resize(width).toBuffer();
const resizedImageSize = buffer.length;
console.log(`Resized image size: ${resizedImageSize} bytes`);
} catch (error) {
console.log(error);
return;
}
try {
// ๋ฆฌ์ฌ์ด์ฆ๋ ์ด๋ฏธ์ง๋ฅผ S3์ ์
๋ก๋
const destparams = {
Bucket: dstBucket,
Key: resizedKey,
Body: buffer,
ContentType: "image"
};
// ์ด๋ฏธ์ง ์
๋ก๋ ์ํ
const putResult = await s3.putObject(destparams).promise();
} catch (error) {
console.log(error);
return;
}
console.log('Successfully resized ' + srcBucket + '/' + srcKey +
' and uploaded to ' + dstBucket + '/' + resizedKey);
};
S3์ ์ฌ์ง์ ์ ๋ก๋ํ๋ฉด CloudWatch ๋ก๊ทธ๋ฅผ ํตํด ์ด๋ฏธ์ง๊ฐ ์ ์์ ์ผ๋ก ๋ฆฌ์ฌ์ด์ง๋์ด ์ฒ๋ฆฌ๋์์์ ํ์ธํ ์ ์๋ค. S3์ resized ๋๋ ํ ๋ฆฌ์์๋ ๋ฆฌ์ฌ์ด์ง๋ ์ด๋ฏธ์ง๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ฅ๋์๋ค.



์ค๋ฅ & ํด๊ฒฐ ๊ณผ์
1. AWS Lambda์์ sharp ๋ชจ๋ ์ค๋ฅ
2025-03-26T12:24:49.529Z undefined ERROR Uncaught Exception
{
"errorType": "Error",
"errorMessage": "Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install",
"stack": [
"Error: Could not load the \"sharp\" module using the linux-x64 runtime",
"Possible solutions:",
"- Ensure optional dependencies can be installed:",
" npm install --include=optional sharp",
"- Ensure your package manager supports multi-platform installation:",
" See https://sharp.pixelplumbing.com/install#cross-platform",
"- Add platform-specific dependencies:",
" npm install --os=linux --cpu=x64 sharp",
"- Consult the installation documentation:",
" See https://sharp.pixelplumbing.com/install",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:113:9)",
" at Module._compile (node:internal/modules/cjs/loader:1554:14)",
" at Object..js (node:internal/modules/cjs/loader:1706:10)",
" at Module.load (node:internal/modules/cjs/loader:1289:32)",
" at Function._load (node:internal/modules/cjs/loader:1108:12)",
" at TracingChannel.traceSync (node:diagnostics_channel:322:14)",
" at wrapModuleLoad (node:internal/modules/cjs/loader:220:24)",
" at Module.require (node:internal/modules/cjs/loader:1311:12)",
" at require (node:internal/modules/helpers:136:16)",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:10:1)"
]
}
sharp๋ ๋ค์ดํฐ๋ธ ๋ฐ์ด๋๋ฆฌ ๋ชจ๋์ ์ฌ์ฉํ๋ ์ด๋ฏธ์ง ์ฒ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค. ์ด๋ ์ด๋ฏธ์ง ์ฒ๋ฆฌ๋ฅผ ์ํ ์ ์์ค ์์ ์ ๊ณ ์์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ปดํ์ผ๋ ๋ค์ดํฐ๋ธ ์ฝ๋๊ฐ ํฌํจ๋์ด ์๋ค. sharp ๋ชจ๋์ ์ด์ ์ฒด์ ์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ปดํ์ผ๋๋๋ฐ MacOS์์ ๋น๋๋ sharp ๋ชจ๋์ ๋ฐ์ด๋๋ฆฌ ์ฝ๋๊ฐ ๋ค๋ฅด๊ฒ ์ปดํ์ผ๋๊ธฐ ๋๋ฌธ์ Linux ํ๊ฒฝ์์ ์คํ๋๋ AWS Lambda์์๋ ์ ์์ ์ผ๋ก ๋์ํ์ง ์๋๊ฒ์ด๋ค.
chatgpt ์ ๋ฌผ์ด๋ดฃ๋๋ 2๊ฐ์ง ํด๊ฒฐ๋ฐฉ๋ฒ์ ์ ์ํ๊ณ , ๋๊ฐ ๋ค ์๋ํด๋ดค๋ค.

1. Lambda Layer ์ฌ์ฉ
2. Docker๋ฅผ ์ด์ฉํ ๋น๋
AWS Lambda์์ sharp ๋ชจ๋์ ์ฌ์ฉํ๊ธฐ ์ํด Lambda Layer๋ฅผ ์ถ๊ฐํด๋ณด๊ณ Docker๋ฅผ ์ด์ฉํด ๋น๋ํ์ฌ ๋ฐฐํฌํ์ง๋ง ์ฌ์ ํ ๋์ผํ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.

ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์๋ ๋ ํฌ์งํ ๋ฆฌ์์ ์ ๊ณตํ๋ zip ํ์ผ์ ๋ค์ด๋ก๋ํ ํ, Lambda ์ฝ๋๋ก ์ ๋ก๋ํ๊ณ , index.js ํ์ผ์ ์์ ํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
https://github.com/dmswn1004/AWS-Lambda-S3-Image-Resizing
GitHub - dmswn1004/AWS-Lambda-S3-Image-Resizing: AWS Lambda ํธ๋ฆฌ๊ฑฐ + S3 ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง ํจ์
AWS Lambda ํธ๋ฆฌ๊ฑฐ + S3 ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง ํจ์. Contribute to dmswn1004/AWS-Lambda-S3-Image-Resizing development by creating an account on GitHub.
github.com
๋๋ ํด๋น zip ํ์ผ์ ๋ค์ด๋ก๋ํ, ์ ๋ก๋ ํ์๊ณ ์ฝ๋๋ฅผ ์ง์ ๋ฐ๊ฟ์ฃผ์ ๋ ๋ฉ๋๋ค.
๋ง์ฝ ์ ๋ฐฉ๋ฒ์ผ๋ก๋ ํด๊ฒฐ๋์ง ์๋๋ค๋ฉด, ์๋์ฐ ์ด์์ฒด์ ์์ ๋ค์ ์๋ํด๋ณด๋ ๊ฒ๋ ํ๋์ ๋ฐฉ๋ฒ์ด๋ค. ์๋์ฐ ํ๊ฒฝ์์ ๋น๋ํ sharp ๋ชจ๋์ด Lambda์ ๋ฆฌ๋ ์ค ํ๊ฒฝ๊ณผ ํธํ๋๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
[์ฐธ๊ณ ํ๋ฉด ์ข์ ๋ธ๋ก๊ทธ]
https://velog.io/@dgh06175/AWS-LambdaEdge-sharp-trouble-shooting
AWS lambda ์์ sharp ๋ชจ๋ ์ฌ์ฉ ์ Could not load the \"sharp\" module using the linux-x64 ์๋ฌ ํด๊ฒฐ ๋ฐฉ๋ฒ
์ฝ์ง๋ก ์ฝ์ง ๋๊ธฐ
velog.io
2. S3 ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ง ํ ๋ฌดํ ์ ๋ก๋ ์ค๋ฅ
S3์์ ์ด๋ฏธ์ง๋ฅผ ๋ฆฌ์ฌ์ด์งํ์ฌ ์ ๋ก๋ํ ๋, source ๋ฒํท๊ณผ destination ๋ฒํท์ด ๋์ผํ ๊ฒฝ์ฐ ๋ฌดํ ์ ๋ก๋ ํ์์ด ๋ฐ์ํ๋ค. ์ด ๋ฌธ์ ๋ ๋ฆฌ์ฌ์ด์ง๋ ์ด๋ฏธ์ง๊ฐ ๋ค์ ์๋ณธ ๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋์ด ํธ๋ฆฌ๊ฑฐ๊ฐ ๊ณ์๋๋ ์ํฉ์ด์๋ค.

ํด๊ฒฐ ๋ฐฉ๋ฒ
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด, ๋ฆฌ์ฌ์ด์ง๋ ์ด๋ฏธ์ง๋ฅผ ์๋ณธ ๋๋ ํ ๋ฆฌ์ธ origin ์์ resized ๋๋ ํฐ๋ฆฌ๋ก ์ ์ฅํ๋๋ก ์ฝ๋๋ฅผ ์์ ํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์๋ณธ ๋๋ ํ ๋ฆฌ์ ์ด๋ฏธ์ง๋ ๋ฆฌ์ฌ์ด์ง ์์ ์ ๊ฑฐ์ณ resized ๋๋ ํ ๋ฆฌ์ ์ ์ฅ๋๋ฉฐ, ๋ ์ด์ ์๋ณธ ์ด๋ฏธ์ง์ ๋ํ ๋ฆฌ์ฌ์ด์ง ์์ ์ด ๋ฐ๋ณต๋์ง ์๊ฒ ๋๋ค.
๋๋ค ํจ์ ์ฝ๋์ resized ๋๋ ํ ๋ฆฌ๋ฅผ ๋ณ๋๋ก ์ค์ ํจ์ผ๋ก์จ ๋ฌดํ ์ ๋ก๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์๋ค.
// ๋ฆฌ์ฌ์ด์ฆ๋ ์ด๋ฏธ์ง ์ ์ฅ ๊ฒฝ๋ก ์ค์
const resizedKey = 'images/resized/' + srcKey.split('/').pop();