From fb8f7844df1b4f5e9f14ea4a1822f43a54fd6e6c Mon Sep 17 00:00:00 2001 From: mrickard Date: Tue, 16 Nov 2021 17:10:54 -0500 Subject: [PATCH 1/3] fix: Skip creating integration when MS or polling integrations exist Signed-off-by: mrickard --- package.json | 2 +- src/integration.ts | 58 +++++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index f251000d..98b4ed47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-newrelic-lambda-layers", - "version": "2.2.0", + "version": "2.2.1", "description": "Serverless plugin for NewRelic APM AWS Lambda layers.", "main": "dist/index.js", "files": [ diff --git a/src/integration.ts b/src/integration.ts index 945c3bdb..88432b87 100644 --- a/src/integration.ts +++ b/src/integration.ts @@ -66,7 +66,7 @@ export default class Integration { "No New Relic AWS Lambda integration found for this New Relic linked account and aws account." ); - if (enableIntegration) { + if (enableIntegration && enableIntegration !== "false") { this.enable(externalId); return; } @@ -251,6 +251,18 @@ export default class Integration { } } + private async requestRoleArn(RoleName: string) { + const params = { + RoleName + }; + + const { + Role: { Arn } + } = await this.awsProvider.request("IAM", "getRole", params); + + return Arn; + } + private async checkAwsIntegrationRole(externalId: string) { const { accountId } = this.config; if (!accountId) { @@ -261,31 +273,29 @@ export default class Integration { } try { - const params = { - RoleName: `NewRelicLambdaIntegrationRole_${accountId}` - }; - - const { - Role: { Arn } - } = await this.awsProvider.request("IAM", "getRole", params); - - return Arn; + return this.requestRoleArn(`NewRelicLambdaIntegrationRole_${accountId}`); } catch (err) { - this.serverless.cli.log( - "The required NewRelicLambdaIntegrationRole cannot be found; Creating Stack with NewRelicLambdaIntegrationRole." - ); - const stackId = await this.createCFStack(accountId); - waitForStatus( - { - awsMethod: "describeStacks", - callbackMethod: () => this.enable(externalId), - methodParams: { - StackName: stackId + // We're limited to one rolename and a 64-character regex, so allowing for streams means a second query + try { + return this.requestRoleArn(`NewRelicInfrastructure-Integrations`); + } catch (fallbackErr) { + this.serverless.cli.log( + `Neither NewRelicLambdaIntegrationRole_${accountId} nor NewRelicInfrastructure-Integrations can be found. + Creating Stack with NewRelicLambdaIntegrationRole.` + ); + const stackId = await this.createCFStack(accountId); + waitForStatus( + { + awsMethod: "describeStacks", + callbackMethod: () => this.enable(externalId), + methodParams: { + StackName: stackId + }, + statusPath: "Stacks[0].StackStatus" }, - statusPath: "Stacks[0].StackStatus" - }, - this - ); + this + ); + } } } From 3558f658b5c1db69a718d1e2076e68dbe01a7669 Mon Sep 17 00:00:00 2001 From: mrickard Date: Fri, 19 Nov 2021 18:31:43 -0500 Subject: [PATCH 2/3] fix: Removing linkedAccount filter on account name, which disregarded streams, and adding staging env Signed-off-by: mrickard --- src/api.ts | 2 ++ src/integration.ts | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/api.ts b/src/api.ts index 3becf337..b7bff08f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -11,6 +11,8 @@ export const nerdgraphFetch = async ( const gqlUrl = region === "eu" ? "https://api.eu.newrelic.com/graphql" + : region === "staging" + ? "https://staging-api.newrelic.com/graphql" : "https://api.newrelic.com/graphql"; const agent = diff --git a/src/integration.ts b/src/integration.ts index 88432b87..2df8f697 100644 --- a/src/integration.ts +++ b/src/integration.ts @@ -30,9 +30,6 @@ export default class Integration { nrRegion, proxy } = this.config; - const { - linkedAccount = `New Relic Lambda Integration - ${accountId}` - } = this.config; const integrationData = await nerdgraphFetch( apiKey, @@ -55,7 +52,6 @@ export default class Integration { const match = linkedAccounts.filter(account => { return ( - account.name === linkedAccount && account.externalId === externalId && account.nrAccountId === parseInt(accountId, 10) ); @@ -255,10 +251,11 @@ export default class Integration { const params = { RoleName }; + const response = await this.awsProvider.request("IAM", "getRole", params); const { Role: { Arn } - } = await this.awsProvider.request("IAM", "getRole", params); + } = response; return Arn; } @@ -277,7 +274,9 @@ export default class Integration { } catch (err) { // We're limited to one rolename and a 64-character regex, so allowing for streams means a second query try { - return this.requestRoleArn(`NewRelicInfrastructure-Integrations`); + return this.requestRoleArn( + `NewRelicInfrastructure-Integrations|NewRelicStagingIntegration` + ); } catch (fallbackErr) { this.serverless.cli.log( `Neither NewRelicLambdaIntegrationRole_${accountId} nor NewRelicInfrastructure-Integrations can be found. From 173732b0265cec05b88ec4c9bceb938679f369b6 Mon Sep 17 00:00:00 2001 From: mrickard Date: Mon, 29 Nov 2021 17:10:07 -0500 Subject: [PATCH 3/3] fix: Removing regex chars from rolename check, which caused errors Signed-off-by: mrickard --- src/integration.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/integration.ts b/src/integration.ts index 2df8f697..4690d63c 100644 --- a/src/integration.ts +++ b/src/integration.ts @@ -274,9 +274,7 @@ export default class Integration { } catch (err) { // We're limited to one rolename and a 64-character regex, so allowing for streams means a second query try { - return this.requestRoleArn( - `NewRelicInfrastructure-Integrations|NewRelicStagingIntegration` - ); + return this.requestRoleArn(`NewRelicInfrastructure-Integrations`); } catch (fallbackErr) { this.serverless.cli.log( `Neither NewRelicLambdaIntegrationRole_${accountId} nor NewRelicInfrastructure-Integrations can be found.