Skip to content

Commit de435cd

Browse files
authored
fix: add separate OS IAM service role check (#3854)
1 parent 4ae34a7 commit de435cd

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

packages/serverless-cms-aws/src/core/plugins/checkEsServiceRole.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ export const checkEsServiceRole = {
1010
name: "hook-before-deploy-es-service-role",
1111
async hook(params: Record<string, any>, context: CliContext) {
1212
const spinner = ora();
13-
spinner.start(`Checking Elastic Search service role...`);
13+
spinner.start(`Checking Amazon Elasticsearch service role...`);
1414
const iam = new IAM();
1515
try {
1616
await iam.getRole({ RoleName: "AWSServiceRoleForAmazonElasticsearchService" });
1717

1818
spinner.stopAndPersist({
1919
symbol: green("✔"),
20-
text: `Found Elastic Search service role!`
20+
text: `Found Amazon Elasticsearch service role!`
2121
});
22-
context.success(`Found Elastic Search service role!`);
22+
context.success(`Found Amazon Elasticsearch service role!`);
2323
} catch (err) {
2424
// We've seen cases where the `iam.getRole` call fails because of an issue
2525
// other than not being able to retrieve the service role. Let's print
2626
// additional info if that's the case. Will make debugging a bit easier.
2727
if (err.code !== NO_SUCH_ENTITY_IAM_ERROR) {
2828
spinner.fail(
29-
"Tried retrieving Elastic Search service role but failed with the following error: " +
29+
"Tried retrieving Amazon Elasticsearch service role but failed with the following error: " +
3030
err.message
3131
);
3232
context.debug(err);
3333
process.exit(1);
3434
}
3535

36-
spinner.text = "Creating Elastic Search service role...";
36+
spinner.text = "Creating Amazon Elasticsearch service role...";
3737

3838
try {
3939
await iam.createServiceLinkedRole({ AWSServiceName: "es.amazonaws.com" });
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { IAM } from "@webiny/aws-sdk/client-iam";
2+
import ora from "ora";
3+
import { green } from "chalk";
4+
import { CliContext } from "@webiny/cli/types";
5+
6+
const NO_SUCH_ENTITY_IAM_ERROR = "NoSuchEntity";
7+
8+
export const checkOsServiceRole = {
9+
type: "hook-before-deploy",
10+
name: "hook-before-deploy-es-service-role",
11+
async hook(params: Record<string, any>, context: CliContext) {
12+
const spinner = ora();
13+
spinner.start(`Checking Amazon OpenSearch service role...`);
14+
const iam = new IAM();
15+
try {
16+
await iam.getRole({ RoleName: "AWSServiceRoleForAmazonOpenSearchService" });
17+
18+
spinner.stopAndPersist({
19+
symbol: green("✔"),
20+
text: `Found Amazon OpenSearch service role!`
21+
});
22+
context.success(`Found Amazon OpenSearch service role!`);
23+
} catch (err) {
24+
// We've seen cases where the `iam.getRole` call fails because of an issue
25+
// other than not being able to retrieve the service role. Let's print
26+
// additional info if that's the case. Will make debugging a bit easier.
27+
if (err.code !== NO_SUCH_ENTITY_IAM_ERROR) {
28+
spinner.fail(
29+
"Tried retrieving Amazon OpenSearch service role but failed with the following error: " +
30+
err.message
31+
);
32+
context.debug(err);
33+
process.exit(1);
34+
}
35+
36+
spinner.text = "Creating Amazon OpenSearch service role...";
37+
38+
try {
39+
await iam.createServiceLinkedRole({ AWSServiceName: "es.amazonaws.com" });
40+
41+
spinner.stop();
42+
} catch (err) {
43+
spinner.fail(err.message);
44+
context.debug(err);
45+
process.exit(1);
46+
}
47+
}
48+
}
49+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./generateDdbToEsHandler";
22
export * from "./checkEsServiceRole";
3+
export * from "./checkOsServiceRole";

packages/serverless-cms-aws/src/createCoreApp.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createCorePulumiApp, CreateCorePulumiAppParams } from "@webiny/pulumi-aws";
22
import { PluginCollection } from "@webiny/plugins/types";
3-
import { generateDdbToEsHandler, checkEsServiceRole } from "./core/plugins";
3+
import { generateDdbToEsHandler, checkEsServiceRole, checkOsServiceRole } from "./core/plugins";
44

55
export { CoreOutput, configureAdminCognitoFederation } from "@webiny/pulumi-aws";
66

@@ -11,7 +11,12 @@ export interface CreateCoreAppParams extends CreateCorePulumiAppParams {
1111
export function createCoreApp(projectAppParams: CreateCoreAppParams = {}) {
1212
const builtInPlugins = [];
1313
if (projectAppParams.elasticSearch || projectAppParams.openSearch) {
14-
builtInPlugins.push(generateDdbToEsHandler, checkEsServiceRole);
14+
builtInPlugins.push(generateDdbToEsHandler);
15+
if (projectAppParams.elasticSearch) {
16+
builtInPlugins.push(checkEsServiceRole);
17+
} else {
18+
builtInPlugins.push(checkOsServiceRole);
19+
}
1520
}
1621

1722
const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];

packages/serverless-cms-aws/src/enterprise/createCoreApp.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createCorePulumiApp, CreateCorePulumiAppParams } from "@webiny/pulumi-aws/enterprise";
22
import { PluginCollection } from "@webiny/plugins/types";
3-
import { generateDdbToEsHandler, checkEsServiceRole } from "~/core/plugins";
3+
import { generateDdbToEsHandler, checkEsServiceRole, checkOsServiceRole } from "~/core/plugins";
44

5-
export { CoreOutput } from "@webiny/pulumi-aws";
5+
export { CoreOutput, configureAdminCognitoFederation } from "@webiny/pulumi-aws";
66

77
export interface CreateCoreAppParams extends CreateCorePulumiAppParams {
88
plugins?: PluginCollection;
@@ -11,7 +11,12 @@ export interface CreateCoreAppParams extends CreateCorePulumiAppParams {
1111
export function createCoreApp(projectAppParams: CreateCoreAppParams = {}) {
1212
const builtInPlugins = [];
1313
if (projectAppParams.elasticSearch || projectAppParams.openSearch) {
14-
builtInPlugins.push(generateDdbToEsHandler, checkEsServiceRole);
14+
builtInPlugins.push(generateDdbToEsHandler);
15+
if (projectAppParams.elasticSearch) {
16+
builtInPlugins.push(checkEsServiceRole);
17+
} else {
18+
builtInPlugins.push(checkOsServiceRole);
19+
}
1520
}
1621

1722
const customPlugins = projectAppParams.plugins ? [...projectAppParams.plugins] : [];

0 commit comments

Comments
 (0)