This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom resource to update search mappings (#474)
- Loading branch information
Showing
6 changed files
with
163 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
import { getSearchMappings, SearchMappingsManager } from 'fhir-works-on-aws-search-es'; | ||
import axios from 'axios'; | ||
import { fhirVersion } from '../src/config'; | ||
|
||
const sendCfnResponse = async (event: any, status: 'SUCCESS' | 'FAILED', error?: Error) => { | ||
if (error !== undefined) { | ||
console.log(error); | ||
} | ||
const responseBody = JSON.stringify({ | ||
Status: status, | ||
Reason: error?.message, | ||
// The value of PhysicalResourceId doesn't really matter in this case. | ||
// It just needs to be the same string on all responses to indicate that it is the same resource. | ||
PhysicalResourceId: 'searchMappings', | ||
StackId: event.StackId, | ||
RequestId: event.RequestId, | ||
LogicalResourceId: event.LogicalResourceId, | ||
}); | ||
console.log(`Sending response to CFN: ${responseBody}`); | ||
await axios.put(event.ResponseURL, responseBody); | ||
}; | ||
|
||
/** | ||
* Custom resource lambda handler that creates or updates the search mappings. | ||
* Custom resource spec: See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html | ||
* @param event Custom resource request event. See https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-requests.html | ||
*/ | ||
exports.handler = async (event: any) => { | ||
console.log(event); | ||
try { | ||
if (process.env.ELASTICSEARCH_DOMAIN_ENDPOINT === undefined) { | ||
throw new Error('Missing required env variable ELASTICSEARCH_DOMAIN_ENDPOINT'); | ||
} | ||
|
||
if (process.env.NUMBER_OF_SHARDS === undefined) { | ||
throw new Error('Missing required env variable NUMBER_OF_SHARDS'); | ||
} | ||
|
||
const numberOfShards = Number.parseInt(process.env.NUMBER_OF_SHARDS, 10); | ||
if (Number.isNaN(numberOfShards)) { | ||
throw new Error('NUMBER_OF_SHARDS env variable is not a number'); | ||
} | ||
|
||
const searchMappingsManager = new SearchMappingsManager({ | ||
numberOfShards, | ||
searchMappings: getSearchMappings(fhirVersion), | ||
ignoreMappingsErrorsForExistingIndices: true, | ||
}); | ||
|
||
switch (event.RequestType as any) { | ||
case 'Create': | ||
case 'Update': | ||
await searchMappingsManager.createOrUpdateMappings(); | ||
await sendCfnResponse(event, 'SUCCESS'); | ||
break; | ||
case 'Delete': | ||
console.log('Received Delete event. Doing nothing'); | ||
await sendCfnResponse(event, 'SUCCESS'); | ||
break; | ||
default: | ||
// This should never happen | ||
await sendCfnResponse( | ||
event, | ||
'FAILED', | ||
new Error(`Unknown event.RequestType value: ${event.RequestType}`), | ||
); | ||
break; | ||
} | ||
} catch (e) { | ||
await sendCfnResponse(event, 'FAILED', e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3075,21 +3075,6 @@ aws-sdk@^2.859.0: | |
uuid "3.3.2" | ||
xml2js "0.4.19" | ||
|
||
aws-sdk@^2.965.0: | ||
version "2.970.0" | ||
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.970.0.tgz#dc258b61b4727dcb5130c494376b598eb19f827b" | ||
integrity sha512-9+ktvE5xgpHr3RsFOcq1SrhXLvU+jUji44jbecFZb5C2lzoEEB29aeN39OLJMW0ZuOrR+3TNum8c3f8YVx6A7w== | ||
dependencies: | ||
buffer "4.9.2" | ||
events "1.1.1" | ||
ieee754 "1.1.13" | ||
jmespath "0.15.0" | ||
querystring "0.2.0" | ||
sax "1.2.1" | ||
url "0.10.3" | ||
uuid "3.3.2" | ||
xml2js "0.4.19" | ||
|
||
aws-sign2@~0.7.0: | ||
version "0.7.0" | ||
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.7.0.tgz#b46e890934a9591f2d2f6f86d7e6a9f1b3fe76a8" | ||
|
@@ -5896,16 +5881,17 @@ [email protected]: | |
serverless-http "^2.3.1" | ||
uuid "^3.4.0" | ||
|
||
fhir-works-on-aws-search-es@3.5.2: | ||
version "3.5.2" | ||
resolved "https://registry.yarnpkg.com/fhir-works-on-aws-search-es/-/fhir-works-on-aws-search-es-3.5.2.tgz#0005625198b64daf905371bc9c1c0c68430153ac" | ||
integrity sha512-p94quGU8HbrVaVsft11zG73MSsgQrjmaTE7l99lqaaQouo7Ca0PQd9UMt7mUz+OQR1BQLZm0Fngqac+vHXnwfQ== | ||
fhir-works-on-aws-search-es@3.6.0: | ||
version "3.6.0" | ||
resolved "https://registry.yarnpkg.com/fhir-works-on-aws-search-es/-/fhir-works-on-aws-search-es-3.6.0.tgz#4e37806f4ff19e82edf7112c470520d17f126c12" | ||
integrity sha512-mMeT2dfcYMVsjILTggDGVX+roBlzZJ+aUHUATY2HomsQlViXSueF/ZJJZPQjqhU6I1YkVr7nRZIiPkq2C7kJug== | ||
dependencies: | ||
"@elastic/elasticsearch" "7.13" | ||
aws-elasticsearch-connector "^8.2.0" | ||
aws-sdk "^2.965.0" | ||
aws-sdk "^2.1000.0" | ||
date-fns "^2.19.0" | ||
fhir-works-on-aws-interface "^10.0.0" | ||
fhir-works-on-aws-interface "^11.1.0" | ||
flat "^5.0.2" | ||
lodash "^4.17.20" | ||
nearley "^2.20.0" | ||
|
||
|