-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
93 lines (76 loc) · 2.69 KB
/
index.js
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
const {
createApi,
createIntegration,
createDefaultAutodeployStage,
setRoute,
allowApiGatewayToInvokeLambda,
getApiDetails,
} = require('./utils')
// TODO handle regional / edge / read up on how edge works
const HFAPINAME = 'hyperform-v1'
/**
* @description Creates a public HTTP endpoint that forwards request to a given Lambda.
* @param {string} lambdaArn
* @param {string} region
* @returns {Promise<string>} Full endpoint URL, eg. https://48ytz1e6f3.execute-api.us-east-2.amazonaws.com/endpoint-hello
*/
async function publishAmazon(lambdaArn, region) {
// console.log('received lambdaar', lambdaArn)
const lambdaName = lambdaArn.split(':').slice(-1)[0]
// Lambda 'endpoint-hello' should be at 'https://.../endpoint-hello'
const routePath = `/${lambdaName}`
/// ///////////////////////////////////////////////
/// //ensure HF API exists in that region /////////
// TODO to edge
/// //////////////////////////////////////////////
let hfApiId
let hfApiUrl
/// ///////////////////////////////////////////////
/// Check if HF umbrella API exists in that region
/// //////////////////////////////////////////////
const apiDetails = await getApiDetails(HFAPINAME, region)
// exists
// use it
if (apiDetails != null && apiDetails.apiId != null) {
hfApiId = apiDetails.apiId
hfApiUrl = apiDetails.apiUrl
// does not exist
// create HF API
} else {
const createRes = await createApi(HFAPINAME, region)
hfApiId = createRes.apiId
hfApiUrl = createRes.apiUrl
}
/// ///////////////////////////////////////////////
/// Add permission to API to lambda accessed by API gateway
/// //////////////////////////////////////////////
// todo iwann spezifisch der api access der lambda erlauben via SourceArn
await allowApiGatewayToInvokeLambda(lambdaName, region)
/// ///////////////////////////////////////////////
/// Create integration that represents the Lambda
/// //////////////////////////////////////////////
const integrationId = await createIntegration(hfApiId, region, lambdaArn)
/// ///////////////////////////////////////////////
/// Create $default Auto-Deploy stage
/// //////////////////////////////////////////////
try {
await createDefaultAutodeployStage(hfApiId, region)
} catch (e) {
// already exists (shouldn't throw because of anything other)
// nice
}
/// ///////////////////////////////////////////////
/// Create / update route with that integration
/// //////////////////////////////////////////////
await setRoute(
hfApiId,
region,
routePath,
integrationId,
)
const endpointUrl = hfApiUrl + routePath
return endpointUrl
}
module.exports = {
publishAmazon,
}