SlideShare a Scribd company logo
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
이상현, Vingle
Lambda@Edge를 이용한
Serverless로의 Migration & Multi-Region
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
이상현 Kurt Lee
Technical Leader, Vingle Inc
kurt@vingle.net
https://github.com/breath103
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Vingle, Interest Network
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Monthly Active User
Monthly Page View
App Store Rating
5 Milion
3 Billion
4.7
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Lambda@Edge
CDN + Edge Computing
Microservice
Lambda + API Gateway
Serverless
Microservice
Gradual
Migration
Multi-Region
(Cache + Edge)
Agenda
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
다루지 않는 내용
1. Microservice / monolith의 개념
2. 왜 Microservice로 옮겼나
3. 왜 Serverless로 옮겼나
4. API Gateway / Lambda / S3 / Athena 상세 설명
5. CI / CD 상세 설명
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Serverless
Microservice
Gradual
Migration
1. Legacy Application이 있고,
2. 새로운 Application이 있고,
3. 새로운 Application으로 기능을 하나씩 옮겨갈때
4. 어떻게 트래픽을 지능적으로 (어떤 Application으로 보낼지 판단해서)
5. 안정적으로, 점진적으로 Routing 할 것 인가?
6. 사람 4명으로, 기능을 계속 추가하면서
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Previously…
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
US-EAST-1
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
Notifications
Card
QnA
1. Microservice들의 응답을 조합 + Routing (API Gateway)
2. 아주 기본적인 유저 인증 (JWT ▷ user_id)
3. 옮기기 어려운 것들 (옛날 RDS라던가.. devise 라던가..)
US-EAST-1
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
1) 요청이 들어옴
2) 유저 인증
JWT ➡ userId
3) Microservice로 보냄
/me/notifications ➡ getNotifications(userId)
4) 응답 return
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Rails가 하는 일을 Microservice로 구현
(Lambda + API Gateway)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
aaa.excute-api.us-east-1.com
이걸 어떻게 배포하지?;;
(천천히?)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
aaa.excute-api.us-east-1.com
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
aaa.excute-api.us-east-1.com
• API Gateway를 target으로 지정 못
함
• URL Pattern으로만 routing 가능
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
aaa.excute-api.us-east-1.com
• Multiple Origin이 지원 안됨
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
aaa.excute-api.us-east-1.com
• Domain 이외의 정보는 모두 사용
불가
확 Reverse Proxy(Nginx) Cluster를 만들어버릴까...
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
Nginx Cluster
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Homo Faber : "Working Man"
Animal Laborans: "Working Animal"
Animal Laborans란 이름 그대로, 기계적이고 일상적으로 일을 하는 가축과 같은 사람을 뜻한다.
아렌트는 오펜하이머가 원자폭탄을 "달콤한 문제" 라고 표현하는 것이나, 아이히만(유대인 학살 시스템
설계자)이
얼마나 가스실을 "효율적"으로 만드는데 집착했는지 에서 이 이미지를 찾았다. 그들은 뭔가가 돌아가게
하는대에(making it work)만 몰입하며, 그로써 "일 그 자체"를 "일의 끝"으로 삼는다.
Homo Faber는 이와 대조되는 종류의 인간을 뜻한다. 원어는 라틴어로 "만드는 사람" 이지만, 아렌트는
좀더 분명한 이미지를 부여했다. 그녀에 따르면 Homo Faber는 노동과 노력의 판단자(Judge)이다. 이때
인간은 일에 몰입하는 존재이지만, 동시에 도덕과 판단의 주체이다. 이들은 뭔가를 생산하기를 멈추고,
다른 판단자들과 자신들이 하는 일에 대해 토론하고 판단할수 있다.
그렇기에, Animal Laborans이 "How"를 물을때 Homo Faber는 "Why"를 묻는다
- Richard Sennett, The Craftsman
문장이 길어지면 안읽는 사람들을 위한 4줄 요약:
1. 인간은 원래 만드는 것에 몰입하는 존재다
2. 몰입해서 일만하면 원자폭탄이나 가스실을 만든다
(그리고 심지어 너무 만족스러워한다)
3. 그러니 지금 하는일을 왜 하는지 판단하면서 해라
4. 안그러면 가축이랑 다른게 뭐냐
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CDN
Load
Balancer
EC2 Fleet
(Nginx + Rails)
Microservices
(Lambda + DynamoDB)
Feed
Interest
US-EAST-1
Notifications
Card
QnA
API Edge
(API Gateway + Lambda)
Nginx Cluster
1. Serverless로 가야함
2. 천천히 migration 하려면 reverse proxy가 필요
3. reverse proxy cluster를 앞에 만들자?
?? Serverless로 가려고 Cluster를 하나 더 만들자 ??
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
function proxy(req: Request) {
if (req.url == "/api/me/notifications" &&
req.headers.get("accept") == "application/json") {
return sendToAPIEdge(req);
} else {
return sendToRails(req);
}
이걸 Serverless로 돌리고 싶을 뿐인데..
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
때는 11월...
Lambda@Edge?
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Origi
n
America
Asia
Europe
Africa
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Lambda@Edge
1. Request에 따라 다른 Origin으로 보낸다거나
2. Request에 따라 CDN에서 바로 응답을 준다거나
3. CDN Cache에 넣기 전에 Origin Response를 변조한다던가
4. Cache된 Reponse를 주기전에 변조한다던가
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Reverse Proxy
(Serverless)
- Multiple Origin Servers
- Dynamic Routing
- Caching
- Managed Reverse Proxy
…
Web App
(Simple)
- CORS (OPTION request)
- 404 / 500 / down
- Redirection (www -> m)
- IP Blocks
…
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices
(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
CDN
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices
(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
CDN
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
import {
CustomOrigin, getRequestHeader, OriginRequest, OriginRequestEvent
} from "../../request";
// Origin-Request Event Handler
export function handler(request: OriginRequest) {
if (
request.uri === "/api/me/notifications"
) {
const apiEdge: CustomOrigin = {
port: 443,
domainName: "xxxxxxx.execute-api.us-east-1.amazonaws.com",
protocol: "https",
path: "/prod",
sslProtocols: ["TLSv1", "TLSv1.1"],
readTimeout: 30,
keepaliveTimeout: 30,
customHeaders: {},
};
request.origin = { custom: apiEdge };
// Override host to let APIGateway swallow this
request.headers.host = [
{ key: "host", value: apiEdge.domainName }
];
}
return request;
}
Proof of concept
API Edge로 보낼수 있는 URL인지
검사
맞으면 Request의 Origin을
API Edge로 변경
request를 return
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
그때는 몰랐습니다. 이게 겨우 시작일줄은...
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
1. The ultimate goal of backend team is to make Vingle service better. Anything “we” can do to
accomplish that is our job
2. Primary goal of backend team is to provide manageable, reliable, scalable, extensible backend
service.
3. There is no such a thing as “Perfect Architecture / Library / Pattern that solve every problems.”
Design Pattern is just a pattern. Remember "To a man with a hammer, everything looks like a nail."
Mixing patterns, or being untraditional, or being heretic is perfectly fine if it solves the given
problem and the situation.
4. Question all the rules and traditions. Test them and rewrite them if you can.
5. Get rid of things to “manage” as much as possible. Our time can be much more valuable when it’s
spent on building things, not monitoring and managing. Let machines do that.
6. API is the product we build, frontend developers are our customer.
7. “Any organization that designs a system will produce a design whose structure is a copy of the
organization's communication structure.”
Fundamental principles of our team
“관리” 해야 하는것을 최대한 없애라.
우리의 시간은 뭔가를 만들기 위해 쓰여질때 가장 가치있다.
“관리” 는 기계가 하게 해라.
Lint / Test / Deployment / Monitoring / Alert
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
문제 1) Lambda@Edge는 반드시 lambda의 “version”을 명시해야함.
arn:xxx:xxx:function-name:$LATEST (X)
arn:xxx:xxx:function-name:98 (✓)
즉 새로운 lambda 버젼을 올릴때마다, Cloudfront도 배포해야함
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Repository Setup
vingle-edge
Origin-Request
요청에 따라, 어떤 Origin으로 보낼지 분기 / 결정
Viewer-Request
요청에 따라, Edge에서 응답을 만들어 주는경우
function handler(request: OriginRequest):
OriginRequest
function handler(request: OriginRequest):
OriginRequest | OriginResponse
CloudFront Distribution
기본 Origin (Rails ELB) / Cache Policy
/ Cookie, Header forwarding.. etc
Continuous Integration
Push
1. Test
2. Lint
3. 새로운 Lambda Version을 올리고,
4. CloudFront에 해당 Lambda Version을 연결
- serverless-lambda-version plugin
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
예상치 못한 여정: Monitoring
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Lambda@Edge
1) Lambda@Edge는 원본 Lambda를 모든
CloudFront region에 복사하는식으로 동작
함
2) 각각의 복사본 이름은
(원본region).(원본 이름):(version)
3) 이때 복사된 Lambda각각은 일반적인
Lambda와 다를바 없이 CloudWatch 제공.
4) AWS console에서는 안보이는데,
filter 켜면 접근 가능
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Lambda@Edge - CloudWatch
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
CloudFront - Basic
1) BytesUploaded
2) BytesDownloaded
3) Requests
4) TotalErrorRate
5) 5xxErrorRate
6) 4xxErrorRate
Cloudwatch 뭔가...더...자세히...보고싶다...
(1) Client별로 요청을 얼마나 보내는지?
(2) Host (www / m / api) 별로 얼마나?
(3) URL 별로?
(4) Language 별로?
(5) 한국에서 영어로 쓰는 사람은 얼마나 있지?
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
1) Raw level log를 CSV로 S3에 쌓아주는 기능이 있
음
2) S3? Athena로 쿼리하면 되겠네?
3) 근데 S3에 만들어질때 파일 이름이 엉망이라
Athena Table에서 파티션이 안됨..
4) S3에 파일이 만들어질때마다
파일 이름을 정리해서 S3에 다시 올리면?
5) 그것도 serverless로 만들어야지..
6) S3 -> Lambda Trigger -> S3 Upload -> Athena
Partition..
7) 자세한 이야기는 https://bit.ly/2Jc8UpO
CloudFront - Advanced
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
SELECT
(year || month || day) as time,
hostheader, count(1) as count
FROM vingle_edge.cloudfront_logs
WHERE
(year || month || day)
> date_format(CURRENT_TIMESTAMP - interval '6' DAY, '%Y%m%d')
GROUP BY 1, 2
ORDER BY 1, 2 DESC
지난 6일동안 domain 별로 요청량 총
합
SELECT
uri, count(1) as count
FROM vingle_edge.cloudfront_logs
WHERE
uri LIKE '/posts/'
AND hostheader = 'https://www.vingle.net'
GROUP BY 1, 2
ORDER BY 1, 2 DESC
LIMIT 100
www.vingle.net/posts/:postId
의 총 요청량 기준 Top 100
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Dashboard
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Deployment
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices
(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
그런데 만들고 보니..
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices
(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
Sitemap Service
(robots.txt + sitemaps)
S3 Bucket
(Lambda + SQS)
CloudFront
s3.bucket.com/bucket
Google 왈:
- Sitemap은 반드시 같은 도메인에서 나와야함
- www.vingle.net/robots.txt + sitemap.s3.com/sitemap.xml (✕)
- www.vingle.net/robots.txt + www.vingle.net/sitemap.xml (✓)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
Sitemap Service
(robots.txt + sitemaps)
S3 Bucket
(Lambda + SQS)
CloudFront
s3.bucket.com/bucket
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
import {
createHeaders, CustomOrigin, getRequestHeader,
OriginRequest, OriginRequestEvent, S3Origin,
} from "../../request";
// Origin-Request Event Handler
export function handler(request: OriginRequest) {
const host = getRequestHeader(request, "Host") || "";
if (
(["www.vingle.net", "m.vingle.net"].indexOf(host) > -1)
&& (
request.uri === "/robots.txt" ||
request.uri.startsWith("/sitemaps/")
)
) {
const s3Origin: S3Origin = {
authMethod: "none",
customHeaders: {},
domainName: "vingle-sitemap-prod.s3.amazonaws.com",
path: `/${host}`,
};
request.origin = { s3: s3Origin };
request.headers = Object.assign(
request.headers,
createHeaders([{ key: "host", value: s3Origin.domainName }])
);
return request;
}
return request;
Sitemap / robots.txt 관련된 요청이
면
Origin을 S3로 설정
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
구글에서 잘 읽어가고 있나?
SELECT
(year || month || day || hour || ‘:00:00’) as time,
status,
count(1) as count
FROM vingle_edge.cloudfront_logs
WHERE
(year || month || day)
> date_format(CURRENT_TIMESTAMP - interval '6' DAY, ‘%Y%m%d’)
BY hostheader = 'www.vingle.net'
BY useragent LIKE '%Googlebot/2.1%'
BY uri LIKE '%/sitemaps_v2/%'
GROUP BY 1, 2
ORDER BY 1, 2 DESC
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Multi-Region
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
Sitemap Service
(robots.txt + sitemaps)
S3 Bucket
(Lambda + SQS)
CloudFront
1. 이러면 API Edge는 구성요소가 lambda + API Gateway가 끝이네?
2. 그러면 다른 region에 올려도 되지 않나?
npm run deploy — --region=ap-northeast-2 하면 끝인데
3. traffic을 region에 따라 다르게 보내야 하는데?
아 lambda@edge에서 routing 하면 되겠네?
4. 특정 region의 lambda가 outage나거나 하면
lambda@edge에서 다른 region으로 보낼수 있고?
5. 일반 유저들은 사용할때 속도가 더 빨라질꺼고?
6. 새로운 기능을 region별로 배포해서
테스트 해볼수도 있으니까 risk도 적어지고?
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
US-EAST-1
Load Balancer
(Lambda + DynamoDB)
Microservices(Nginx + Rails)
EC2 Fleet
(API Gateway + Lambda)
API Edge
Sitemap Service
(robots.txt + sitemaps)
S3 Bucket
(Lambda + SQS)
CloudFront
1. Multi Region이 좋은건 다들 아는데, 왜 안하지?
2. 관리가 너무 어려우니까
3. Serverless는 기본적으로 관리할게 없는데?
4. 오잉?
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
Legacy
Load Balancer
Legacy
Rails Application
Microservices
US-EAST-1
API Edge
(API Gateway + Lambda)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
AP-NORTHEAST-2
CloudFront
Legacy
Load Balancer
Legacy
Rails Application
Microservices
US-EAST-1
API Edge
(API Gateway + Lambda)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
AP-NORTHEAST-2
API Edge
(API Gateway + Lambda)
CloudFront
Legacy
Load Balancer
Legacy
Rails Application
Microservices
US-EAST-1
API Edge
(API Gateway + Lambda)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
us-east-1 ——> aaaa.execute-api.us-east-1.amazonaws.com/prod
ap-northeast-2 ——> bbbb.execute-api.ap-northeast-2.amazonaws.com/prod
us-west-1 ——> cccc.execute-api.us-west-1.amazonaws.com/prod
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
export const Endpoints = {
["us-east-1"]: {
port: 443,
domainName: "aaaa.execute-api.us-east-1.amazonaws.com",
protocol: "https",
path: "/prod",
sslProtocols: ["TLSv1", "TLSv1.1"],
readTimeout: 30,
keepaliveTimeout: 30,
customHeaders: {},
} as CustomOrigin,
["ap-northeast-2"]: {
port: 443,
domainName: "bbbb.execute-api.ap-northeast-2.amazonaws.com",
protocol: "https",
path: "/prod",
sslProtocols: ["TLSv1", "TLSv1.1"],
readTimeout: 30,
keepaliveTimeout: 30,
customHeaders: {},
} as CustomOrigin,
["us-west-1"]: {
port: 443,
domainName: "cccc.execute-api.us-west-1.amazonaws.com",
protocol: "https",
path: "/prod",
sslProtocols: ["TLSv1", "TLSv1.1"],
readTimeout: 30,
keepaliveTimeout: 30,
customHeaders: {},
} as CustomOrigin,
const APIEdgeRegionMap
= new Map<string, CustomOrigin>([
["ap-northeast-1", Endpoints["ap-northeast-2"]],
["ap-northeast-2", Endpoints["ap-northeast-2"]],
["ap-south-1", Endpoints["ap-northeast-2"]],
["ap-southeast-1", Endpoints["ap-northeast-2"]],
["ap-southeast-2", Endpoints["ap-northeast-2"]],
["ca-central-1", Endpoints["us-east-1"]],
["cn-north-1", Endpoints["us-east-1"]],
["eu-central-1", Endpoints["us-east-1"]],
["eu-west-1", Endpoints["us-east-1"]],
["eu-west-2", Endpoints["us-east-1"]],
["eu-west-3", Endpoints["us-east-1"]],
["sa-east-1", Endpoints["us-east-1"]],
["us-east-1", Endpoints["us-east-1"]],
["us-east-2", Endpoints["us-east-1"]],
["us-west-1", Endpoints["us-east-1"]],
["us-west-2", Endpoints["us-east-1"]],
]);
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
import {
CustomOrigin, getRequestHeader, OriginRequest, OriginRequestEvent
} from "../../request";
// Origin-Request Event Handler
export function handler(request: OriginRequest) {
if (
request.uri === "/api/me/notifications"
) {
const endpoint =
APIEdgeRegionMap.get(process.env.AWS_REGION as string)
|| APIEdgeEndpoints["us-east-1"];
// If missing region, which should not happen anyway,
// send it to us-east-1
API Edge로 보낼수 있는 URL인지
검사
request를 return
현재 Lambda@Edge & Cloudfront
의 region
(process.env.AWS_REGION)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
서울에서의 속도 개선
GET /cards/:cardId/likes
- 카드를 좋아요 한 사람 목록
- 유저가 새로 좋아요를 하지 않는 이상 캐쉬된 결과가 나옴. 10~20ms 이내
- 하지만 Seoul -> Virginia 최소 Latency가 100ms~200ms…
300ms ➡ 70ms
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
www.vingle.net
m.vingle.net
api.vingle.net
CloudFront
AP-NORTHEAST-2
CloudFront
US-EAST-1
API Edge
(API Gateway + Lambda)
API Edge
(API Gateway + Lambda)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
삽질을 피하기 위해서 반드시 필요한,
하지만 경험해보지 않으면 모르는 팁들
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
1. CloudFront는 Origin에서 오는 Error Response를
기본적으로 캐쉬한다.
분명히 아이디 / 비밀번호가 맞는데
로그인이 되다 안되다 해요
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
2. Lambda@Edge에서 Request Header에 접근하려면
Cloudfront에서 이걸 whitelist 해야한다
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
3. Lambda@edge는 Production환경을 디버깅 하기 어렵다.
if (
req.method === "GET"
&& regex(//api/questions/(S*)+/answers/(S*)/g, req.uri)
)
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Unit Test는 필수
it("should send request to vingle-api-edge", async () => {
const tester = async (method: string, uri: string, accept: string) => {
expect((await handler(createRequest({ method, uri, accept }))).origin)
.to.deep.eq({ custom: api1.APIEdgeEndpoints["us-east-1"] });
};
await tester("GET", "/api/talks_v2", "application/vnd.vingle-v4+json");
await tester("GET", "/api/talks_v2/anything", "application/vnd.vingle-v4+json");
await tester("GET", "/api/cards/12345/likes", "application/vnd.vingle-v4+json");
});
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Unit Test는 필수
it("should send request to vingle-api-edge", async () => {
const tester = async (method: string, uri: string, accept: string) => {
const startTime = new Date();
expect((await handler(createRequest({ method, uri, accept }))).origin)
.to.deep.eq({ custom: api1.APIEdgeEndpoints["us-east-1"] });
expect(new Date().getTime() - startTime.getTime())
.to.be.lessThan(10, "it should not take longer than 10 ms");
};
await tester("GET", "/api/talks_v2", "application/vnd.vingle-v4+json");
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
Lambda@Edge, 이러면 고려해볼만 합니다.
- Application 앞에서 (CDN - Edge)할일이 있을때
- 트래픽을 동적으로 분배하고 싶을때
- CDN 레벨에서 캐쉬를 조작하고 싶을때
- Unit Test 는 필수
- CloudFront / Athena / Cloudwatch
- 자동화! 자동화!
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
이런 문제를 함께 풀어가실 분을 기다립니다.
careers.vingle.net

More Related Content

PDF
Route53 및 CloudFront를 이용한 CDN 활용기 - AWS Summit Seoul 2017
Amazon Web Services Korea
 
PDF
복잡한 권한신청문제 ConsoleMe로 해결하기 - 손건 (AB180) :: AWS Community Day Online 2021
AWSKRUG - AWS한국사용자모임
 
PDF
20190730 AWS Black Belt Online Seminar Amazon CloudFrontの概要
Amazon Web Services Japan
 
PDF
Amazon OpenSearch Deep dive - 내부구조, 성능최적화 그리고 스케일링
Amazon Web Services Korea
 
PDF
IDC 서버 몽땅 AWS로 이전하기 위한 5가지 방법 - 윤석찬 (AWS 테크에반젤리스트)
Amazon Web Services Korea
 
PDF
더욱 진화하는 AWS 네트워크 보안 - 신은수 AWS 시큐리티 스페셜리스트 솔루션즈 아키텍트 :: AWS Summit Seoul 2021
Amazon Web Services Korea
 
PDF
AWS Fargate on EKS 실전 사용하기
AWSKRUG - AWS한국사용자모임
 
PDF
금융 서비스 패러다임의 전환 가속화 시대, 신한금융투자의 Cloud First 전략 - 신중훈 AWS 솔루션즈 아키텍트 / 최성봉 클라우...
Amazon Web Services Korea
 
Route53 및 CloudFront를 이용한 CDN 활용기 - AWS Summit Seoul 2017
Amazon Web Services Korea
 
복잡한 권한신청문제 ConsoleMe로 해결하기 - 손건 (AB180) :: AWS Community Day Online 2021
AWSKRUG - AWS한국사용자모임
 
20190730 AWS Black Belt Online Seminar Amazon CloudFrontの概要
Amazon Web Services Japan
 
Amazon OpenSearch Deep dive - 내부구조, 성능최적화 그리고 스케일링
Amazon Web Services Korea
 
IDC 서버 몽땅 AWS로 이전하기 위한 5가지 방법 - 윤석찬 (AWS 테크에반젤리스트)
Amazon Web Services Korea
 
더욱 진화하는 AWS 네트워크 보안 - 신은수 AWS 시큐리티 스페셜리스트 솔루션즈 아키텍트 :: AWS Summit Seoul 2021
Amazon Web Services Korea
 
AWS Fargate on EKS 실전 사용하기
AWSKRUG - AWS한국사용자모임
 
금융 서비스 패러다임의 전환 가속화 시대, 신한금융투자의 Cloud First 전략 - 신중훈 AWS 솔루션즈 아키텍트 / 최성봉 클라우...
Amazon Web Services Korea
 

What's hot (20)

PDF
Oracle DB를 AWS로 이관하는 방법들 - 서호석 클라우드 사업부/컨설팅팀 이사, 영우디지탈 :: AWS Summit Seoul 2021
Amazon Web Services Korea
 
PDF
Security on AWS :: 이경수 솔루션즈아키텍트
Amazon Web Services Korea
 
PPTX
OpsNow를 활용한 AWS Cloud 비용 최적화 전략
BESPIN GLOBAL
 
PDF
20201118 AWS Black Belt Online Seminar 形で考えるサーバーレス設計 サーバーレスユースケースパターン解説
Amazon Web Services Japan
 
PDF
20211203 AWS Black Belt Online Seminar AWS re:Invent 2021アップデート速報
Amazon Web Services Japan
 
PDF
AWS Summit Seoul 2023 | 가격은 저렴, 성능은 최대로! 확 달라진 Amazon EC2 알아보기
Amazon Web Services Korea
 
PPTX
9월 웨비나 - AWS에서의 네트워크 보안 (이경수 솔루션즈 아키텍트)
Amazon Web Services Korea
 
PDF
AWS 시작하기 및 Amazon S3 살펴보기 (윤석찬) - AWS 웨비나 시리즈
Amazon Web Services Korea
 
PPTX
AWS 기반 대규모 트래픽 견디기 - 장준엽 (구로디지털 모임) :: AWS Community Day 2017
AWSKRUG - AWS한국사용자모임
 
PDF
20191218 AWS Black Belt Online Seminar AWSのマネジメント&ガバナンス サービスアップデート
Amazon Web Services Japan
 
PDF
[2017 AWS Startup Day] AWS 비용 최대 90% 절감하기: 스팟 인스턴스 Deep-Dive
Amazon Web Services Korea
 
PDF
AWS Black Belt Online Seminar 2017 AWS Shield
Amazon Web Services Japan
 
PDF
AWS Black Belt Online Seminar 2016 クラウドのためのアーキテクチャ設計 -ベストプラクティス-
Amazon Web Services Japan
 
PDF
Amazon RDS Proxy 집중 탐구 - 윤석찬 :: AWS Unboxing 온라인 세미나
Amazon Web Services Korea
 
PDF
DMS와 SCT를 활용한 Oracle에서 Open Source DB로의 전환
Amazon Web Services Korea
 
PDF
AWS로 사용자 천만 명 서비스 만들기 (윤석찬)- 클라우드 태권 2015
Amazon Web Services Korea
 
PDF
실시간 스트리밍 분석 Kinesis Data Analytics Deep Dive
Amazon Web Services Korea
 
PPTX
CloudFront(클라우드 프론트)와 Route53(라우트53) AWS Summit Seoul 2015
WineSOFT
 
PPTX
AWS 의 비용 절감 프레임워크와 신규 프로그램을 활용한 전략적 비용절감 :: AWS Travel and Transportation 온라인...
Amazon Web Services Korea
 
PDF
AWS 네트워크 보안을 위한 계층별 보안 구성 모범 사례 – 조이정, AWS 솔루션즈 아키텍트:: AWS 온라인 이벤트 – 클라우드 보안 특집
Amazon Web Services Korea
 
Oracle DB를 AWS로 이관하는 방법들 - 서호석 클라우드 사업부/컨설팅팀 이사, 영우디지탈 :: AWS Summit Seoul 2021
Amazon Web Services Korea
 
Security on AWS :: 이경수 솔루션즈아키텍트
Amazon Web Services Korea
 
OpsNow를 활용한 AWS Cloud 비용 최적화 전략
BESPIN GLOBAL
 
20201118 AWS Black Belt Online Seminar 形で考えるサーバーレス設計 サーバーレスユースケースパターン解説
Amazon Web Services Japan
 
20211203 AWS Black Belt Online Seminar AWS re:Invent 2021アップデート速報
Amazon Web Services Japan
 
AWS Summit Seoul 2023 | 가격은 저렴, 성능은 최대로! 확 달라진 Amazon EC2 알아보기
Amazon Web Services Korea
 
9월 웨비나 - AWS에서의 네트워크 보안 (이경수 솔루션즈 아키텍트)
Amazon Web Services Korea
 
AWS 시작하기 및 Amazon S3 살펴보기 (윤석찬) - AWS 웨비나 시리즈
Amazon Web Services Korea
 
AWS 기반 대규모 트래픽 견디기 - 장준엽 (구로디지털 모임) :: AWS Community Day 2017
AWSKRUG - AWS한국사용자모임
 
20191218 AWS Black Belt Online Seminar AWSのマネジメント&ガバナンス サービスアップデート
Amazon Web Services Japan
 
[2017 AWS Startup Day] AWS 비용 최대 90% 절감하기: 스팟 인스턴스 Deep-Dive
Amazon Web Services Korea
 
AWS Black Belt Online Seminar 2017 AWS Shield
Amazon Web Services Japan
 
AWS Black Belt Online Seminar 2016 クラウドのためのアーキテクチャ設計 -ベストプラクティス-
Amazon Web Services Japan
 
Amazon RDS Proxy 집중 탐구 - 윤석찬 :: AWS Unboxing 온라인 세미나
Amazon Web Services Korea
 
DMS와 SCT를 활용한 Oracle에서 Open Source DB로의 전환
Amazon Web Services Korea
 
AWS로 사용자 천만 명 서비스 만들기 (윤석찬)- 클라우드 태권 2015
Amazon Web Services Korea
 
실시간 스트리밍 분석 Kinesis Data Analytics Deep Dive
Amazon Web Services Korea
 
CloudFront(클라우드 프론트)와 Route53(라우트53) AWS Summit Seoul 2015
WineSOFT
 
AWS 의 비용 절감 프레임워크와 신규 프로그램을 활용한 전략적 비용절감 :: AWS Travel and Transportation 온라인...
Amazon Web Services Korea
 
AWS 네트워크 보안을 위한 계층별 보안 구성 모범 사례 – 조이정, AWS 솔루션즈 아키텍트:: AWS 온라인 이벤트 – 클라우드 보안 특집
Amazon Web Services Korea
 
Ad

Similar to Lambda@Edge를통한멀티리전기반글로벌트래픽길들이기::이상현::AWS Summit Seoul 2018 (14)

PDF
More Containers Less Operations
Donnie Prakoso
 
PPTX
Application Modernization using the Strangler Pattern
Tom Laszewski
 
PDF
Serverless best practices plus design principles 20m version
Heitor Lessa
 
PPTX
Building API Driven Microservices
Chris Munns
 
PDF
높은 가용성과 성능 향상을 위한 ElastiCache 활용 팁 - 임근택, SendBird :: AWS Summit Seoul 2019
Amazon Web Services Korea
 
PDF
How to Use AWS Lambda Layers and Lambda Runtime
Donnie Prakoso
 
PDF
Thinking Asynchronously Full Vesion - Utah UG
Eric Johnson
 
PDF
Microservices for Startups - Donnie Prakoso - AWS - CC18
CodeOps Technologies LLP
 
PPTX
The New Normal Getting Started with AWS
Tom Laszewski
 
PDF
Serverless Architectural Patterns - ServerlessDays TLV
Boaz Ziniman
 
PDF
AWS Application Service Workshop - Serverless Architecture
John Yeung
 
PDF
Modern Applications Web Day | Impress Your Friends with Your First Serverless...
AWS Germany
 
PDF
Building Serverless Microservices with AWS
Donnie Prakoso
 
PPTX
Serverless Architectural Patterns I AWS Dev Day 2018
AWS Germany
 
More Containers Less Operations
Donnie Prakoso
 
Application Modernization using the Strangler Pattern
Tom Laszewski
 
Serverless best practices plus design principles 20m version
Heitor Lessa
 
Building API Driven Microservices
Chris Munns
 
높은 가용성과 성능 향상을 위한 ElastiCache 활용 팁 - 임근택, SendBird :: AWS Summit Seoul 2019
Amazon Web Services Korea
 
How to Use AWS Lambda Layers and Lambda Runtime
Donnie Prakoso
 
Thinking Asynchronously Full Vesion - Utah UG
Eric Johnson
 
Microservices for Startups - Donnie Prakoso - AWS - CC18
CodeOps Technologies LLP
 
The New Normal Getting Started with AWS
Tom Laszewski
 
Serverless Architectural Patterns - ServerlessDays TLV
Boaz Ziniman
 
AWS Application Service Workshop - Serverless Architecture
John Yeung
 
Modern Applications Web Day | Impress Your Friends with Your First Serverless...
AWS Germany
 
Building Serverless Microservices with AWS
Donnie Prakoso
 
Serverless Architectural Patterns I AWS Dev Day 2018
AWS Germany
 
Ad

More from Amazon Web Services Korea (20)

PDF
[D3T1S01] Gen AI를 위한 Amazon Aurora 활용 사례 방법
Amazon Web Services Korea
 
PDF
[D3T1S06] Neptune Analytics with Vector Similarity Search
Amazon Web Services Korea
 
PDF
[D3T1S03] Amazon DynamoDB design puzzlers
Amazon Web Services Korea
 
PDF
[D3T1S04] Aurora PostgreSQL performance monitoring and troubleshooting by use...
Amazon Web Services Korea
 
PDF
[D3T1S07] AWS S3 - 클라우드 환경에서 데이터베이스 보호하기
Amazon Web Services Korea
 
PDF
[D3T1S05] Aurora 혼합 구성 아키텍처를 사용하여 예상치 못한 트래픽 급증 대응하기
Amazon Web Services Korea
 
PDF
[D3T1S02] Aurora Limitless Database Introduction
Amazon Web Services Korea
 
PDF
[D3T2S01] Amazon Aurora MySQL 메이저 버전 업그레이드 및 Amazon B/G Deployments 실습
Amazon Web Services Korea
 
PDF
[D3T2S03] Data&AI Roadshow 2024 - Amazon DocumentDB 실습
Amazon Web Services Korea
 
PDF
AWS Modern Infra with Storage Roadshow 2023 - Day 2
Amazon Web Services Korea
 
PDF
AWS Modern Infra with Storage Roadshow 2023 - Day 1
Amazon Web Services Korea
 
PDF
사례로 알아보는 Database Migration Service : 데이터베이스 및 데이터 이관, 통합, 분리, 분석의 도구 - 발표자: ...
Amazon Web Services Korea
 
PDF
Amazon DocumentDB - Architecture 및 Best Practice (Level 200) - 발표자: 장동훈, Sr. ...
Amazon Web Services Korea
 
PDF
Amazon Elasticache - Fully managed, Redis & Memcached Compatible Service (Lev...
Amazon Web Services Korea
 
PDF
Internal Architecture of Amazon Aurora (Level 400) - 발표자: 정달영, APAC RDS Speci...
Amazon Web Services Korea
 
PDF
[Keynote] 슬기로운 AWS 데이터베이스 선택하기 - 발표자: 강민석, Korea Database SA Manager, WWSO, A...
Amazon Web Services Korea
 
PDF
Demystify Streaming on AWS - 발표자: 이종혁, Sr Analytics Specialist, WWSO, AWS :::...
Amazon Web Services Korea
 
PDF
Amazon EMR - Enhancements on Cost/Performance, Serverless - 발표자: 김기영, Sr Anal...
Amazon Web Services Korea
 
PDF
Amazon OpenSearch - Use Cases, Security/Observability, Serverless and Enhance...
Amazon Web Services Korea
 
PDF
Enabling Agility with Data Governance - 발표자: 김성연, Analytics Specialist, WWSO,...
Amazon Web Services Korea
 
[D3T1S01] Gen AI를 위한 Amazon Aurora 활용 사례 방법
Amazon Web Services Korea
 
[D3T1S06] Neptune Analytics with Vector Similarity Search
Amazon Web Services Korea
 
[D3T1S03] Amazon DynamoDB design puzzlers
Amazon Web Services Korea
 
[D3T1S04] Aurora PostgreSQL performance monitoring and troubleshooting by use...
Amazon Web Services Korea
 
[D3T1S07] AWS S3 - 클라우드 환경에서 데이터베이스 보호하기
Amazon Web Services Korea
 
[D3T1S05] Aurora 혼합 구성 아키텍처를 사용하여 예상치 못한 트래픽 급증 대응하기
Amazon Web Services Korea
 
[D3T1S02] Aurora Limitless Database Introduction
Amazon Web Services Korea
 
[D3T2S01] Amazon Aurora MySQL 메이저 버전 업그레이드 및 Amazon B/G Deployments 실습
Amazon Web Services Korea
 
[D3T2S03] Data&AI Roadshow 2024 - Amazon DocumentDB 실습
Amazon Web Services Korea
 
AWS Modern Infra with Storage Roadshow 2023 - Day 2
Amazon Web Services Korea
 
AWS Modern Infra with Storage Roadshow 2023 - Day 1
Amazon Web Services Korea
 
사례로 알아보는 Database Migration Service : 데이터베이스 및 데이터 이관, 통합, 분리, 분석의 도구 - 발표자: ...
Amazon Web Services Korea
 
Amazon DocumentDB - Architecture 및 Best Practice (Level 200) - 발표자: 장동훈, Sr. ...
Amazon Web Services Korea
 
Amazon Elasticache - Fully managed, Redis & Memcached Compatible Service (Lev...
Amazon Web Services Korea
 
Internal Architecture of Amazon Aurora (Level 400) - 발표자: 정달영, APAC RDS Speci...
Amazon Web Services Korea
 
[Keynote] 슬기로운 AWS 데이터베이스 선택하기 - 발표자: 강민석, Korea Database SA Manager, WWSO, A...
Amazon Web Services Korea
 
Demystify Streaming on AWS - 발표자: 이종혁, Sr Analytics Specialist, WWSO, AWS :::...
Amazon Web Services Korea
 
Amazon EMR - Enhancements on Cost/Performance, Serverless - 발표자: 김기영, Sr Anal...
Amazon Web Services Korea
 
Amazon OpenSearch - Use Cases, Security/Observability, Serverless and Enhance...
Amazon Web Services Korea
 
Enabling Agility with Data Governance - 발표자: 김성연, Analytics Specialist, WWSO,...
Amazon Web Services Korea
 

Recently uploaded (20)

PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Future of Artificial Intelligence (AI)
Mukul
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 

Lambda@Edge를통한멀티리전기반글로벌트래픽길들이기::이상현::AWS Summit Seoul 2018

  • 1. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 이상현, Vingle Lambda@Edge를 이용한 Serverless로의 Migration & Multi-Region
  • 2. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 이상현 Kurt Lee Technical Leader, Vingle Inc [email protected] https://github.com/breath103
  • 3. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Vingle, Interest Network
  • 4. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  • 5. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Monthly Active User Monthly Page View App Store Rating 5 Milion 3 Billion 4.7
  • 6. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Lambda@Edge CDN + Edge Computing Microservice Lambda + API Gateway Serverless Microservice Gradual Migration Multi-Region (Cache + Edge) Agenda
  • 7. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 다루지 않는 내용 1. Microservice / monolith의 개념 2. 왜 Microservice로 옮겼나 3. 왜 Serverless로 옮겼나 4. API Gateway / Lambda / S3 / Athena 상세 설명 5. CI / CD 상세 설명
  • 8. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Serverless Microservice Gradual Migration 1. Legacy Application이 있고, 2. 새로운 Application이 있고, 3. 새로운 Application으로 기능을 하나씩 옮겨갈때 4. 어떻게 트래픽을 지능적으로 (어떤 Application으로 보낼지 판단해서) 5. 안정적으로, 점진적으로 Routing 할 것 인가? 6. 사람 4명으로, 기능을 계속 추가하면서
  • 9. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Previously…
  • 10. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) US-EAST-1
  • 11. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest Notifications Card QnA 1. Microservice들의 응답을 조합 + Routing (API Gateway) 2. 아주 기본적인 유저 인증 (JWT ▷ user_id) 3. 옮기기 어려운 것들 (옛날 RDS라던가.. devise 라던가..) US-EAST-1
  • 12. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 1) 요청이 들어옴 2) 유저 인증 JWT ➡ userId 3) Microservice로 보냄 /me/notifications ➡ getNotifications(userId) 4) 응답 return
  • 13. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Rails가 하는 일을 Microservice로 구현 (Lambda + API Gateway)
  • 14. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) aaa.excute-api.us-east-1.com 이걸 어떻게 배포하지?;; (천천히?)
  • 15. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) aaa.excute-api.us-east-1.com
  • 16. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) aaa.excute-api.us-east-1.com • API Gateway를 target으로 지정 못 함 • URL Pattern으로만 routing 가능
  • 17. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) aaa.excute-api.us-east-1.com • Multiple Origin이 지원 안됨
  • 18. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) aaa.excute-api.us-east-1.com • Domain 이외의 정보는 모두 사용 불가 확 Reverse Proxy(Nginx) Cluster를 만들어버릴까...
  • 19. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) Nginx Cluster
  • 20. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Homo Faber : "Working Man" Animal Laborans: "Working Animal" Animal Laborans란 이름 그대로, 기계적이고 일상적으로 일을 하는 가축과 같은 사람을 뜻한다. 아렌트는 오펜하이머가 원자폭탄을 "달콤한 문제" 라고 표현하는 것이나, 아이히만(유대인 학살 시스템 설계자)이 얼마나 가스실을 "효율적"으로 만드는데 집착했는지 에서 이 이미지를 찾았다. 그들은 뭔가가 돌아가게 하는대에(making it work)만 몰입하며, 그로써 "일 그 자체"를 "일의 끝"으로 삼는다. Homo Faber는 이와 대조되는 종류의 인간을 뜻한다. 원어는 라틴어로 "만드는 사람" 이지만, 아렌트는 좀더 분명한 이미지를 부여했다. 그녀에 따르면 Homo Faber는 노동과 노력의 판단자(Judge)이다. 이때 인간은 일에 몰입하는 존재이지만, 동시에 도덕과 판단의 주체이다. 이들은 뭔가를 생산하기를 멈추고, 다른 판단자들과 자신들이 하는 일에 대해 토론하고 판단할수 있다. 그렇기에, Animal Laborans이 "How"를 물을때 Homo Faber는 "Why"를 묻는다 - Richard Sennett, The Craftsman 문장이 길어지면 안읽는 사람들을 위한 4줄 요약: 1. 인간은 원래 만드는 것에 몰입하는 존재다 2. 몰입해서 일만하면 원자폭탄이나 가스실을 만든다 (그리고 심지어 너무 만족스러워한다) 3. 그러니 지금 하는일을 왜 하는지 판단하면서 해라 4. 안그러면 가축이랑 다른게 뭐냐
  • 21. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CDN Load Balancer EC2 Fleet (Nginx + Rails) Microservices (Lambda + DynamoDB) Feed Interest US-EAST-1 Notifications Card QnA API Edge (API Gateway + Lambda) Nginx Cluster 1. Serverless로 가야함 2. 천천히 migration 하려면 reverse proxy가 필요 3. reverse proxy cluster를 앞에 만들자? ?? Serverless로 가려고 Cluster를 하나 더 만들자 ??
  • 22. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. function proxy(req: Request) { if (req.url == "/api/me/notifications" && req.headers.get("accept") == "application/json") { return sendToAPIEdge(req); } else { return sendToRails(req); } 이걸 Serverless로 돌리고 싶을 뿐인데..
  • 23. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 때는 11월... Lambda@Edge?
  • 24. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Origi n America Asia Europe Africa
  • 25. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Lambda@Edge 1. Request에 따라 다른 Origin으로 보낸다거나 2. Request에 따라 CDN에서 바로 응답을 준다거나 3. CDN Cache에 넣기 전에 Origin Response를 변조한다던가 4. Cache된 Reponse를 주기전에 변조한다던가
  • 26. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Reverse Proxy (Serverless) - Multiple Origin Servers - Dynamic Routing - Caching - Managed Reverse Proxy … Web App (Simple) - CORS (OPTION request) - 404 / 500 / down - Redirection (www -> m) - IP Blocks …
  • 27. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices (Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge CDN
  • 28. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices (Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge CDN
  • 29. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. import { CustomOrigin, getRequestHeader, OriginRequest, OriginRequestEvent } from "../../request"; // Origin-Request Event Handler export function handler(request: OriginRequest) { if ( request.uri === "/api/me/notifications" ) { const apiEdge: CustomOrigin = { port: 443, domainName: "xxxxxxx.execute-api.us-east-1.amazonaws.com", protocol: "https", path: "/prod", sslProtocols: ["TLSv1", "TLSv1.1"], readTimeout: 30, keepaliveTimeout: 30, customHeaders: {}, }; request.origin = { custom: apiEdge }; // Override host to let APIGateway swallow this request.headers.host = [ { key: "host", value: apiEdge.domainName } ]; } return request; } Proof of concept API Edge로 보낼수 있는 URL인지 검사 맞으면 Request의 Origin을 API Edge로 변경 request를 return
  • 30. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 그때는 몰랐습니다. 이게 겨우 시작일줄은...
  • 31. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 1. The ultimate goal of backend team is to make Vingle service better. Anything “we” can do to accomplish that is our job 2. Primary goal of backend team is to provide manageable, reliable, scalable, extensible backend service. 3. There is no such a thing as “Perfect Architecture / Library / Pattern that solve every problems.” Design Pattern is just a pattern. Remember "To a man with a hammer, everything looks like a nail." Mixing patterns, or being untraditional, or being heretic is perfectly fine if it solves the given problem and the situation. 4. Question all the rules and traditions. Test them and rewrite them if you can. 5. Get rid of things to “manage” as much as possible. Our time can be much more valuable when it’s spent on building things, not monitoring and managing. Let machines do that. 6. API is the product we build, frontend developers are our customer. 7. “Any organization that designs a system will produce a design whose structure is a copy of the organization's communication structure.” Fundamental principles of our team “관리” 해야 하는것을 최대한 없애라. 우리의 시간은 뭔가를 만들기 위해 쓰여질때 가장 가치있다. “관리” 는 기계가 하게 해라. Lint / Test / Deployment / Monitoring / Alert
  • 32. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 문제 1) Lambda@Edge는 반드시 lambda의 “version”을 명시해야함. arn:xxx:xxx:function-name:$LATEST (X) arn:xxx:xxx:function-name:98 (✓) 즉 새로운 lambda 버젼을 올릴때마다, Cloudfront도 배포해야함
  • 33. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Repository Setup vingle-edge Origin-Request 요청에 따라, 어떤 Origin으로 보낼지 분기 / 결정 Viewer-Request 요청에 따라, Edge에서 응답을 만들어 주는경우 function handler(request: OriginRequest): OriginRequest function handler(request: OriginRequest): OriginRequest | OriginResponse CloudFront Distribution 기본 Origin (Rails ELB) / Cache Policy / Cookie, Header forwarding.. etc Continuous Integration Push 1. Test 2. Lint 3. 새로운 Lambda Version을 올리고, 4. CloudFront에 해당 Lambda Version을 연결 - serverless-lambda-version plugin
  • 34. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 예상치 못한 여정: Monitoring
  • 35. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Lambda@Edge 1) Lambda@Edge는 원본 Lambda를 모든 CloudFront region에 복사하는식으로 동작 함 2) 각각의 복사본 이름은 (원본region).(원본 이름):(version) 3) 이때 복사된 Lambda각각은 일반적인 Lambda와 다를바 없이 CloudWatch 제공. 4) AWS console에서는 안보이는데, filter 켜면 접근 가능
  • 36. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Lambda@Edge - CloudWatch
  • 37. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. CloudFront - Basic 1) BytesUploaded 2) BytesDownloaded 3) Requests 4) TotalErrorRate 5) 5xxErrorRate 6) 4xxErrorRate Cloudwatch 뭔가...더...자세히...보고싶다... (1) Client별로 요청을 얼마나 보내는지? (2) Host (www / m / api) 별로 얼마나? (3) URL 별로? (4) Language 별로? (5) 한국에서 영어로 쓰는 사람은 얼마나 있지?
  • 38. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 1) Raw level log를 CSV로 S3에 쌓아주는 기능이 있 음 2) S3? Athena로 쿼리하면 되겠네? 3) 근데 S3에 만들어질때 파일 이름이 엉망이라 Athena Table에서 파티션이 안됨.. 4) S3에 파일이 만들어질때마다 파일 이름을 정리해서 S3에 다시 올리면? 5) 그것도 serverless로 만들어야지.. 6) S3 -> Lambda Trigger -> S3 Upload -> Athena Partition.. 7) 자세한 이야기는 https://bit.ly/2Jc8UpO CloudFront - Advanced
  • 39. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. SELECT (year || month || day) as time, hostheader, count(1) as count FROM vingle_edge.cloudfront_logs WHERE (year || month || day) > date_format(CURRENT_TIMESTAMP - interval '6' DAY, '%Y%m%d') GROUP BY 1, 2 ORDER BY 1, 2 DESC 지난 6일동안 domain 별로 요청량 총 합 SELECT uri, count(1) as count FROM vingle_edge.cloudfront_logs WHERE uri LIKE '/posts/' AND hostheader = 'https://www.vingle.net' GROUP BY 1, 2 ORDER BY 1, 2 DESC LIMIT 100 www.vingle.net/posts/:postId 의 총 요청량 기준 Top 100
  • 40. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Dashboard
  • 41. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Deployment
  • 42. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices (Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge
  • 43. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 그런데 만들고 보니..
  • 44. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices (Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge
  • 45. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices(Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge Sitemap Service (robots.txt + sitemaps) S3 Bucket (Lambda + SQS) CloudFront s3.bucket.com/bucket Google 왈: - Sitemap은 반드시 같은 도메인에서 나와야함 - www.vingle.net/robots.txt + sitemap.s3.com/sitemap.xml (✕) - www.vingle.net/robots.txt + www.vingle.net/sitemap.xml (✓)
  • 46. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices(Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge Sitemap Service (robots.txt + sitemaps) S3 Bucket (Lambda + SQS) CloudFront s3.bucket.com/bucket
  • 47. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. import { createHeaders, CustomOrigin, getRequestHeader, OriginRequest, OriginRequestEvent, S3Origin, } from "../../request"; // Origin-Request Event Handler export function handler(request: OriginRequest) { const host = getRequestHeader(request, "Host") || ""; if ( (["www.vingle.net", "m.vingle.net"].indexOf(host) > -1) && ( request.uri === "/robots.txt" || request.uri.startsWith("/sitemaps/") ) ) { const s3Origin: S3Origin = { authMethod: "none", customHeaders: {}, domainName: "vingle-sitemap-prod.s3.amazonaws.com", path: `/${host}`, }; request.origin = { s3: s3Origin }; request.headers = Object.assign( request.headers, createHeaders([{ key: "host", value: s3Origin.domainName }]) ); return request; } return request; Sitemap / robots.txt 관련된 요청이 면 Origin을 S3로 설정
  • 48. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 구글에서 잘 읽어가고 있나? SELECT (year || month || day || hour || ‘:00:00’) as time, status, count(1) as count FROM vingle_edge.cloudfront_logs WHERE (year || month || day) > date_format(CURRENT_TIMESTAMP - interval '6' DAY, ‘%Y%m%d’) BY hostheader = 'www.vingle.net' BY useragent LIKE '%Googlebot/2.1%' BY uri LIKE '%/sitemaps_v2/%' GROUP BY 1, 2 ORDER BY 1, 2 DESC
  • 49. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  • 50. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Multi-Region
  • 51. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices(Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge Sitemap Service (robots.txt + sitemaps) S3 Bucket (Lambda + SQS) CloudFront 1. 이러면 API Edge는 구성요소가 lambda + API Gateway가 끝이네? 2. 그러면 다른 region에 올려도 되지 않나? npm run deploy — --region=ap-northeast-2 하면 끝인데 3. traffic을 region에 따라 다르게 보내야 하는데? 아 lambda@edge에서 routing 하면 되겠네? 4. 특정 region의 lambda가 outage나거나 하면 lambda@edge에서 다른 region으로 보낼수 있고? 5. 일반 유저들은 사용할때 속도가 더 빨라질꺼고? 6. 새로운 기능을 region별로 배포해서 테스트 해볼수도 있으니까 risk도 적어지고?
  • 52. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net US-EAST-1 Load Balancer (Lambda + DynamoDB) Microservices(Nginx + Rails) EC2 Fleet (API Gateway + Lambda) API Edge Sitemap Service (robots.txt + sitemaps) S3 Bucket (Lambda + SQS) CloudFront 1. Multi Region이 좋은건 다들 아는데, 왜 안하지? 2. 관리가 너무 어려우니까 3. Serverless는 기본적으로 관리할게 없는데? 4. 오잉?
  • 53. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront Legacy Load Balancer Legacy Rails Application Microservices US-EAST-1 API Edge (API Gateway + Lambda)
  • 54. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront AP-NORTHEAST-2 CloudFront Legacy Load Balancer Legacy Rails Application Microservices US-EAST-1 API Edge (API Gateway + Lambda)
  • 55. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront AP-NORTHEAST-2 API Edge (API Gateway + Lambda) CloudFront Legacy Load Balancer Legacy Rails Application Microservices US-EAST-1 API Edge (API Gateway + Lambda)
  • 56. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. us-east-1 ——> aaaa.execute-api.us-east-1.amazonaws.com/prod ap-northeast-2 ——> bbbb.execute-api.ap-northeast-2.amazonaws.com/prod us-west-1 ——> cccc.execute-api.us-west-1.amazonaws.com/prod
  • 57. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. export const Endpoints = { ["us-east-1"]: { port: 443, domainName: "aaaa.execute-api.us-east-1.amazonaws.com", protocol: "https", path: "/prod", sslProtocols: ["TLSv1", "TLSv1.1"], readTimeout: 30, keepaliveTimeout: 30, customHeaders: {}, } as CustomOrigin, ["ap-northeast-2"]: { port: 443, domainName: "bbbb.execute-api.ap-northeast-2.amazonaws.com", protocol: "https", path: "/prod", sslProtocols: ["TLSv1", "TLSv1.1"], readTimeout: 30, keepaliveTimeout: 30, customHeaders: {}, } as CustomOrigin, ["us-west-1"]: { port: 443, domainName: "cccc.execute-api.us-west-1.amazonaws.com", protocol: "https", path: "/prod", sslProtocols: ["TLSv1", "TLSv1.1"], readTimeout: 30, keepaliveTimeout: 30, customHeaders: {}, } as CustomOrigin, const APIEdgeRegionMap = new Map<string, CustomOrigin>([ ["ap-northeast-1", Endpoints["ap-northeast-2"]], ["ap-northeast-2", Endpoints["ap-northeast-2"]], ["ap-south-1", Endpoints["ap-northeast-2"]], ["ap-southeast-1", Endpoints["ap-northeast-2"]], ["ap-southeast-2", Endpoints["ap-northeast-2"]], ["ca-central-1", Endpoints["us-east-1"]], ["cn-north-1", Endpoints["us-east-1"]], ["eu-central-1", Endpoints["us-east-1"]], ["eu-west-1", Endpoints["us-east-1"]], ["eu-west-2", Endpoints["us-east-1"]], ["eu-west-3", Endpoints["us-east-1"]], ["sa-east-1", Endpoints["us-east-1"]], ["us-east-1", Endpoints["us-east-1"]], ["us-east-2", Endpoints["us-east-1"]], ["us-west-1", Endpoints["us-east-1"]], ["us-west-2", Endpoints["us-east-1"]], ]);
  • 58. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. import { CustomOrigin, getRequestHeader, OriginRequest, OriginRequestEvent } from "../../request"; // Origin-Request Event Handler export function handler(request: OriginRequest) { if ( request.uri === "/api/me/notifications" ) { const endpoint = APIEdgeRegionMap.get(process.env.AWS_REGION as string) || APIEdgeEndpoints["us-east-1"]; // If missing region, which should not happen anyway, // send it to us-east-1 API Edge로 보낼수 있는 URL인지 검사 request를 return 현재 Lambda@Edge & Cloudfront 의 region (process.env.AWS_REGION)
  • 59. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 서울에서의 속도 개선 GET /cards/:cardId/likes - 카드를 좋아요 한 사람 목록 - 유저가 새로 좋아요를 하지 않는 이상 캐쉬된 결과가 나옴. 10~20ms 이내 - 하지만 Seoul -> Virginia 최소 Latency가 100ms~200ms… 300ms ➡ 70ms
  • 60. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. www.vingle.net m.vingle.net api.vingle.net CloudFront AP-NORTHEAST-2 CloudFront US-EAST-1 API Edge (API Gateway + Lambda) API Edge (API Gateway + Lambda)
  • 61. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  • 62. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 삽질을 피하기 위해서 반드시 필요한, 하지만 경험해보지 않으면 모르는 팁들
  • 63. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 1. CloudFront는 Origin에서 오는 Error Response를 기본적으로 캐쉬한다. 분명히 아이디 / 비밀번호가 맞는데 로그인이 되다 안되다 해요
  • 64. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 2. Lambda@Edge에서 Request Header에 접근하려면 Cloudfront에서 이걸 whitelist 해야한다
  • 65. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 3. Lambda@edge는 Production환경을 디버깅 하기 어렵다. if ( req.method === "GET" && regex(//api/questions/(S*)+/answers/(S*)/g, req.uri) )
  • 66. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Unit Test는 필수 it("should send request to vingle-api-edge", async () => { const tester = async (method: string, uri: string, accept: string) => { expect((await handler(createRequest({ method, uri, accept }))).origin) .to.deep.eq({ custom: api1.APIEdgeEndpoints["us-east-1"] }); }; await tester("GET", "/api/talks_v2", "application/vnd.vingle-v4+json"); await tester("GET", "/api/talks_v2/anything", "application/vnd.vingle-v4+json"); await tester("GET", "/api/cards/12345/likes", "application/vnd.vingle-v4+json"); });
  • 67. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Unit Test는 필수 it("should send request to vingle-api-edge", async () => { const tester = async (method: string, uri: string, accept: string) => { const startTime = new Date(); expect((await handler(createRequest({ method, uri, accept }))).origin) .to.deep.eq({ custom: api1.APIEdgeEndpoints["us-east-1"] }); expect(new Date().getTime() - startTime.getTime()) .to.be.lessThan(10, "it should not take longer than 10 ms"); }; await tester("GET", "/api/talks_v2", "application/vnd.vingle-v4+json");
  • 68. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Lambda@Edge, 이러면 고려해볼만 합니다. - Application 앞에서 (CDN - Edge)할일이 있을때 - 트래픽을 동적으로 분배하고 싶을때 - CDN 레벨에서 캐쉬를 조작하고 싶을때 - Unit Test 는 필수 - CloudFront / Athena / Cloudwatch - 자동화! 자동화!
  • 69. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 이런 문제를 함께 풀어가실 분을 기다립니다. careers.vingle.net