Skip to content

Commit

Permalink
Merge pull request #1229 from minenam/minenam
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jul 15, 2023
2 parents c2c27f0 + 8d36c49 commit d74f786
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions minenam/2023-07-12-AWS-Lambda-and-AWS-SAM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# AWS Lambda 와 AWS SAM을 ν™œμš©ν•œ Local Test

## μ†Œκ°œ

AWSμ—μ„œ μ½”λ“œλ₯Ό μ‹€ν–‰ν•  수 μžˆλŠ” μ„œλΉ„λ¦¬μŠ€ μ»΄ν“¨νŒ… μ„œλΉ„μŠ€λ‘œ, κ°œλ°œμžκ°€ μ½”λ“œ λ‹¨μœ„λ‘œ μž‘μ—…μ„ κ΅¬ν˜„ν•˜κ³  이벀트 기반 μž‘μ—…μ— μ ν•©ν•˜λ‹€.

> 예. 데이터 μ—…λ°μ΄νŠΈ, 파일 λ³€ν™˜, μ•Œλ¦Ό 전솑 λ“±
### μž₯점

- 관리 및 μœ μ§€λ³΄μˆ˜ 용이: μ„œλ²„λ₯Ό ν”„λ‘œλΉ„μ €λ‹ν•˜κ±°λ‚˜ 관리할 ν•„μš” 없이 μ½”λ“œλ₯Ό μ‹€ν–‰ν•  수 μžˆλ‹€.
- μ €λ ΄ν•œ λΉ„μš©: μ‚¬μš©ν•œ λ¦¬μ†ŒμŠ€μ— λŒ€ν•΄μ„œ λΉ„μš©μ„ μ§€λΆˆν•˜κΈ° λ•Œλ¬Έμ— μƒλŒ€μ μœΌλ‘œ νŠΈλž˜ν”½μ΄ λ§Žμ§€ μ•ŠμœΌλ©΄ 보닀 νš¨μœ¨μ μ΄λ‹€.
- ν™•μž₯μ„±κ³Ό μœ μ—°μ„±: νŠΈλž˜ν”½μ΄ λ§Žμ•„μ§€λ©΄ μžλ™μœΌλ‘œ ν™•μž₯되기 λ•Œλ¬Έμ— μ„œλ²„λ₯Ό 관리할 ν•„μš”κ°€ μ—†λ‹€.
- λΉ λ₯Έ 배포: κ°„λ‹¨νžˆ μ½”λ“œλ₯Ό μ—…λ‘œλ“œν•˜κ³  ꡬ성을 μ„€μ •ν•˜λ©΄ μ‹ μ†ν•˜κ²Œ 배포λ₯Ό ν•  수 μžˆλ‹€.

#### 단점

- μ œν•œλœ ν™˜κ²½: μ„œλ²„λ¦¬μŠ€ ν™˜κ²½μ΄ μ•„λ‹Œ, νŠΉμ • μ„œλ²„ 섀정이 ν•„μš”ν•œ 인프라 기반의 ν™˜κ²½μ—μ„œλŠ” μ‚¬μš©ν•˜κΈ° μ–΄λ €μšΈ 수 μžˆλ‹€.
- λ³΅μž‘μ„±: λ‹¨μˆœν•˜μ§€ μ•Šκ³  λ³΅μž‘ν•œ λΉ„μ¦ˆλ‹ˆμŠ€ λ‘œμ§μ„ μ²˜λ¦¬ν•˜λŠ” 데 μ ν•©ν•˜μ§€ μ•Šλ‹€.

## 둜컬 ν™˜κ²½μ—μ„œ AWS Lambda Function ν…ŒμŠ€νŠΈν•˜κΈ°

### AWS Lambda Console

### AWS SAM(Severless Application Model)

1. AWS CLI & Docker μ„€μΉ˜
- AWS SAM 을 μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” AWS CLI와 Dockerλ₯Ό ν•„μˆ˜λ‘œ μ„€μΉ˜ν•΄μ•Ό ν•œλ‹€.
- [AWS CLI μ„€μΉ˜ κ°€μ΄λ“œ](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)
- [AWS SAM CLIλ₯Ό μœ„ν•œ Docker μ„€μΉ˜](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-docker.html)
2. Lambda Event Test μž‘μ„±
- Lambda ν•¨μˆ˜λ₯Ό ν…ŒμŠ€νŠΈν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ 이벀트λ₯Ό μž‘μ„±ν•œλ‹€. Lambda ν•¨μˆ˜μ— μ „λ‹¬λ˜λŠ” μž…λ ₯ λ°μ΄ν„°λ‘œ, μ•„λž˜μ˜ μ˜ˆμ‹œμ—μ„œλŠ” JSON ν˜•νƒœλ‘œ μž‘μ„±ν•œλ‹€.
```json
// events/event.json
{
"key1": "Hello",
"key2": "World"
}
```
3. Lambda Function ν•¨μˆ˜ μž‘μ„±

```typescript
// app/index.ts
export const handler = async (event, context) => {
const { key1, key2 } = event;
try {
const response = {
statusCode: 200,
body: JSON.stringify({
message: `${key1} ${key2}`,
}),
};

return response;
} catch (error) {
console.log(error);
return error;
}
};
```

4. AWS SAM 으둜 λ‘œμ»¬μ—μ„œ Lambda Function μ‹€ν–‰

```bash
# sam local invoke -e <ν…ŒμŠ€νŠΈ 이벀트 파일 경둜>
$ sam local invoke -e events/event.json

{"statusCode":200,"body":"{\"message\":\"Hello World\"}"}%
```

## 참고자료

- [AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html)

0 comments on commit d74f786

Please sign in to comment.