K O R E A | M A Y 1 1 - 1 2 , 2 0 2 1
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
대용량 트래픽을 처리하는
최적의 서버리스 애플리케이션
안효빈
솔루션즈 아키텍트
AWS
구성완
솔루션즈 아키텍트
AWS
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
서버리스 애플리케이션
함수
Node.js
Python
Java
C#
Go
Ruby
Runtime API
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
함수
Node.js
Python
Java
C#
Go
Ruby
Runtime API
이벤트 소스
데이터 상태 변화
엔드포인트 요청
리소스 상태 변화
서버리스 애플리케이션
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
서버리스 애플리케이션
함수
Node.js
Python
Java
C#
Go
Ruby
Runtime API
이벤트 소스 서비스
데이터 상태 변화
엔드포인트 요청
리소스 상태 변화
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 실행 모델
비동기
동기 스트림
Amazon API
Gateway
/order
Lambda
function
reqs
Lambda
function
Amazon
SNS
Amazon
S3
function
Amazon
DynamoDB
Amazon
Kinesis
changes
AWS Lambda
service
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 를 트리거하는 이벤트
데이터 저장소
Amazon S3 Amazon
DynamoDB
Amazon
Kinesis
Amazon
Cognito
엔드포인트
Amazon
API Gateway
AWS IoT AWS Step
Functions
Amazon
Alexa
구성 저장소
AWS
CloudFormation
AWS
CloudTrail
AWS
CodeCommit
Amazon
CloudWatch
이벤트 / 메시지 서비스
Amazon SES Amazon SNS Cron events
… 그리고 계속 추가 되고 있습니다!
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 에서 컨테이너 이미지를 통해 함수 패키징과 배포
Lambda 컨테이너 이미지 지원
컨테이너 및 Lambda 기반 애플리케이션에 일관된
도구 사용
AWS 나 3rd 파티에서 제공하는 최대 10GB 크기의
이미지로 대규모 애플리케이션 배포
빠른 스케일링, 고가용성, 140개의 기본 서비스 통합,
사용량에 따른 비용 지불 등의 장점
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
기존 3,008 MB 에서 3x 증가
Lambda 함수에서 최대 10GB 메모리 프로비저닝
Lambda 함수에 최대 10GB 의 메모리를 할당
각 실행 환경에 최대 6개의 vCPU 할당
배치, ETL 등 컴퓨팅 집약적인 워크로드에 활용 가능
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
최대 70% 의 Lambda 비용 절감
Lambda 함수 결제 기간 단위 1ms 로 축소
비용 청구 단위를 100ms 에서 1ms 로 축소로
최대 70% 의 Lambda 함수 비용 절감
2020년 12월 요금부터 반영
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 내부
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Users
Availability Zone1
Availability Zone 2, etc.
Frontend Worker manager Placement
AWS Cloud
Region
Lambda 내부 아키텍처
Workers
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 내부 아키텍처: 콜드 스타트
Users
2. ReserveSandbox
Availability Zone1
Availability Zone 2, etc.
Frontend Worker manager Placement
3. ClaimSandbox
Workers
AWS Cloud
Region
1. Invoke
4. CreateSandbox
5. Init
6. Invoke
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 내부 아키텍처: 웜 스타트
Users
2. ReserveSandbox
Availability Zone1
Availability Zone 2, etc.
Frontend Worker manager Placement
AWS Cloud
Region
3. Prepare
1. Invoke
Workers
4. Invoke
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 를 활용한
서버리스 애플리케이션
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 일반적인 서버리스 아키텍처
Amazon API
Gateway
AWS
Lambda
Amazon
RDS
Client
Serverless microservice
•동기식 API
•관계형 데이터베이스
•AWS Lambda 에서 다양한 로직 수행
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
발생 가능한 문제들
가장 일반적인 서버리스 아키텍처
Amazon API
Gateway
AWS
Lambda
Amazon
RDS
Client
Serverless microservice
•DB 부하가 쿼리 성능, Lambda 실행
시간에 영향
•Lambda 실행 시간이 길수록 비용이
증가, API Gateway 타임 아웃 발생
가능 (29초)
•Lambda 타임 아웃 발생 가능 (15분)
•Lambda 버스트 스로틀링으로 인한
API Gateway 오류 발생 가능
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 스로틀링
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
스로틀링 포인트
Lambda 함수 세부 정보
•평균 실행 시간: 3 초
•평균 RPS: 3,000
•평균 Lambda 동시성: 9,000
Amazon RDS
PostgreSQL
Function
Client
Amazon API
Gateway
Lambda 스로틀링
API 타임아웃
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 동시성
https://docs.aws.amazon.com/lambda/latest/dg/scaling.html
Lambda 함수 스케일링
•함수를 처음 호출하면 Lambda 가 함수의 인스턴스를 생성합니다.
•함수가 응답을 반환한 뒤에도 추가 이벤트 처리를 위해 인스턴스는 일정 기간 유지됩니다.
•첫 번째 이벤트를 처리하는 중에 함수 호출이 이루어지면 Lambda 가 다른 인스턴스를
생성합니다.
•더 많은 이벤트가 발생하면 Lambda 는 이를 사용 가능한 인스턴스로 라우팅하고 필요에
따라 새 인스턴스를 생성합니다. 함수의 동시성은 주어진 시간에 요청을 처리하는 인스턴스의
수를 이야기 합니다.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 동시성 vs 처리량
동시성
• 평균 함수 실행 시간 (초) * 평균 요청/초 = 동시성
• 0.2 초 * 5 req/sec = 1
함수 실행 컨텍스트 재사용
• 함수의 handler 외부의 코드는 호출시 재사용 가능
• 정적 생성자, 전역/정적 변수, 데이터베이스 연결 등에 활용
동시성 = 1
200 ms 200 ms
200 ms 200 ms 200 ms
처리량 = 5 req/sec
1회 호출
실행 컨텍스트 재사용
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
주의 할 점
• 트래픽이 꾸준하다는 가정하에 함수의 실행 시간이 길어지면 동시성은 증가
• 일반적인 원인은 함수의 높은 종속성이나 코드 변경에 따른 지연 시간 증가
• 동시성이 증가하면 실행 컨텍스트도 함께 증가하고, 유지되는 연결도 증가 (ex. DB 연결)
Lambda 동시성 vs 처리량
동시성 = 1
200 ms 200 ms
200 ms 200 ms 200 ms
처리량 = 5 req/sec
1회 호출
실행 컨텍스트 재사용
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 스케일링
Lambda 함수 스케일링
•초기 트래픽이 급증하면 함수의 동시성은 사용하는 AWS 리전에 따라 500 에서 3,000 사이의
초기 버스트 한도에 도달합니다.
•초기 버스트 한도 이후에 동시성은 1분마다 추가로 500개의 인스턴스 씩 확장됩니다. 이는
모든 요청을 처리하기에 충분한 인스턴스를 확보하거나 계정의 동시성 한도에 도달 할
때까지 계속 됩니다.
https://docs.aws.amazon.com/lambda/latest/dg/scaling.html
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 스케일링 예시
계정의 동시성 한도는 4,000 으로
증가시킨 상태
(기본은 1,000)
최초 1,000개의 요청은 바로 처리
이 후 3,000 개의 요청이 추가:
2,000 개는 응답, 이후 요청은
버스트 한도
이후 1분마다 500개의 추가
인스턴스가 할당되고
계정 한도인 4,000 에 도달
0
500
1000
1500
2000
2500
3000
3500
4000
Baseline Burst 4,000 Min 1 Min 2 Min 3
Throttle Throttle
Concurrency limit = 4,000 (increased, us-east-1)
Burst limit = 3,000 (default, us-east-1)
Reqs Handled New Reqs Handled Reqs Throttled
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda 리전 별 버스트 한도
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Provisioned Concurrency
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Provisioned Concurrency
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda Provisioned Concurrency
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 최적화
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
타임 아웃 포인트 1
Amazon RDS
PostgreSQL
Function
Client
Amazon API
Gateway
함수 타임아웃
Lambda 함수 세부 정보
•평균 실행 시간: 3 초
•평균 RPS: 3,000
•평균 Lambda 동시성: 9,000
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
무거운 Lambda 함수의 최적화
비효율적인 방법:
AWS Lambda Function
…
conn = dbClient.createConnection(params);
def lambda_handler(event, context):
future1 = doThing1Async(conn)
future2 = doThing2Async(conn)
wait.forAll(future1, future2)
doThing3(conn)
}
AWS Step Functions Workflow
더 효율적인 방법:
handler(e, c):
doThing1(conn)
}
handler(e, c):
doThing2(conn)
}
handler(e, c):
doThing3(conn)
}
Function2
Function1 Function2
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Step Functions
Step Functions
Amazon
SNS
Amazon
SQS
Amazon
SageMaker
AWS Glue AWS Batch Amazon ECS AWS
Fargate
• 코드 외부에서 오케스트레이션 유지
• 각 단계를 자동으로 트리거 및 추적하고
오류가 있을 경우 재시도 및 로깅 지원
더 많은 서비스와 통합
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
코딩 문제로 성능 저하 및 스로틀링 발생
db = os.environ['RDS_DB_NAME’]
user = os.environ['RDS_USERNAME’]
pw_arn = os.environ[‘RDS_PASSWORD_ARN’]
def lambda_handler(event, context):
pw = secretsManager.get_secret_value(
SecretId = pw_arn)
conn = openConnection(db, user, pw)
…
Lambda 코드 예제 API 스로틀링
GetSecretValue = 1,500 requests/sec
모든 함수 호출
때마다 실행
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
코드 최적화
db = os.environ['RDS_DB_NAME’]
user = os.environ['RDS_USERNAME’]
pw_arn = os.environ[‘RDS_PASSWORD_ARN’]
pw = secretsManager.get_secret_value(
SecretId=pw_arn)
conn = openConnection(db, user, pw)
def lambda_handler(event, context):
…
Sample Lambda code 최적화
• 자격 증명 등은 Lambda 핸들러 외부에서
가져옴 (초기화 시에만 수행)
• 더 자주 새로 고침을 해야하는 경우, 만료
정책이 있는 인-메모리 캐시를 활용
※ AWS Systems Manager 의 Parameter Store 에서
구성 데이터를 가져오는 것과 유사하게 접근
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
타임 아웃 포인트 2
Amazon RDS
PostgreSQL
Function
Client
Amazon API
Gateway
DB 커넥션 관리
문제
Lambda 함수 세부 정보
•평균 실행 시간: 3 초
•평균 RPS: 3,000
•평균 Lambda 동시성: 9,000
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Connection pooling
RDS
Proxy
App
App
App
Amazon RDS를 위한 완전 관리형 데이터베이스 프록시
Amazon RDS Proxy
DB 연결 풀링
장애 조치 시간 단축
데이터 보안 관리
완전 관리형 서비스
AWS Secrets Manager 와 IAM 을 통한
보안 향상
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 모범 아키텍처 패턴
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
운영 안정성 보안 성능 비용
AWS Lambda
Client
Amazon API Gateway Amazon RDS
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
운영 안정성 보안 성능 비용
AWS Lambda
Client
Amazon API Gateway Amazon RDS
Amazon CloudWatch
Logs & metrics
AWS X-Ray
• 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선
• CloudWatch Embedded Metric 형식으로 비동기
메트릭 생성
모범 사례
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
운영 안정성 보안 성능 비용
AWS Lambda
Client
x-api-key
100 RPS
50 burst
Amazon API Gateway Amazon RDS
Amazon CloudWatch
Logs & metrics
AWS X-Ray
• 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선
• CloudWatch Embedded Metric 형식으로 비동기
메트릭 생성
• RPS 에 따라 Lambda Provisioned 구성
모범 사례
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
운영 안정성 보안 성능 비용
AWS Lambda
AWS Secrets Manager
Amazon API Gateway Amazon RDS
Amazon CloudWatch
Logs & metrics
AWS X-Ray
• 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선
• CloudWatch Embedded Metric 형식으로 비동기
메트릭 생성
• RPS 에 따라 Lambda Provisioned 구성
• AWS Secrets Manager 로 자격 증명을 보다
안전하게 관리
모범 사례
Client
x-api-key
100 RPS
50 burst
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
운영 안정성 보안 성능 비용
AWS Lambda
AWS Secrets Manager
Amazon API Gateway
Regional endpoint with RDS Proxy
Amazon RDS
Amazon CloudWatch
Logs & metrics
AWS X-Ray
• 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선
• CloudWatch Embedded Metric 형식으로 비동기
메트릭 생성
• RPS 에 따라 Lambda Provisioned 구성
• AWS Secrets Manager 로 자격 증명을 보다
안전하게 관리
• RDS Proxy 를 통해 효과적인 DB 커넥션 관리
• 리전 API 엔드포인트는 HTTP2 지원
모범 사례
Client
x-api-key
100 RPS
50 burst
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
가장 자주 사용되는 REST API 기반 애플리케이션
모범 사례
AWS Lambda
AWS Secrets Manager
Amazon API Gateway
Regional endpoint with RDS Proxy
Amazon RDS
운영 안정성 보안 성능 비용
• 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선
• CloudWatch Embedded Metric 형식으로 비동기
메트릭 생성
• RPS 에 따라 Lambda Provisioned 구성
• AWS Secrets Manager 로 자격 증명을 보다
안전하게 관리
• RDS Proxy 를 통해 효과적인 DB 커넥션 관리
• 리전 API 엔드포인트는 HTTP2 지원
• Lambda Insights 를 통해 함수를 모니터링하고
최적의 메모리 할당
Amazon CloudWatch
Logs & metrics
AWS X-Ray
Client
x-api-key
100 RPS
50 burst
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
마치며
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 모범 사례
•코드 패키지 사이즈를 최소화
• Lambda Handler와 Core Logic을 분리
•실행 컨텍스트 재사용을 적극 활용
• Environment Variable을 적극 활용
•자바의 경우 .jar 파일들을 별도의 /lib 디렉토리에 배치
• Max Memory Used 사이즈는 Lambda Function 실행에 충분할 정도만
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda 모범 사례
•스로틀링에 대비
•반드시 필요한 경우에만 Lambda 를 VPC 에 배치
• CloudWatch Custom Metrics 을 통해 Lambda 에 필요한 지표를 기록
• X-Ray 를 이용해 Lambda 함수 트레이스
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
여러분의 소중한 피드백을 기다립니다.
강연 종료 후, 강연 평가에 참여해 주세요!
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
감사합니다
© 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.

대용량 트래픽을 처리하는 최적의 서버리스 애플리케이션 - 안효빈, 구성완 AWS 솔루션즈 아키텍트 :: AWS Summit Seoul 2021

  • 1.
    K O RE A | M A Y 1 1 - 1 2 , 2 0 2 1
  • 2.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 대용량 트래픽을 처리하는 최적의 서버리스 애플리케이션 안효빈 솔루션즈 아키텍트 AWS 구성완 솔루션즈 아키텍트 AWS
  • 3.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 4.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 서버리스 애플리케이션 함수 Node.js Python Java C# Go Ruby Runtime API
  • 5.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 함수 Node.js Python Java C# Go Ruby Runtime API 이벤트 소스 데이터 상태 변화 엔드포인트 요청 리소스 상태 변화 서버리스 애플리케이션
  • 6.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 서버리스 애플리케이션 함수 Node.js Python Java C# Go Ruby Runtime API 이벤트 소스 서비스 데이터 상태 변화 엔드포인트 요청 리소스 상태 변화
  • 7.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 실행 모델 비동기 동기 스트림 Amazon API Gateway /order Lambda function reqs Lambda function Amazon SNS Amazon S3 function Amazon DynamoDB Amazon Kinesis changes AWS Lambda service
  • 8.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 를 트리거하는 이벤트 데이터 저장소 Amazon S3 Amazon DynamoDB Amazon Kinesis Amazon Cognito 엔드포인트 Amazon API Gateway AWS IoT AWS Step Functions Amazon Alexa 구성 저장소 AWS CloudFormation AWS CloudTrail AWS CodeCommit Amazon CloudWatch 이벤트 / 메시지 서비스 Amazon SES Amazon SNS Cron events … 그리고 계속 추가 되고 있습니다!
  • 9.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 에서 컨테이너 이미지를 통해 함수 패키징과 배포 Lambda 컨테이너 이미지 지원 컨테이너 및 Lambda 기반 애플리케이션에 일관된 도구 사용 AWS 나 3rd 파티에서 제공하는 최대 10GB 크기의 이미지로 대규모 애플리케이션 배포 빠른 스케일링, 고가용성, 140개의 기본 서비스 통합, 사용량에 따른 비용 지불 등의 장점
  • 10.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 기존 3,008 MB 에서 3x 증가 Lambda 함수에서 최대 10GB 메모리 프로비저닝 Lambda 함수에 최대 10GB 의 메모리를 할당 각 실행 환경에 최대 6개의 vCPU 할당 배치, ETL 등 컴퓨팅 집약적인 워크로드에 활용 가능
  • 11.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 최대 70% 의 Lambda 비용 절감 Lambda 함수 결제 기간 단위 1ms 로 축소 비용 청구 단위를 100ms 에서 1ms 로 축소로 최대 70% 의 Lambda 함수 비용 절감 2020년 12월 요금부터 반영
  • 12.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 내부
  • 13.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Users Availability Zone1 Availability Zone 2, etc. Frontend Worker manager Placement AWS Cloud Region Lambda 내부 아키텍처 Workers
  • 14.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 내부 아키텍처: 콜드 스타트 Users 2. ReserveSandbox Availability Zone1 Availability Zone 2, etc. Frontend Worker manager Placement 3. ClaimSandbox Workers AWS Cloud Region 1. Invoke 4. CreateSandbox 5. Init 6. Invoke
  • 15.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 내부 아키텍처: 웜 스타트 Users 2. ReserveSandbox Availability Zone1 Availability Zone 2, etc. Frontend Worker manager Placement AWS Cloud Region 3. Prepare 1. Invoke Workers 4. Invoke
  • 16.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 를 활용한 서버리스 애플리케이션
  • 17.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 일반적인 서버리스 아키텍처 Amazon API Gateway AWS Lambda Amazon RDS Client Serverless microservice •동기식 API •관계형 데이터베이스 •AWS Lambda 에서 다양한 로직 수행
  • 18.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 발생 가능한 문제들 가장 일반적인 서버리스 아키텍처 Amazon API Gateway AWS Lambda Amazon RDS Client Serverless microservice •DB 부하가 쿼리 성능, Lambda 실행 시간에 영향 •Lambda 실행 시간이 길수록 비용이 증가, API Gateway 타임 아웃 발생 가능 (29초) •Lambda 타임 아웃 발생 가능 (15분) •Lambda 버스트 스로틀링으로 인한 API Gateway 오류 발생 가능
  • 19.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 스로틀링
  • 20.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 스로틀링 포인트 Lambda 함수 세부 정보 •평균 실행 시간: 3 초 •평균 RPS: 3,000 •평균 Lambda 동시성: 9,000 Amazon RDS PostgreSQL Function Client Amazon API Gateway Lambda 스로틀링 API 타임아웃
  • 21.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 동시성 https://docs.aws.amazon.com/lambda/latest/dg/scaling.html Lambda 함수 스케일링 •함수를 처음 호출하면 Lambda 가 함수의 인스턴스를 생성합니다. •함수가 응답을 반환한 뒤에도 추가 이벤트 처리를 위해 인스턴스는 일정 기간 유지됩니다. •첫 번째 이벤트를 처리하는 중에 함수 호출이 이루어지면 Lambda 가 다른 인스턴스를 생성합니다. •더 많은 이벤트가 발생하면 Lambda 는 이를 사용 가능한 인스턴스로 라우팅하고 필요에 따라 새 인스턴스를 생성합니다. 함수의 동시성은 주어진 시간에 요청을 처리하는 인스턴스의 수를 이야기 합니다.
  • 22.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 동시성 vs 처리량 동시성 • 평균 함수 실행 시간 (초) * 평균 요청/초 = 동시성 • 0.2 초 * 5 req/sec = 1 함수 실행 컨텍스트 재사용 • 함수의 handler 외부의 코드는 호출시 재사용 가능 • 정적 생성자, 전역/정적 변수, 데이터베이스 연결 등에 활용 동시성 = 1 200 ms 200 ms 200 ms 200 ms 200 ms 처리량 = 5 req/sec 1회 호출 실행 컨텍스트 재사용
  • 23.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 주의 할 점 • 트래픽이 꾸준하다는 가정하에 함수의 실행 시간이 길어지면 동시성은 증가 • 일반적인 원인은 함수의 높은 종속성이나 코드 변경에 따른 지연 시간 증가 • 동시성이 증가하면 실행 컨텍스트도 함께 증가하고, 유지되는 연결도 증가 (ex. DB 연결) Lambda 동시성 vs 처리량 동시성 = 1 200 ms 200 ms 200 ms 200 ms 200 ms 처리량 = 5 req/sec 1회 호출 실행 컨텍스트 재사용
  • 24.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 스케일링 Lambda 함수 스케일링 •초기 트래픽이 급증하면 함수의 동시성은 사용하는 AWS 리전에 따라 500 에서 3,000 사이의 초기 버스트 한도에 도달합니다. •초기 버스트 한도 이후에 동시성은 1분마다 추가로 500개의 인스턴스 씩 확장됩니다. 이는 모든 요청을 처리하기에 충분한 인스턴스를 확보하거나 계정의 동시성 한도에 도달 할 때까지 계속 됩니다. https://docs.aws.amazon.com/lambda/latest/dg/scaling.html
  • 25.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 스케일링 예시 계정의 동시성 한도는 4,000 으로 증가시킨 상태 (기본은 1,000) 최초 1,000개의 요청은 바로 처리 이 후 3,000 개의 요청이 추가: 2,000 개는 응답, 이후 요청은 버스트 한도 이후 1분마다 500개의 추가 인스턴스가 할당되고 계정 한도인 4,000 에 도달 0 500 1000 1500 2000 2500 3000 3500 4000 Baseline Burst 4,000 Min 1 Min 2 Min 3 Throttle Throttle Concurrency limit = 4,000 (increased, us-east-1) Burst limit = 3,000 (default, us-east-1) Reqs Handled New Reqs Handled Reqs Throttled
  • 26.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda 리전 별 버스트 한도
  • 27.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda Provisioned Concurrency
  • 28.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda Provisioned Concurrency
  • 29.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Lambda Provisioned Concurrency
  • 30.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 최적화
  • 31.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 타임 아웃 포인트 1 Amazon RDS PostgreSQL Function Client Amazon API Gateway 함수 타임아웃 Lambda 함수 세부 정보 •평균 실행 시간: 3 초 •평균 RPS: 3,000 •평균 Lambda 동시성: 9,000
  • 32.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 무거운 Lambda 함수의 최적화 비효율적인 방법: AWS Lambda Function … conn = dbClient.createConnection(params); def lambda_handler(event, context): future1 = doThing1Async(conn) future2 = doThing2Async(conn) wait.forAll(future1, future2) doThing3(conn) } AWS Step Functions Workflow 더 효율적인 방법: handler(e, c): doThing1(conn) } handler(e, c): doThing2(conn) } handler(e, c): doThing3(conn) } Function2 Function1 Function2
  • 33.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Step Functions Step Functions Amazon SNS Amazon SQS Amazon SageMaker AWS Glue AWS Batch Amazon ECS AWS Fargate • 코드 외부에서 오케스트레이션 유지 • 각 단계를 자동으로 트리거 및 추적하고 오류가 있을 경우 재시도 및 로깅 지원 더 많은 서비스와 통합
  • 34.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 코딩 문제로 성능 저하 및 스로틀링 발생 db = os.environ['RDS_DB_NAME’] user = os.environ['RDS_USERNAME’] pw_arn = os.environ[‘RDS_PASSWORD_ARN’] def lambda_handler(event, context): pw = secretsManager.get_secret_value( SecretId = pw_arn) conn = openConnection(db, user, pw) … Lambda 코드 예제 API 스로틀링 GetSecretValue = 1,500 requests/sec 모든 함수 호출 때마다 실행
  • 35.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 코드 최적화 db = os.environ['RDS_DB_NAME’] user = os.environ['RDS_USERNAME’] pw_arn = os.environ[‘RDS_PASSWORD_ARN’] pw = secretsManager.get_secret_value( SecretId=pw_arn) conn = openConnection(db, user, pw) def lambda_handler(event, context): … Sample Lambda code 최적화 • 자격 증명 등은 Lambda 핸들러 외부에서 가져옴 (초기화 시에만 수행) • 더 자주 새로 고침을 해야하는 경우, 만료 정책이 있는 인-메모리 캐시를 활용 ※ AWS Systems Manager 의 Parameter Store 에서 구성 데이터를 가져오는 것과 유사하게 접근
  • 36.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 타임 아웃 포인트 2 Amazon RDS PostgreSQL Function Client Amazon API Gateway DB 커넥션 관리 문제 Lambda 함수 세부 정보 •평균 실행 시간: 3 초 •평균 RPS: 3,000 •평균 Lambda 동시성: 9,000
  • 37.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. Connection pooling RDS Proxy App App App Amazon RDS를 위한 완전 관리형 데이터베이스 프록시 Amazon RDS Proxy DB 연결 풀링 장애 조치 시간 단축 데이터 보안 관리 완전 관리형 서비스 AWS Secrets Manager 와 IAM 을 통한 보안 향상
  • 38.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 모범 아키텍처 패턴
  • 39.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 운영 안정성 보안 성능 비용 AWS Lambda Client Amazon API Gateway Amazon RDS
  • 40.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 운영 안정성 보안 성능 비용 AWS Lambda Client Amazon API Gateway Amazon RDS Amazon CloudWatch Logs & metrics AWS X-Ray • 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선 • CloudWatch Embedded Metric 형식으로 비동기 메트릭 생성 모범 사례
  • 41.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 운영 안정성 보안 성능 비용 AWS Lambda Client x-api-key 100 RPS 50 burst Amazon API Gateway Amazon RDS Amazon CloudWatch Logs & metrics AWS X-Ray • 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선 • CloudWatch Embedded Metric 형식으로 비동기 메트릭 생성 • RPS 에 따라 Lambda Provisioned 구성 모범 사례
  • 42.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 운영 안정성 보안 성능 비용 AWS Lambda AWS Secrets Manager Amazon API Gateway Amazon RDS Amazon CloudWatch Logs & metrics AWS X-Ray • 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선 • CloudWatch Embedded Metric 형식으로 비동기 메트릭 생성 • RPS 에 따라 Lambda Provisioned 구성 • AWS Secrets Manager 로 자격 증명을 보다 안전하게 관리 모범 사례 Client x-api-key 100 RPS 50 burst
  • 43.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 운영 안정성 보안 성능 비용 AWS Lambda AWS Secrets Manager Amazon API Gateway Regional endpoint with RDS Proxy Amazon RDS Amazon CloudWatch Logs & metrics AWS X-Ray • 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선 • CloudWatch Embedded Metric 형식으로 비동기 메트릭 생성 • RPS 에 따라 Lambda Provisioned 구성 • AWS Secrets Manager 로 자격 증명을 보다 안전하게 관리 • RDS Proxy 를 통해 효과적인 DB 커넥션 관리 • 리전 API 엔드포인트는 HTTP2 지원 모범 사례 Client x-api-key 100 RPS 50 burst
  • 44.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 가장 자주 사용되는 REST API 기반 애플리케이션 모범 사례 AWS Lambda AWS Secrets Manager Amazon API Gateway Regional endpoint with RDS Proxy Amazon RDS 운영 안정성 보안 성능 비용 • 액세스 로그를 활성화 하고 로그 구조화 및 코드 개선 • CloudWatch Embedded Metric 형식으로 비동기 메트릭 생성 • RPS 에 따라 Lambda Provisioned 구성 • AWS Secrets Manager 로 자격 증명을 보다 안전하게 관리 • RDS Proxy 를 통해 효과적인 DB 커넥션 관리 • 리전 API 엔드포인트는 HTTP2 지원 • Lambda Insights 를 통해 함수를 모니터링하고 최적의 메모리 할당 Amazon CloudWatch Logs & metrics AWS X-Ray Client x-api-key 100 RPS 50 burst
  • 45.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. 마치며
  • 46.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 모범 사례 •코드 패키지 사이즈를 최소화 • Lambda Handler와 Core Logic을 분리 •실행 컨텍스트 재사용을 적극 활용 • Environment Variable을 적극 활용 •자바의 경우 .jar 파일들을 별도의 /lib 디렉토리에 배치 • Max Memory Used 사이즈는 Lambda Function 실행에 충분할 정도만
  • 47.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS Lambda 모범 사례 •스로틀링에 대비 •반드시 필요한 경우에만 Lambda 를 VPC 에 배치 • CloudWatch Custom Metrics 을 통해 Lambda 에 필요한 지표를 기록 • X-Ray 를 이용해 Lambda 함수 트레이스
  • 48.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. 여러분의 소중한 피드백을 기다립니다. 강연 종료 후, 강연 평가에 참여해 주세요! © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.
  • 49.
    © 2021, AmazonWeb Services, Inc. or its affiliates. All rights reserved. 감사합니다 © 2021, Amazon Web Services, Inc. or its affiliates. All rights reserved.