땅콩이 맛난고 먹이자

0827_🌏 SERVERLESS FRAMEWORK & 💾 JWT & 🥁 AWS Lambda 본문

HJ_웹 맹글기

0827_🌏 SERVERLESS FRAMEWORK & 💾 JWT & 🥁 AWS Lambda

pea_Nut 2020. 8. 27. 14:58

🌏 SERVERLESS FRAMEWORK

 

AWS Lamda function의 개발과 배포에 도움을 준다.

"allowing you to focus on building sophisticated, event-driven, serverless architectures, comprised of Functions and Events."

 

# FUNCTIONS

- AWS 람다 함수

- 독립적인 배포 유닛

- 하나의 일을 수행하도록 작성 됨 (예: db에 user 정보 저장)

- 하나의 함수에 하나의 job 작성 권장

 

 

# EVENTS

- AWS 람다 함수를 트리거 하는 모든 것들을 이벤트라고 함

- 이벤트는 AWS의 infrastructure  event (예: An AWS API Gateway HTTP endpoint request (e.g., for a REST API))

- 서버리스 프레임워크에서 AWS 람다 함수를 작성하면, 프레임워크가 해당 이벤트에 필요한 모든 인프라(예: API Gateway endpoint )들을 자동 생성하고 이를 수신하도록 AWS 람다 함수를 구성해준다.

 

 

# RESOURCES

- 함수가 사용하는 AWS 인프라 구성 요소 (예: An AWS DynamoDB Table(e.g., for saving Users/Posts/Comments data))

- 함수와 이벤트와 더불어 AWS 인프라 구성 요소도 같이 배포함

 

 

# SERVICES

- 프레임 워크의 조직 단위, project file

- 하나의 머플리케이션에 여러개의 서비스를 가질 수 있음

- 함수, 이벤트, 리소스들을 정의하는 곳

- YAML 또는 JSON 포멧으로 작성됨, 파일이름은 serverless.yml 또는 serverless.json, root directory에 위치함

 

service 작성 예시

- 프레임 워크는 JSON 객체를 Javascript file로 내보낼 수 있음 (프로젝트가 Node.js 로 되어있으면 이 작업이 유리함)

js 로 JSON object 내보내는 예시

- serverless deploy 로 프레임 워크를 running 하면 모든 service configuration file 이 한꺼번에 배포됨

 

 

 

 💾 JWT_JSON Web Token

 

개념

- JSON 전송을 안전하게 해주는 컴펙트하고 독립적인 방식 

- digital signed 이기 때문에 verified & trusted

 

사용

# Authorization

- Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token.

- Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

# Information Exchange

- JSON Web Tokens are a good way of securely transmitting information between parties.

- As the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

 

구조

dot(.)으로 나눠짐

Header.Payload.Signature xxxxx.yyyyy.zzzzz

 

# Header
- type(JWT)과 사용되는 signing algorithm(HMAC SHA256 or RSA)으로 구성되어 있음

# Payload

- claims를 포함하고 있음

- Claims: statements about an entity (typically, the user) and additional data.

- Types of claims

  • Registered claims: 미리 정의된 claim 집합 (권장)
    • iss(issuer), exp(expiration time), sub(subject), aud(audience), and others
  • Public claims: 사용자 마음대로 정의
  • Private claims: 공개하지 않는 사용자 지정 claim

# Signature

- signature 부분을 생성하기 위해서는 encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign 이 필요함

ex) 만약 HMAC SHA256 algorithm 을 사용한다면 signature은 다음과 같은 방식으로 구해질 것이다.

- The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.

 

# 다 합치면?

- output: 점으로 구분되어있는 Base64-URL

- easily passed in HTML and HTTP environments

JWT 예시

 

 

작동 원리

- 사용자가 로그인을 성공하면 JWT token이 리턴된다. 

- Whenever the user wants to access a protected route or resource, the user agent should send the JWT, typically in the Authorization header using the Bearer schema.

header 예시

- 서버의 보호되는 라우트는 해당 JWT의 Authorization header를 체크하고, 패스되면 해당 유저는 보호되는 리소스에 접근 가능하게 됨

- 만약 JWT가 necessary data를 가지고 있으면 db를 쿼리해야할 수 도 있음 

- 토큰이 Authorization header로 전송되는 경우 CORS는 문제되지 않음

 

  1. 사용자가 authorization server 로 토큰을 요구함
  2. authorizaion이 부여되면 토큰이 부여됨
  3. 사용자는 그 토큰을 가지고 보호되는 자원에 접근가능하게 됨

 

 

🥁 AWS Lambda

 

- AWS Lambda를 사용하여 Amazon S3 버킷 또는 Amazon DynamoDB 테이블의 데이터 변경과 같은 이벤트에 대한 응답으로 코드를 실행할 수 있다.

- Amazon API Gateway를 사용하여 HTTP 요청에 대한 응답으로 코드를 실행할 수도 있다.

- AWS SDK를 사용하여 만든 API 호출을 통해 코드를 호출할 수 있다.

 

=> Lambda를 사용하여 Amazon S3 및 Amazon DynamoDB와 같은 AWS 서비스에 대한 데이터 처리 트리거를쉽게 빌드!

 

 

 

자료 출처

'HJ_웹 맹글기' 카테고리의 다른 글

🏠Node.js  (0) 2020.08.08
0723_API 호출/ HOOK/ export 기본 개념  (0) 2020.07.23
0722  (0) 2020.07.23
0719_Django 개념 공부 (2)  (0) 2020.07.19
0717&19_Django 개념 공부  (0) 2020.07.19