Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Bertrand PINEL
ember-aws-ehipster
Commits
be992af8
Commit
be992af8
authored
Dec 02, 2018
by
Bertrand PINEL
Browse files
fully working terraform script
parent
4bb80bb3
Changes
4
Hide whitespace changes
Inline
Side-by-side
cloud/lambda/lambda-jsonapi.js
View file @
be992af8
...
...
@@ -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
(
'
JSONAPI
Lambda
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
(
"
JSONAPI
Lambda
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
(
'
JSONAPI
Lambda
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
(
'
JSONAPI
Lambda
Lambda GET multiple values with params:
'
,
params
);
}
dbGet
(
params
).
then
(
(
data
)
=>
{
console
.
log
(
'
||
JSONAPI
||
Lambda GET data received:
'
,
data
);
console
.
log
(
'
JSONAPI
Lambda
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
(
`JSONAPI
Lambda
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
(
`JSONAPI
Lambda
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
(
`JSONAPI
Lambda
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
(
'
JSONAPI
Lambda
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
(
"
JSONAPI
Lambda
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
(
`JSONAPI
Lambda
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
(
`
JSONAPI
Lambda
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
(
`JSONAPI
Lambda
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
(
"
JSONAPI
Lambda
********************** Received Event *******************
\n
"
+
JSON
.
stringify
(
event
));
console
.
log
(
"
JSONAPI
Lambda
httpMethod=
"
+
event
.
context
.
httpMethod
);
switch
(
event
.
context
.
httpMethod
)
{
case
'
GET
'
:
getMethod
(
event
,
context
,
callback
);
...
...
cloud/terraform/README.md
View file @
be992af8
...
...
@@ -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 :
```
...
...
cloud/terraform/api-gateway.tf
View file @
be992af8
...
...
@@ -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
.
200
TypeGet
.
status_code
}
"
response_templates
=
{
"application/json"
=
""
}
}
# Setup Integration Response for status code 200 and POST on {type}
...
...
cloud/terraform/lambda.tf
View file @
be992af8
...
...
@@ -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"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment