-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapigateway-env.js
More file actions
109 lines (97 loc) · 5.52 KB
/
Copy pathapigateway-env.js
File metadata and controls
109 lines (97 loc) · 5.52 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var AWS = require("aws-sdk");
exports.handler = function(region) {
var apigateway = new AWS.APIGateway({ region: region });
apigateway.createRestApi({ name: "LambdaMicroservice" }, function(err, apiData) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(apiData); // successful response
apigateway.getResources({ restApiId: apiData.id }, function(err, resourcesData) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(resourcesData); // successful response
var baseResource = resourcesData.items[0];
var resourceParams = {
restApiId: apiData.id,
parentId: baseResource.id,
pathPart: "callFunction"
};
apigateway.createResource(resourceParams, function(err, resourceData) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(resourceData); // successful response
var methodParams = {
authorizationType: 'AWS_IAM', // required
httpMethod: 'POST', // required
resourceId: resourceData.id, // required
restApiId: apiData.id, // required
apiKeyRequired: false
};
apigateway.putMethod(methodParams, function(err, methodData) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(methodData); // successful response
apigateway.putMethodResponse({
httpMethod: 'POST', // required
resourceId: resourceData.id, // required
restApiId: apiData.id, // required
"responseModels": {},
"statusCode": "200"
}, function(err, methodResponseData) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(methodResponseData); // successful response
var integrationParams = {
httpMethod: 'POST', // required
type: "AWS",
uri: "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:824411538776:function:callFunction/invocations",
resourceId: resourceData.id, // required
restApiId: apiData.id, // required
integrationHttpMethod: 'POST',
credentials: 'arn:aws:iam::824411538776:role/apiGatewayLambdaInvokeRole'
};
apigateway.putIntegration(integrationParams, function(err, integrationData) {
if (err) {
console.log(err, err.stack); // an error occurred
apigateway.deleteRestApi({ restApiId: apiData.id }, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
else {
console.log(integrationData); // successful response
apigateway.putIntegrationResponse({
httpMethod: 'POST', // required
resourceId: resourceData.id, // required
restApiId: apiData.id, // required
"selectionPattern": ".*",
"statusCode": "200"
}, function(err, integrationResponseData) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(integrationResponseData); // successful response
});
}
});
}
});
}
});
// ,
// "methodIntegration": {
// "integrationResponses": {
// "200": {
// "selectionPattern": ".*",
// "statusCode": "200"
// }
// },
// "cacheKeyParameters": [],
// "uri": "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:824411538776:function:callFunction/invocations",
// "httpMethod": "POST",
// "type": "AWS"
// }
}
});
}
});
}
});
};