From be992af853e479a6a95e4897bd46c505df3e5f8d Mon Sep 17 00:00:00 2001
From: bertrand <bpinel@ippon.fr>
Date: Sun, 2 Dec 2018 14:51:14 +0100
Subject: [PATCH] fully working terraform script

---
 cloud/lambda/lambda-jsonapi.js | 30 +++++++++++++++---------------
 cloud/terraform/README.md      |  4 ++--
 cloud/terraform/api-gateway.tf |  3 +++
 cloud/terraform/lambda.tf      |  6 +++---
 4 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/cloud/lambda/lambda-jsonapi.js b/cloud/lambda/lambda-jsonapi.js
index 245f189..1e111b6 100644
--- a/cloud/lambda/lambda-jsonapi.js
+++ b/cloud/lambda/lambda-jsonapi.js
@@ -42,7 +42,7 @@ const createObject = (obj) => {
             objout[(attr==='ObjectType')?'type':'id'] = obj[attr];
         }
     }
-    console.log('||JSONAPI|| Return object is '+JSON.stringify(objout));
+    console.log('JSONAPILambda Return object is '+JSON.stringify(objout));
     return objout;
 }
 
@@ -63,7 +63,7 @@ const createRelationships = (data) => {
 }
 
 const createResponse = (statusCode, body) => {
-    console.log("||JSONAPI|| Body is "+JSON.stringify(body));
+    console.log("JSONAPILambda Body is "+JSON.stringify(body));
     return {
         'statusCode': statusCode,
         'data': createData(body),
@@ -86,31 +86,31 @@ const getMethod = (event, context, callback) => {
             'Id': id
         };
         dbGet = (params) => { return dynamo.get(params).promise() };
-        console.log('||JSONAPI|| Lambda GET single value with params: ', params);
+        console.log('JSONAPILambda Lambda GET single value with params: ', params);
 
     } else {
         params.KeyConditionExpression = 'ObjectType = :objectType';
         params.ExpressionAttributeValues = { ':objectType': type };
         dbGet = (params) => { return dynamo.query(params).promise() };
-        console.log('||JSONAPI|| Lambda GET multiple values with params: ', params);
+        console.log('JSONAPILambda Lambda GET multiple values with params: ', params);
     }
 
     dbGet(params).then( (data) => {
-        console.log('||JSONAPI|| Lambda GET data received: ', data);
+        console.log('JSONAPILambda Lambda GET data received: ', data);
 
         if (id && !data.Item) {
             callback(null, createResponse(404, "ITEM NOT FOUND"));
             return;
         } else if (id && data.Item) {
-            console.log(`||JSONAPI|| RETRIEVED ITEM SUCCESSFULLY WITH doc = ${data.Item}`);
+            console.log(`JSONAPILambda RETRIEVED ITEM SUCCESSFULLY WITH doc = ${data.Item}`);
             callback(null, createResponse(200, data.Item));
         } else {
-            console.log(`||JSONAPI|| RETRIEVED ITEMS SUCCESSFULLY WITH doc = ${data.Items}`);
+            console.log(`JSONAPILambda RETRIEVED ITEMS SUCCESSFULLY WITH doc = ${data.Items}`);
             callback(null, createResponse(200, data.Items));
         }
         
     }).catch( (err) => { 
-        console.log(`||JSONAPI|| GET ITEM FAILED FOR Entry = ${params}, WITH ERROR: ${err}`);
+        console.log(`JSONAPILambda GET ITEM FAILED FOR Entry = ${params}, WITH ERROR: ${err}`);
         callback(null, createResponse(500, err));
     });
 };
@@ -158,16 +158,16 @@ const putMethod = (event, context, callback) => {
         TableName: tableName,
         Item: content
     };
-    console.log('||JSONAPI|| Try saving entity of type '+type+' and content '+JSON.stringify(entry));
+    console.log('JSONAPILambda Try saving entity of type '+type+' and content '+JSON.stringify(entry));
     //let dbPut = (entry) => { return dynamo.put(entry).promise() };
     dynamo.put(entry, function(err, data) {
         if (err) {
-            console.log("||JSONAPI|| Error", err);
+            console.log("JSONAPILambda Error", err);
             callback(null, createResponse(500, 'Error '+err));
         } else {
             body.data.id = id;
             body['statusCode'] = 200;
-            console.log(`||JSONAPI|| PUT ITEM SUCCEEDED WITH data=`+JSON.stringify(body));
+            console.log(`JSONAPILambda PUT ITEM SUCCEEDED WITH data=`+JSON.stringify(body));
             callback(null, body);
         }
     });
