LoginSignup
6
1

More than 1 year has passed since last update.

【New Relic】AWS CDK と CDK for Terraform で AWS インテグレーションを作成する

Last updated at Posted at 2022-12-18

概要

本稿では AWS CDK と CDK for Terraform (CDKTF) を使って AWS インテグレーションを作成する方法をご紹介します。

想定読者

  • 既に AWS CDK を運用している
  • 新たに New Relic で AWS インテグレーションを作成したい
  • CDK の開発体験を他クラウドプロバイダに拡張したい

やること

Terraform を使用した AWS インテグレーションの公式サンプルがありますので、これを参考に AWS サイドは AWS CDK、New Relic サイドは CDKTF を使ってインテグレーションに必要なリソースを作成します。

ソースコード

注意事項
AWS と New Relic の間で API Key や ARN などの値の受け渡しが発生します。本稿では Systems Manager Parameter Store を利用してコード上に秘匿情報が出現しないようにしていますが、tfstate や CloudFormation パラメータには平文で格納されるため取り扱いには十分ご注意ください。

手順

大まかな流れは次の通りです。

  • New Relic から User API Key を取得
  • New Relic で AWS に提供する LICENSE Key を生成
  • AWS で IAM Role や Firehose Derivery Stream を構成
  • New Relic で AWS Link Account を作成

以下では、New Relic での操作と AWS での操作を区別するため色分けしています
🟢 New Relic
🟠 AWS

🟢 User API Key を取得する

New Relic provider を設定するために User API Key を取得します。
取得手順はドキュメントを参考にしてください。

こちらは New Relic Terraform Provider の設定に利用します。
通常は NRAK というプレフィックスの Key 値となります。

🟠 Parameter Store に格納する

本稿では AWS と New Relic の間の値の受け渡しに Parameter Store を利用します。
取得した User API Key を下記の設定で Parameter Store に格納します。

名前: /${env}/cdktf/newrelic/api_key_for_provider
種類: SecureString
データ型: text

🟢 AWS に提供する LICENSE Key を生成

ここから CDKTF で New Relic にリソースを作成していきます。
まずは AWS に提供する LICENSE Key を生成します。
同時に Remote Backend と Provider 関連の設定も行います。

main.ts(一部抜粋)
class NewrelicStack extends TerraformStack {
  constructor(scope: Construct, id: string, config: NewrelicStackConfig) {
    super(scope, id);

    // remote backendにS3を利用する場合
    new S3Backend(this, {
      bucket: `cdktf-remote-backend-${config.envName}`,
      key: `${config.envName}/newrelic`,
      region: "ap-northeast-1"
    })

    new AwsProvider(this, "AWS", {
      region: "ap-northeast-1",
    });

    // 手動作成したUser API KeyをSystems Manager Parameter Store経由で取得する
    const apiKeyForProvider = new DataAwsSsmParameter(this, 'ApiKeyForProviderParameterData', {
      name: `/${config.envName}/cdktf/newrelic/api_key_for_provider`,
    });

    new NewrelicProvider(this, "New Relic", {
      accountId: config.newrelicAccountId,
      apiKey: apiKeyForProvider.value,
      region: "US",
    });

    // AWS(Amazon Kinesis Data Firehose)に渡すLICENSE Keyを生成
    const apiKeyForFirehose = new ApiAccessKey(this, "ApiKeyForFirehose", {
      name: "Ingest License key",
      notes: "For AWS Cloud Integrations (Used in Firehose)",
      accountId: config.newrelicAccountId,
      keyType: "INGEST",
      ingestType: "LICENSE",
    });

    // 作成したLICENSE KeyをSystems Manager Parameter Storeに格納する
    new SsmParameter(this, "ApiKeyForFirehoseParameter", {
      name: `/${config.envName}/cdktf/newrelic/api_key_for_firehose`,
      type: 'String',
      value: apiKeyForFirehose.key
    });
  }
}

次のコマンドを実行して Deploy してください。

❯ cdktf deploy newrelic-sample

🟠 IAM Role や Firehose Derivery Stream を構成

AWS (AWS CDK) で IAM Role や Firehose Derivery Stream といったデータ連携に必要なリソースを作成します。
こちらのリソースを次のコマンドを実行して Deploy してください。

❯ cdk deploy NewrelicIntegrationsStack

これで AWS サイドの準備は完了です。

🟢 AWS Link Account を作成

最後に New Relic (CDKTF) で AWS Link Account を作成します。

main.ts(一部抜粋)
class NewrelicStack extends TerraformStack {
  constructor(scope: Construct, id: string, config: NewrelicStackConfig) {
    super(scope, id);

    // AWS CDKで作成したRoleのArnをSystems Manager Parameter Store経由で取得する
    const newrelicAwsRoleArnParameter = new DataAwsSsmParameter(this, 'NewrelicAwsRoleArnParameterData', {
      name: `/${config.envName}/newrelic_integrations/newrelic_aws_role/arn`,
    });

    new CloudAwsLinkAccount(this, "NewrelicCloudIntegrationPush", {
      name: "Sample Integration",
      metricCollectionMode: "PUSH",
      arn: newrelicAwsRoleArnParameter.value,
    })
  }
}

次のコマンドを実行して Deploy してください。

❯ cdktf deploy newrelic-sample

以上で作業完了です。

確認方法

one.newrelic.com > Infrastructure > AWS の順にコンソールを移動し、下記のような表示があれば問題なく連携ができています。

180 metrics from 19 namespaces received from AWS account xxxxxxxx with 0 errors.

参考

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1