資料がなかったので、備忘録代わりに。
client-cloudsearch-domain、ローカルなので認証のためにcredentialが必要。endpointだけじゃなくて、regionも必要でした。cloudに持っていくと、認証は別方式のほうが良いと思われるので、消してください。
参考(AWS公式、node用のサンプルがあります)
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudsearch-domain/index.html
docker-compose.yml
Dockerfile
package.json
app.js (実行ファイルなので、任意にindex.jsなどに)
docker-compose.yml
version: '3'
services:
cloudsearch-api:
image: cloudsearch-api
build: .
ports:
- 9199:8080
tty: true
volumes:
- "./data/:/var/task/data"
working_dir: /var/task/
Dockerfile
FROM public.ecr.aws/lambda/nodejs:16
COPY app.js package*.json ./
RUN npm install
CMD [ "app.lambdaHandler" ]
package.json
app.jsは、実行ファイルなので、任意にindex.jsなどに。lambdaHandlerは実行関数
FROM public.ecr.aws/lambda/nodejs:16
COPY app.js package*.json ./
RUN npm install
CMD [ "app.lambdaHandler" ]
app.js
AWS公式のサンプルから、ES6+形式
import { CloudSearchDomainClient, SearchCommand } from "@aws-sdk/client-cloudsearch-domain";
const client = new CloudSearchDomainClient({
region: 'ap-northeast-1'
, endpoint: 'https://search-xxxxxxxxxxxxxxxxx.ap-northeast-1.cloudsearch.amazonaws.com'
, credentials: { accessKeyId: 'AKXXXXXXXXXXXXZWH', secretAccessKey: '6o6FDDDDDDDDDDDDDDDDDDDDDDDO' },
});
export const lambdaHandler = async (event, context) => {
var params = {
query: event.q
};
const command = new SearchCommand(params);
// async/await.
try {
var data = await client.send(command);
// process data.
} catch (error) {
console.log(error);
// error handling.
} finally {
// finally.
}
return {
statusCode: 200,
body: data,
};;
};
実行 dockerを立ち上げて
docker-compose up --build
別プロセスで curlで叩く
curl -XPOST "http://localhost:9199/2015-03-31/functions/function/invocations" -d '{
"q": "ウクライナ"
}'
{
"statusCode": 200,
"body": {
"$metadata": {
"httpStatusCode": 200,
"attempts": 1,
"totalRetryDelay": 0
},
"hits": {
"found": 11799,
"hit": [
{
"fields": {
"title": [
"ウクライナ避難民特例支援"
],
"sub_title": [
"抱き合親子"
],
"body": [
" ウクライナから日本に、大阪空港"
],
"type": [
"1"
],
"post_date": [
"2022-04-05T11:44:02Z"
],
"category": [
"10",
"6",
"105",
"1"
],
"has_eyecatch": [
"1"
],
"place": [
"48"
],
"content": [
"0"
],
"author": [
"0"
]
},
"id": "7621245"
},
],
"start": 0
},
"status": {
"rid": "fasfdsafawer=",
"timems": 2
}
}
}