@@ -192,17 +192,17 @@ const deleteMethod = (event, context, callback) => {
             callback(null, createResponse(404, "ITEM NOT FOUND FOR DELETION"));
             return;
         }
-        console.log(||JSONAPI|| `DELETED ITEM OF TYPE ${type} SUCCESSFULLY WITH id = ${id}`);
+        console.log(`JSONAPILambda DELETED ITEM OF TYPE ${type} SUCCESSFULLY WITH id = ${id}`);
         callback(null, body);
     }).catch( (err) => { 
-        console.log(`||JSONAPI|| DELETE ITEM OF TYPE ${type} FAILED FOR id = ${id}, WITH ERROR: ${err}`);
+        console.log(`JSONAPILambda DELETE ITEM OF TYPE ${type} FAILED FOR id = ${id}, WITH ERROR: ${err}`);
         callback(null, createResponse(500, err));
     });
 };
 
 exports.handler = (event, context, callback) => {
-    console.log("||JSONAPI|| **********************  Received Event  *******************\n"+ JSON.stringify(event));
-    console.log("||JSONAPI|| httpMethod="+event.context.httpMethod);
+    console.log("JSONAPILambda **********************  Received Event  *******************\n"+ JSON.stringify(event));
+    console.log("JSONAPILambda httpMethod="+event.context.httpMethod);
     switch ( event.context.httpMethod ) {
         case 'GET':
             getMethod(event,context,callback);
diff --git a/cloud/terraform/README.md b/cloud/terraform/README.md
index a018a6b..6612765 100644
--- a/cloud/terraform/README.md
+++ b/cloud/terraform/README.md
@@ -25,8 +25,8 @@ The following operations need to be performed (adapting the region you are using
 
 ```
 zip lambda-jsonapi.zip ../lambda/lambda-jsonapi.js
-aws s3api create-bucket --bucket=lambda-jsonapi-bucket --region=us-east-1
-aws s3 cp lambda-jsonapi-bucket.zip s3://lambda-jsonapi-bucket/v1.0.0/lambda-jsonapi-bucket.zip
+aws s3api create-bucket --bucket=lambda-jsonapi-code-bucket --region=us-east-1
+aws s3 cp lambda-jsonapi.zip s3://lambda-jsonapi-code-bucket/v1.0.0/lambda-jsonapi.zip
 ```
 One this is done, simply run the terraform script :
 ```
diff --git a/cloud/terraform/api-gateway.tf b/cloud/terraform/api-gateway.tf
index 460b0c6..3030fb5 100644
--- a/cloud/terraform/api-gateway.tf
+++ b/cloud/terraform/api-gateway.tf
@@ -152,6 +152,9 @@ resource "aws_api_gateway_integration_response" "200TypeGetIntegrationResponse"
   resource_id = "${aws_api_gateway_resource.typePath.id}"
   http_method = "${aws_api_gateway_method.typePathGet.http_method}"
   status_code = "${aws_api_gateway_method_response.200TypeGet.status_code}"
+  response_templates = {
+    "application/json" = ""
+  }
 }
 
 # Setup Integration Response for status code 200 and POST on {type}
diff --git a/cloud/terraform/lambda.tf b/cloud/terraform/lambda.tf
index 8fa7c62..6f30758 100644
--- a/cloud/terraform/lambda.tf
+++ b/cloud/terraform/lambda.tf
@@ -7,10 +7,10 @@ provider "aws" {
 }
 
 resource "aws_lambda_function" "lambda-jsonapi" {
-  function_name = "JSONAPILambda"
+  function_name = "lambda-jsonapi"
 
-  # The bucket name as created earlier with "aws s3api create-bucket"
-  s3_bucket = "lambda-jsonapi-bucket"
+  # The bucket name as created before running terraform scripts with "aws s3api create-bucket"
+  s3_bucket = "lambda-jsonapi-code-bucket"
   s3_key    = "v1.0.0/lambda-jsonapi.zip"
 
   # "main" is the filename within the zip file (main.js) and "handler"
-- 
GitLab