Skip to content
Snippets Groups Projects
Commit be992af8 authored by Bertrand PINEL's avatar Bertrand PINEL
Browse files

fully working terraform script

parent 4bb80bb3
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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 :
```
......
......@@ -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}
......
......@@ -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"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment