0
0

More than 3 years have passed since last update.

Amazon ElasticSearchのロギングを有効にする - Cloudformation

Last updated at Posted at 2021-03-15

open_disto-elasticsearch-logo-800x400.jpg
cloudformationを使用していて、ElasticSearchのログ設定に問題がある場合は、このブログが役に立ちます。

ElasticSearch cloudformation

以下は、ElasticSearchを作成するためのcloudformationテンプレートのサンプルです。ネストしたスタックの一部であるため、パラメータの入力検証などが欠落していますが、このブログの趣旨としては問題ありません。

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Elastic Search",
  "Parameters": {
    "Environment": {
      "Type": "String"
    },
    "ProjectName": {
      "Type": "String"
    },
    "CFVPCSubnetES1": {
      "Type": "String"
    },
    "CFVPCSubnetES2": {
      "Type": "String"
    },
    "CFVPCSGES": {
      "Type": "String"
    },
    "ESUserpassword": {
      "NoEcho": true,
      "Type": "String"
    },
    "ESStorage": {
      "Type": "String"
    }
  },
  "Conditions": {
    "ProdCondition": {
      "Fn::Equals": [
        {
          "Ref": "Environment"
        },
        "prod"
      ]
    }
  },
  "Resources": {
    "CFES": {
      "Type": "AWS::Elasticsearch::Domain",
      "Properties": {
        "AccessPolicies": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "AWS": "*"
              },
              "Action": "es:*",
              "Resource": {
                "Fn::Sub": "arn:aws:es:ap-northeast-1:337609275973:domain/${ProjectName}-${Environment}-es/*"
              }
            }
          ]
        },
        "AdvancedSecurityOptions": {
          "Enabled": true,
          "InternalUserDatabaseEnabled": true,
          "MasterUserOptions": {
            "MasterUserName": {
              "Fn::Sub": "${ProjectName}-${Environment}esadmin"
            },
            "MasterUserPassword": {
              "Ref": "ESUserpassword"
            }
          }
        },
        "DomainEndpointOptions": {
          "EnforceHTTPS": "true",
          "TLSSecurityPolicy": "Policy-Min-TLS-1-2-2019-07"
        },
        "DomainName": {
          "Fn::Sub": "${ProjectName}-${Environment}-es"
        },
        "EBSOptions": {
          "EBSEnabled": "true",
          "VolumeSize": {
            "Ref": "ESStorage"
          },
          "VolumeType": "gp2"
        },
        "ElasticsearchClusterConfig": {
          "Fn::If": [
            "ProdCondition",
            {
              "DedicatedMasterCount": 3,
              "DedicatedMasterEnabled": "true",
              "DedicatedMasterType": "t3.medium.elasticsearch",
              "InstanceCount": "2",
              "InstanceType": "t3.medium.elasticsearch",
              "ZoneAwarenessConfig": {
                "AvailabilityZoneCount": "2"
              },
              "ZoneAwarenessEnabled": "true"
            },
            {
              "DedicatedMasterEnabled": "false",
              "InstanceCount": "1",
              "InstanceType": "t3.small.elasticsearch",
              "ZoneAwarenessEnabled": "false"
            }
          ]
        },
        "ElasticsearchVersion": "7.8",
        "SnapshotOptions": {
          "AutomatedSnapshotStartHour": "15"
        },
        "EncryptionAtRestOptions": {
          "Enabled": true
        },
        "LogPublishingOptions": {
          "ES_APPLICATION_LOGS": {
            "CloudWatchLogsLogGroupArn": {
              "Fn::GetAtt": [
                "CFCWLogGroupApp",
                "Arn"
              ]
            },
            "Enabled": true
          },
          "AUDIT_LOGS": {
            "CloudWatchLogsLogGroupArn": {
              "Fn::GetAtt": [
                "CFCWLogGroupAudit",
                "Arn"
              ]
            },
            "Enabled": true
          }
        },
        "NodeToNodeEncryptionOptions": {
          "Enabled": true
        },
        "VPCOptions": {
          "SecurityGroupIds": [
            {
              "Ref": "CFVPCSGES"
            }
          ],
          "SubnetIds": {
            "Fn::If": [
              "ProdCondition",
              [
                {
                  "Ref": "CFVPCSubnetES1"
                },
                {
                  "Ref": "CFVPCSubnetES2"
                }
              ],
              [
                {
                  "Ref": "CFVPCSubnetES1"
                }
              ]
            ]
          }
        }
      }
    },
    "CFCWLogGroupAudit": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": {
          "Fn::Sub": "/aws/aes/domains/${ProjectName}-${Environment}-es/audit-logs"
        }
      }
    },
    "CFCWLogGroupApp": {
      "Type": "AWS::Logs::LogGroup",
      "Properties": {
        "LogGroupName": {
          "Fn::Sub": "/aws/aes/domains/${ProjectName}-${Environment}-es/application-logs"
        }
      }
    }
  },
  "Outputs": {
    "CFES": {
      "Value": {
        "Ref": "CFES"
      }
    },
    "CFESEndpoint": {
      "Value": {
        "Fn::GetAtt": [
          "CFES",
          "DomainEndpoint"
        ]
      }
    }
  }
}

AWSのアカウントでリージョンを選択し、初めて上記のcloudformationを実行すると、次のようなエラーが発生します。

The Resource Access Policy specified for the CloudWatch Logs log group /es/es-domain/AppLogs does not grant sufficient permissions for Amazon Elasticsearch Service to create a log stream. Please check the Resource Access Policy. (Service: AWSElasticsearch; Status Code: 400; Error Code: ValidationException; Request ID: 915d5df2-6b0c-432b-ad6f-0257ab32a85f; Proxy: null)

解決策

上記の問題を解決するには、AWS CLIを使用して以下のコマンドを実行すると、次回の実行時にelastic search logging用のcloudformationテンプレートが動作するようになります。

 aws --region ap-northeast-1 logs put-resource-policy --policy-name elasticsearch-logging --policy-document '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "es.amazonaws.com" ] }, "Action": [ "logs:CreateLogStream", "logs:PutLogEvents", "logs:PutLogEventsBatch" ], "Resource": "arn:aws:logs:*:*:log-group:/aws/aes/domains/*:*" } ] }'

これにより、指定したリージョンにcloudwatchのログ用のリソースポリシーが作成され、elastic searchが適切なロググループにログを書き込みます。

{
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Principal": {
            "Service": ["es.amazonaws.com"]
        },
        "Action": ["logs:CreateLogStream", "logs:PutLogEvents", "logs:PutLogEventsBatch"],
        "Resource": "arn:aws:logs:*:*:log-group:/aws/aes/domains/*:*"
    }]
}
0
0
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
0
0