๋ฐ˜์‘ํ˜•

๐ŸŽฏ AWS ๋‚ด์— ๊ตฌ์ถ•ํ•œ Lambda์—์„œ S3์— ์ ‘๊ทผํ•˜์—ฌ ํŒŒ์ผ ์ฝ๊ธฐ

โœ… Lambda, S3๋ฅผ ํ•™์Šต ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๐Ÿ‘‰ S3 ๋ฒ„ํ‚ท ์ƒ์„ฑ

์•„๋ž˜์™€ ๊ฐ™์ด S3 ๋ฒ„ํ‚ท์„ ๋งŒ๋“ค์–ด๋ณด์ž.

s3๋กœ ์ ‘์†

 

๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ๋ฅผ ํด๋ฆญ

 

๋ฒ„ํ‚ท ์ด๋ฆ„, ๋ฆฌ์ „ ์„ค์ • (์ด๋ฆ„์€ ์ค‘๋ณต ๋ถˆ๊ฐ€, ๋ฆฌ์ „์€ ์„œ์šธ๋กœ) (โ›” lambda๋ฅผ lamdba๋กœ ์ž˜๋ชป ์ ์€ ๋ฒ„ํ‚ท์ด๋ฏ€๋กœ ์ฃผ์˜)
๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„œ๋ฒ„ ์ธก ์•”ํ˜ธํ™” ํ™œ์„ฑ ํ›„ SSE-S3 ํด๋ฆญ ํ›„ ๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ ์‹คํ–‰

 

 

๐Ÿ‘‰ S3์— ํŒŒ์ผ ์—…๋กœ๋“œ

๋žŒ๋‹ค์—์„œ ์“ธ ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋กœ๋“œ ํ•ด๋ณด์ž  

 

์—…๋กœ๋“œ ๋ฒ„ํŠผ ํด๋ฆญ

 

hello world๋ผ๊ณ  ๋งŒ๋“  test.txt๋ฅผ ์—…๋กœ๋“œ

 

 

๐Ÿ‘‰ IAM Role ๋งŒ๋“ค๊ธฐ

Lambda๋ฅผ ์œ„ํ•œ IAM Role์„ ๋งŒ๋“ค์–ด S3 ์—‘์„ธ์Šค ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜์ž

IAM ์ ‘๊ทผ

 

์•ก์„ธ์Šค ๊ด€๋ฆฌ → ์—ญํ•  → ์—ญํ•  ๋งŒ๋“ค๊ธฐ ์„ ํƒ

 

Lambda๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ˆ AWS ์„œ๋น„์Šค → Lambda → ๋‹ค์Œ ์„ ํƒ

 

AmazonS3FullAccess ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋„๋ก ํ•œ๋‹ค.

 

์—ญํ•  ์ด๋ฆ„์„ ์„ค์ •

 

์—ญํ•  ์ƒ์„ฑ ํด๋ฆญ

 

๐Ÿ‘‰ Lambda ํ•จ์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ

์ด์ œ S3์™€ ์—ฐ๊ฒฐ ํ•  lambda๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

lambda ์„ ํƒ

 

ํ•จ์ˆ˜ ์ƒ์„ฑ ํด๋ฆญ

 

ํ•จ์ˆ˜ ์ด๋ฆ„(Lambda ์ด๋ฆ„)์„ ์ •ํ•ด์ฃผ๊ณ  ์—ญํ• (role)์€ crocusS3FullAccess๋กœ ์ •ํ•ด์ค€๋‹ค.

 

 

๐Ÿ‘‰ Lambda ํ•จ์ˆ˜ ์ž‘์„ฑํ•˜์—ฌ S3 ๋ฐ์ดํ„ฐ ํ˜ธ์ถœํ•˜๊ธฐ

์•„๋ž˜ ์ƒ˜ํ”Œ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ S3์— ์—ฐ๊ฒฐํ•ด๋ณด์ž.

 

https://docs.aws.amazon.com/ko_kr/AmazonS3/latest/userguide/example_s3_GetObject_section.html

 

AWS SDK๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Amazon S3 ๋ฒ„ํ‚ท์—์„œ ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ - Amazon Simple Storage Service

์ด ์„ค๋ช…์„œ๋Š” ํ‰๊ฐ€ํŒ ๋ฒ„์ „ SDK์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. SDK๋Š” ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ”„๋กœ๋•์…˜์—์„œ ์‚ฌ์šฉํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

docs.aws.amazon.com

 

import { GetObjectCommand } from "@aws-sdk/client-s3";
import { S3Client } from "@aws-sdk/client-s3";

const REGION = "ap-northeast-2"; //e.g. "us-east-1"
const s3Client = new S3Client({ region: REGION });

export const handler = async(event) => {
    const bucketParams = {
        Bucket: "crocus-lamdba-s3-test-bucket",
        Key: "test.txt"
    }

    try {
        // Create a helper function to convert a ReadableStream to a string.
        const streamToString = (stream) =>
          new Promise((resolve, reject) => {
            const chunks = [];
            stream.on("data", (chunk) => chunks.push(chunk));
            stream.on("error", reject);
            stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
          });
        
        // Get the object} from the Amazon S3 bucket. It is returned as a ReadableStream.
        const data = await s3Client.send(new GetObjectCommand(bucketParams));
        
        // Convert the ReadableStream to a string.
        const bodyContents = await streamToString(data.Body);
        return {
            statusCode: 200,
            body: JSON.stringify(bodyContents),
        };
    } catch (err) {
        console.log("Error", err);
    }

};

 

์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ ํ›„ deploy → test๋ฅผ ํ†ตํ•ด ์‹คํ–‰

 

test.txt์— hello world๋ฅผ ์ ์€ ๋‚ด์šฉ์ด ์ž˜ ๋‚˜ํƒ€๋‚จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•