[AWS] REST API Gateway

sm_cloud_life·2023년 4월 3일
0
post-thumbnail

1. Amazon API Gateway 생성


[경로] Amazon API → Amazon REST API Gateway 구축

프로토콜 선택 - REST

새 API 생성 - 새 API

API 이름 - test-api

  • 엔드포인트 유형 - 지역 [엔드포인트 유형] 지역 : 특정 리전(ap-northeat-2) 안에서 사용 최적화된 에지 : CloudFront를 사용 (일반적인 인터넷 상) 프라이빗 : AWS 내 VPC에서만 접근 가능

API 생성

Resource 설명

여기서의 리소스는 - api를 호출하는 api url을 정의한다라고 보면 된다.

리소스 방식

2. Method 생성


  1. 작업 클릭 후 Method 생성 클릭
  2. POST방식으로 Method 생성
  3. 통합 유형 - AWS 서비스
  4. AWS 리전 - ap-northeast-2
  5. HTTP 메서드 - POST (클라이언트와 서버 사이에 이루어지는 요청(Request)과 응답(Response) 데이터를 전송하는 방식)
  6. 작업 - PutRecord (kinesis에 레코드를 넣는다)
  7. 실행 역할 - Iam
  8. 저장 클릭

3. 메서드 요청 생성


메서드 요청 클릭

요청 검사기 - Validate body (본문 검사를 한다)

URL 쿼리 문자열 파라미터 - stream 생성 (stream으로만 URL을 받는다

콘텐츠 유형 - application/json (appllication/json 본문 검사)

모델 이름 - Empty

4. 통합 요청 생성


통합 요청 클릭

HTTP 헤더

이름 - Content-Type

다음에서 매핑됨 - 'application/x-amz-json-1.1'

application/x-amz-json-1.1(또는 다른 버전)도 JSON 데이터를 요청/수신 하고 위의 문서에 설명된 추가 동작을 기대합니다. (즉, 서버/클라이언트에게 이것이 JSON과 추가 요소임을 알려줌)

매핑 템플릿

요청 본문 패스스루 - 없음

Content-Type - application/json

매핑 탬플릿 구문 - https://docs.aws.amazon.com/ko_kr/kinesis/latest/APIReference/API_PutRecord.html

  • 템플릿 생성
    ```json
    {
    	"StreamName": "$input.params('stream')",
    	"Data": "$util.base64Encode($input.body)",
    	"PartitionKey": "$context.requestId"
    }
    ```
    {
     "StreamName": "api-kds",
     "Data": "$util.base64Encode($input.body)",
     "PartitionKey": "$context.requestId"
    }
    
    **Mapping Templates** 내용 설명:
    
    ※ **Content-Type**: application/json를 입력한 이유는 json 방식으로 데이터를 보낼기 때문에 template 언어를 json으로 설정하겠다는 의미이다.
    
    ※ **General Template** 내용 해석:
    
    ※ **StreamName**: 앞에서 생성한 URL 쿼리 문자열 파라미터 stream만 허용한다는 것이다
    
    ※ **Data**: Kinesis는 Data가 base64 encoded되어있음을 데이터를 변환할 필요가 있음
    
    **PartitionKey**: data stream은 1개 이상의 shard로 이루어지는데, 적절히 랜덤한 Partition Key를 사용하면 PUT 로드가 shard에 골고루 분산되어 좋다.
    
    **참고 사이트 -** [https://docs.aws.amazon.com/ko_kr/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html](https://docs.aws.amazon.com/ko_kr/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html)
    

저장 클릭
### 5. **API 배포** 
___
![](https://velog.velcdn.com/images/osm/post/39e13255-02c0-49c6-b9a0-f38e4832bd91/image.png)


API 배포 클릭

![](https://velog.velcdn.com/images/osm/post/c345bed8-bdf2-450e-b25c-adc2c4c733a3/image.png)


배포 스테이지 - deploy (임의 값)

배포 클릭

**API 배포 주의 할 점**

![](https://velog.velcdn.com/images/osm/post/fbb8c9cf-8f7c-4a0f-889c-90db508c6a71/image.png)


```bash
curl \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
    	"app_id": "com.sokoloff06.sdktest",
        "event_name": "af_cross_promotion",
        "event_time": "2020-05-10 00:57:26.038",
        "idfa": null,
        "advertising_id": "cb654aa6-8026-4633-bfd1-de619896fd6a"
    '} \
    "https://nip0txi325.execute-api.ap-northeast-2.amazonaws.com/deploy?stream=skills-kds"
profile
나의 비밀스러운 클라우드 일상 정리 ❤️

0개의 댓글