|
| 1 | +# Existing Cluster with the AWS Observability accelerator base module and Java monitoring |
| 2 | + |
| 3 | + |
| 4 | +This example demonstrates how to use the AWS Observability Accelerator Terraform |
| 5 | +modules with Java monitoring enabled. |
| 6 | +The current example deploys the [AWS Distro for OpenTelemetry Operator](https://docs.aws.amazon.com/eks/latest/userguide/opentelemetry.html) for Amazon EKS with its requirements and make use of existing |
| 7 | +Amazon Managed Service for Prometheus and Amazon Managed Grafana workspaces. |
| 8 | + |
| 9 | +It is based on the `java module`, one of our [workloads modules](../../modules/workloads/) |
| 10 | +to provide an existing EKS cluster with an OpenTelemetry collector, |
| 11 | +curated Grafana dashboards, Prometheus alerting and recording rules with multiple |
| 12 | +configuration options on the cluster infrastructure. |
| 13 | + |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Ensure that you have the following tools installed locally: |
| 18 | + |
| 19 | +1. [aws cli v2](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) |
| 20 | +2. [kubectl](https://kubernetes.io/docs/tasks/tools/) |
| 21 | +3. [terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) |
| 22 | + |
| 23 | + |
| 24 | +## Setup |
| 25 | + |
| 26 | +This example uses a local terraform state. If you need states to be saved remotely, |
| 27 | +on Amazon S3 for example, visit the [terraform remote states](https://www.terraform.io/language/state/remote) documentation |
| 28 | + |
| 29 | +1. Clone the repo using the command below |
| 30 | + |
| 31 | +``` |
| 32 | +git clone https://github.com/aws-observability/terraform-aws-observability-accelerator.git |
| 33 | +``` |
| 34 | + |
| 35 | +2. Initialize terraform |
| 36 | + |
| 37 | +```console |
| 38 | +cd examples/existing-cluster-java |
| 39 | +terraform init |
| 40 | +``` |
| 41 | + |
| 42 | +3. AWS Region |
| 43 | + |
| 44 | +Specify the AWS Region where the resources will be deployed. Edit the `terraform.tfvars` file and modify `aws_region="..."`. You can also use environement variables `export TF_VAR_aws_region=xxx`. |
| 45 | + |
| 46 | +4. Amazon EKS Cluster |
| 47 | + |
| 48 | +To run this example, you need to provide your EKS cluster name. |
| 49 | +If you don't have a cluster ready, visit [this example](../eks-cluster-with-vpc) |
| 50 | +first to create a new one. |
| 51 | + |
| 52 | +Add your cluster name for `eks_cluster_id="..."` to the `terraform.tfvars` or use an environment variable `export TF_VAR_eks_cluster_id=xxx`. |
| 53 | + |
| 54 | +5. Amazon Managed Service for Prometheus workspace (optional) |
| 55 | + |
| 56 | +If you have an existing workspace, add `managed_prometheus_workspace_id=ws-xxx` |
| 57 | +or use an environment variable `export TF_VAR_managed_prometheus_workspace_id=ws-xxx`. |
| 58 | + |
| 59 | +If you don't specify anything a new workspace will be created for you. |
| 60 | + |
| 61 | +6. Amazon Managed Grafana workspace |
| 62 | + |
| 63 | +If you have an existing workspace, create an environment variable `export TF_VAR_managed_grafana_workspace_id=g-xxx`. |
| 64 | + |
| 65 | +7. <a name="apikey"></a> Grafana API Key |
| 66 | + |
| 67 | +Amazon Managed Service for Grafana provides a control plane API for generating Grafana API keys. We will provide to Terraform |
| 68 | +a short lived API key to run the `apply` or `destroy` command. |
| 69 | +Ensure you have necessary IAM permissions (`CreateWorkspaceApiKey, DeleteWorkspaceApiKey`) |
| 70 | + |
| 71 | +```sh |
| 72 | +export TF_VAR_grafana_api_key=`aws grafana create-workspace-api-key --key-name "observability-accelerator-$(date +%s)" --key-role ADMIN --seconds-to-live 1200 --workspace-id $TF_VAR_managed_grafana_workspace_id --query key --output text` |
| 73 | +``` |
| 74 | + |
| 75 | +## Deploy |
| 76 | + |
| 77 | +```sh |
| 78 | +terraform apply -var-file=terraform.tfvars |
| 79 | +``` |
| 80 | + |
| 81 | +or if you had only setup environment variables, run |
| 82 | + |
| 83 | +```sh |
| 84 | +terraform apply |
| 85 | +``` |
| 86 | + |
| 87 | +## Visualization |
| 88 | + |
| 89 | +1. Prometheus datasource on Grafana |
| 90 | + |
| 91 | +Open your Grafana workspace and under Configuration -> Data sources, you will see `aws-observability-accelerator`. Open and click `Save & test`. You will then see a notification confirming that the Amazon Managed Service for Prometheus workspace is ready to be used on Grafana. |
| 92 | + |
| 93 | +2. Grafana dashboards |
| 94 | + |
| 95 | +Go to the Dashboards panel of your Grafana workspace. There will be a folder called `Observability Accelerator Dashboards` |
| 96 | + |
| 97 | +<img width="832" alt="image" src="https://user-images.githubusercontent.com/97046295/194903648-57c55d30-6f90-4b03-9eb6-577aaba7dc22.png"> |
| 98 | + |
| 99 | +Open the "Java/JMX" dashboard to view its visualization |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +2. Amazon Managed Service for Prometheus rules and alerts |
| 106 | + |
| 107 | +Open the Amazon Managed Service for Prometheus console and view the details of your workspace. Under the `Rules management` tab, you will find new rules deployed. |
| 108 | + |
| 109 | +<img width="1314" alt="image" src="https://user-images.githubusercontent.com/97046295/194904104-09a28577-d149-478e-b0a1-dc21cb7effc1.png"> |
| 110 | + |
| 111 | + |
| 112 | +To setup your alert receiver, with Amazon SNS, follow [this documentation](https://docs.aws.amazon.com/prometheus/latest/userguide/AMP-alertmanager-receiver.html) |
| 113 | + |
| 114 | + |
| 115 | +## Deploy an Example Java Application |
| 116 | + |
| 117 | +In this section we will reuse an example from the AWS OpenTelemetry collector [repository](https://github.com/aws-observability/aws-otel-collector/blob/main/docs/developers/container-insights-eks-jmx.md). For convenience, the steps can be found below. |
| 118 | + |
| 119 | +1. Clone [this repository](https://github.com/aws-observability/aws-otel-test-framework) and navigate to the `sample-apps/jmx/` directory. |
| 120 | + |
| 121 | +2. Authenticate to Amazon ECR |
| 122 | + |
| 123 | +```sh |
| 124 | +export AWS_ACCOUNT_ID=`aws sts get-caller-identity --query Account --output text` |
| 125 | +export AWS_REGION={region} |
| 126 | +aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com |
| 127 | +``` |
| 128 | + |
| 129 | +3. Create an Amazon ECR repository |
| 130 | + |
| 131 | +```sh |
| 132 | +aws ecr create-repository --repository-name prometheus-sample-tomcat-jmx \ |
| 133 | + --image-scanning-configuration scanOnPush=true \ |
| 134 | + --region $AWS_REGION |
| 135 | +``` |
| 136 | + |
| 137 | +4. Build Docker image and push to ECR. |
| 138 | + |
| 139 | +```sh |
| 140 | +docker build -t $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/prometheus-sample-tomcat-jmx:latest . |
| 141 | +docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/prometheus-sample-tomcat-jmx:latest |
| 142 | +``` |
| 143 | + |
| 144 | +5. Install sample application |
| 145 | + |
| 146 | +```sh |
| 147 | +export SAMPLE_TRAFFIC_NAMESPACE=javajmx-sample |
| 148 | +curl https://raw.githubusercontent.com/aws-observability/aws-otel-test-framework/terraform/sample-apps/jmx/examples/prometheus-metrics-sample.yaml > metrics-sample.yaml |
| 149 | +sed -i "s/{{aws_account_id}}/$AWS_ACCOUNT_ID/g" metrics-sample.yaml |
| 150 | +sed -i "s/{{region}}/$AWS_REGION/g" metrics-sample.yaml |
| 151 | +sed -i "s/{{namespace}}/$SAMPLE_TRAFFIC_NAMESPACE/g" metrics-sample.yaml |
| 152 | +kubectl apply -f metrics-sample.yaml |
| 153 | +``` |
| 154 | + |
| 155 | +Verify that the sample application is running: |
| 156 | + |
| 157 | +```sh |
| 158 | +kubectl get pods -n $SAMPLE_TRAFFIC_NAMESPACE |
| 159 | + |
| 160 | +NAME READY STATUS RESTARTS AGE |
| 161 | +tomcat-bad-traffic-generator 1/1 Running 0 11s |
| 162 | +tomcat-example-7958666589-2q755 0/1 ContainerCreating 0 11s |
| 163 | +tomcat-traffic-generator 1/1 Running 0 11s |
| 164 | +``` |
| 165 | + |
| 166 | +## Advanced configuration |
| 167 | + |
| 168 | +1. Cross-region Amazon Managed Prometheus workspace |
| 169 | + |
| 170 | +If your existing Amazon Managed Prometheus workspace is in another AWS Region, |
| 171 | +add this `managed_prometheus_region=xxx` and `managed_prometheus_workspace_id=ws-xxx`. |
| 172 | + |
| 173 | +2. Cross-region Amazon Managed Grafana workspace |
| 174 | + |
| 175 | +If your existing Amazon Managed Prometheus workspace is in another AWS Region, |
| 176 | +add this `managed_prometheus_region=xxx` and `managed_prometheus_workspace_id=ws-xxx`. |
| 177 | + |
| 178 | +## Destroy resources |
| 179 | + |
| 180 | +If you leave this stack running, you will continue to incur charges. To remove all resources |
| 181 | +created by Terraform, [refresh your Grafana API key](#apikey) and run: |
| 182 | + |
| 183 | +```sh |
| 184 | +terraform destroy -var-file=terraform.tfvars |
| 185 | +``` |
| 186 | + |
| 187 | + |
| 188 | +<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
| 189 | +## Requirements |
| 190 | + |
| 191 | +| Name | Version | |
| 192 | +|------|---------| |
| 193 | +| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.1.0, < 1.3.0 | |
| 194 | +| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 | |
| 195 | +| <a name="requirement_grafana"></a> [grafana](#requirement\_grafana) | >= 1.25.0 | |
| 196 | +| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.4.1 | |
| 197 | +| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | >= 1.14 | |
| 198 | +| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.10 | |
| 199 | + |
| 200 | +## Providers |
| 201 | + |
| 202 | +| Name | Version | |
| 203 | +|------|---------| |
| 204 | +| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 | |
| 205 | + |
| 206 | +## Modules |
| 207 | + |
| 208 | +| Name | Source | Version | |
| 209 | +|------|--------|---------| |
| 210 | +| <a name="module_eks_observability_accelerator"></a> [eks\_observability\_accelerator](#module\_eks\_observability\_accelerator) | ../../ | n/a | |
| 211 | +| <a name="module_workloads_java"></a> [workloads\_java](#module\_workloads\_java) | ../../modules/workloads/java | n/a | |
| 212 | + |
| 213 | +## Resources |
| 214 | + |
| 215 | +| Name | Type | |
| 216 | +|------|------| |
| 217 | +| [aws_eks_cluster.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster) | data source | |
| 218 | +| [aws_eks_cluster_auth.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source | |
| 219 | + |
| 220 | +## Inputs |
| 221 | + |
| 222 | +| Name | Description | Type | Default | Required | |
| 223 | +|------|-------------|------|---------|:--------:| |
| 224 | +| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS Region | `string` | n/a | yes | |
| 225 | +| <a name="input_eks_cluster_id"></a> [eks\_cluster\_id](#input\_eks\_cluster\_id) | Name of the EKS cluster | `string` | n/a | yes | |
| 226 | +| <a name="input_grafana_api_key"></a> [grafana\_api\_key](#input\_grafana\_api\_key) | API key for authorizing the Grafana provider to make changes to Amazon Managed Grafana | `string` | `""` | no | |
| 227 | +| <a name="input_managed_grafana_workspace_id"></a> [managed\_grafana\_workspace\_id](#input\_managed\_grafana\_workspace\_id) | Amazon Managed Grafana Workspace ID | `string` | `""` | no | |
| 228 | +| <a name="input_managed_prometheus_workspace_id"></a> [managed\_prometheus\_workspace\_id](#input\_managed\_prometheus\_workspace\_id) | Amazon Managed Service for Prometheus Workspace ID | `string` | `""` | no | |
| 229 | + |
| 230 | +## Outputs |
| 231 | + |
| 232 | +| Name | Description | |
| 233 | +|------|-------------| |
| 234 | +| <a name="output_aws_region"></a> [aws\_region](#output\_aws\_region) | AWS Region | |
| 235 | +| <a name="output_eks_cluster_id"></a> [eks\_cluster\_id](#output\_eks\_cluster\_id) | EKS Cluster Id | |
| 236 | +| <a name="output_eks_cluster_version"></a> [eks\_cluster\_version](#output\_eks\_cluster\_version) | EKS Cluster version | |
| 237 | +| <a name="output_grafana_dashboard_urls"></a> [grafana\_dashboard\_urls](#output\_grafana\_dashboard\_urls) | URLs for dashboards created | |
| 238 | +| <a name="output_managed_prometheus_workspace_endpoint"></a> [managed\_prometheus\_workspace\_endpoint](#output\_managed\_prometheus\_workspace\_endpoint) | Amazon Managed Prometheus workspace endpoint | |
| 239 | +| <a name="output_managed_prometheus_workspace_id"></a> [managed\_prometheus\_workspace\_id](#output\_managed\_prometheus\_workspace\_id) | Amazon Managed Prometheus workspace ID | |
| 240 | +<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
0 commit comments