Louie NRT Story

[AWS] API Gateway -> Lambda 본문

서버시스템

[AWS] API Gateway -> Lambda

hyeok0724.kim@gmail.com 2020. 1. 27. 19:39
반응형

Writed: 27 JAN 2020

 

Index

1. Make Lambda Function

2. Create API Gateway

3. Test an API's invoke url

 

1. Make Lambda Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
console.log('Loading hello world function');
 
exports.handler = async (event=> {
    let name = "you";
    let city = 'World';
    let time = 'day';
    let day = '';
    let responseCode = 200;
    console.log("request: " + JSON.stringify(event));
    
    if (event.queryStringParameters && event.queryStringParameters.name) {
        console.log("Received name: " + event.queryStringParameters.name);
        name = event.queryStringParameters.name;
    }
    
    if (event.queryStringParameters && event.queryStringParameters.city) {
        console.log("Received city: " + event.queryStringParameters.city);
        city = event.queryStringParameters.city;
    }
    
    if (event.headers && event.headers['day']) {
        console.log("Received day: " + event.headers.day);
        day = event.headers.day;
    }
    
    if (event.body) {
        let body = JSON.parse(event.body)
        if (body.time) 
            time = body.time;
    }
 
    let greeting = `Good ${time}, ${name} of ${city}.`;
    if (day) greeting += ` Happy ${day}!`;
 
    let responseBody = {
        message: greeting,
        input: event
    };
    
    // The output from a Lambda proxy integration must be 
    // in the following JSON object. The 'headers' property 
    // is for custom response headers in addition to standard 
    // ones. The 'body' property  must be a JSON string. For 
    // base64-encoded payload, you must also set the 'isBase64Encoded'
    // property to 'true'.
    let response = {
        statusCode: responseCode,
        headers: {
            "x-custom-header" : "my custom header value"
        },
        body: JSON.stringify(responseBody)
    };
    console.log("response: " + JSON.stringify(response))
    return response;
};
 
 

2. Create API Gateway

1) Create an empty API 

2) Create Resource

3) Create method

 

4) Deploy API

 

5) Note API's invoke API

 

3. Test an API with cURL

1) Download cURL from http://curl.haxx.se/windows 

 

curl

command line tool and library for transferring data with URLs Supports... DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTT

curl.haxx.se

2) Execute curl.exe with Options(Below URL is exmaple)

 

.\curl.exe -v -X POST "https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle" -H "content-type: application/json" -H "day: Thursday"

 

Reference:

https://docs.aws.amazon.com/ko_kr/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

 

https://docs.aws.amazon.com/ko_kr/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html

자습서: Lambda 프록시 통합을 사용하여 Hello World API 빌드

docs.aws.amazon.com

 

 

 

반응형

'서버시스템' 카테고리의 다른 글

[AWS] IoT Core -> Lambda -> DynamoDB (2)  (0) 2020.02.04
[AWS] IoT Core -> Lambda -> DynamoDB (1)  (0) 2020.01.27
[AWS] IOT Core Lambda  (0) 2020.01.13
[AWS] IOT Core DynamoDB  (0) 2020.01.13
[AWS] IOT Core SNS 규칙  (0) 2020.01.11
Comments