0
Hi team, We are trying to use AWS v3 SDK apis, and have create iam user with providing him all the roles. Now we have created cron job to connect with OpenSearch as below:
const { OpenSearchClient } = require("@aws-sdk/client-opensearch");
const { awsAuthMiddleware } = require("@aws-sdk/middleware-signing");
const { fromStatic } = require("@aws-sdk/credential-provider-static");
const { AwsCredentialProvider } = require("@aws-sdk/types");
// Replace 'your-access-key-id' and 'your-secret-access-key' with your actual AWS access key and secret key
const accessKeyId = 'your-access-key-id';
const secretAccessKey = 'your-secret-access-key';
// Replace 'your-region' and 'your-open-search-endpoint' with your actual AWS region and OpenSearch endpoint
const region = 'your-region';
const openSearchEndpoint = 'https://your-open-search-endpoint';
// Create an instance of the OpenSearch client
const client = new OpenSearchClient({
region: region,
endpoint: openSearchEndpoint, // Specify the OpenSearch endpoint
});
// Create a credentials provider with your AWS access key and secret key
const credentialsProvider = fromStatic({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey
});
// Attach the AWS Signature Version 4 signing middleware to the client
client.middlewareStack.add(
awsAuthMiddleware({
credentials: credentialsProvider,
signerService: 'es', // Specify the service name for Amazon OpenSearch Service
region: region, // Replace with your AWS region
})
);
// Now you can use the client to make authenticated requests to OpenSearch
// Function to create an index
async function createIndex(indexName) {
const command = new CreateIndexCommand({ IndexName: indexName });
try {
const response = await client.send(command);
console.log(`Index "${indexName}" created successfully.`);
return response;
} catch (error) {
console.error("Error creating index:", error);
throw error;
}
}
// Function to insert a single document
async function insertDocument(indexName, document) {
const command = new PutCommand({
IndexName: indexName,
Body: document
});
try {
const response = await client.send(command);
console.log("Document inserted successfully:", response);
return response;
} catch (error) {
console.error("Error inserting document:", error);
throw error;
}
}
// Function to perform bulk insertion
async function bulkInsert(indexName, documents) {
const commands = documents.map(document => ({
create: { _index: indexName },
}));
const bulkCommandInput = new BulkCommandInput({
body: commands
});
const command = new BulkCommand(bulkCommandInput);
try {
const response = await client.send(command);
console.log("Bulk insertion successful:", response);
return response;
} catch (error) {
console.error("Error performing bulk insertion:", error);
throw error;
}
}
Unfortunately we are getting errors:
security_exception: [security_exception] Reason: no permissions for [indices:admin/create] and User [name=arn:aws:iam::939064200607:user/strapi-user, backend_roles=[], requestedTenant=null]
Please suggest