Skip to content

Commit a5a444e

Browse files
lewinkedrsbonclay7
andauthored
Java dev (#52)
* Update Java module * Add java example * Updating Readme * Updating Readme Images * Pre-commit * Fixing readme screenshot * Update README.md Co-authored-by: Rodrigue Koffi <[email protected]>
1 parent 4fdb719 commit a5a444e

File tree

12 files changed

+581
-71
lines changed

12 files changed

+581
-71
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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+
![image](https://user-images.githubusercontent.com/10175027/195903211-c47a5746-daa7-41f2-a6ea-bfe13f630c63.png)
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 -->
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
provider "aws" {
2+
region = local.region
3+
}
4+
5+
data "aws_eks_cluster_auth" "this" {
6+
name = var.eks_cluster_id
7+
}
8+
9+
data "aws_eks_cluster" "this" {
10+
name = var.eks_cluster_id
11+
}
12+
13+
provider "kubernetes" {
14+
host = local.eks_cluster_endpoint
15+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority[0].data)
16+
token = data.aws_eks_cluster_auth.this.token
17+
}
18+
19+
provider "helm" {
20+
kubernetes {
21+
host = local.eks_cluster_endpoint
22+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.this.certificate_authority[0].data)
23+
token = data.aws_eks_cluster_auth.this.token
24+
}
25+
}
26+
27+
locals {
28+
region = var.aws_region
29+
eks_cluster_endpoint = data.aws_eks_cluster.this.endpoint
30+
create_new_workspace = var.managed_prometheus_workspace_id == "" ? true : false
31+
tags = {
32+
Source = "github.com/aws-observability/terraform-aws-observability-accelerator"
33+
}
34+
}
35+
36+
# deploys the base module
37+
module "eks_observability_accelerator" {
38+
# source = "aws-observability/terrarom-aws-observability-accelerator"
39+
source = "../../"
40+
41+
aws_region = var.aws_region
42+
eks_cluster_id = var.eks_cluster_id
43+
44+
# deploys AWS Distro for OpenTelemetry operator into the cluster
45+
enable_amazon_eks_adot = true
46+
47+
# reusing existing certificate manager? defaults to true
48+
enable_cert_manager = true
49+
50+
# creates a new Amazon Managed Prometheus workspace, defaults to true
51+
enable_managed_prometheus = local.create_new_workspace
52+
53+
# reusing existing Amazon Managed Prometheus if specified
54+
managed_prometheus_workspace_id = var.managed_prometheus_workspace_id
55+
managed_prometheus_workspace_region = null # defaults to the current region, useful for cross region scenarios (same account)
56+
57+
# sets up the Amazon Managed Prometheus alert manager at the workspace level
58+
enable_alertmanager = true
59+
60+
# reusing existing Amazon Managed Grafana workspace
61+
enable_managed_grafana = false
62+
managed_grafana_workspace_id = var.managed_grafana_workspace_id
63+
grafana_api_key = var.grafana_api_key
64+
65+
tags = local.tags
66+
}
67+
68+
# https://www.terraform.io/language/modules/develop/providers
69+
# A module intended to be called by one or more other modules must not contain
70+
# any provider blocks.
71+
# This allows forcing dependency between base and workloads module
72+
provider "grafana" {
73+
url = module.eks_observability_accelerator.managed_grafana_workspace_endpoint
74+
auth = var.grafana_api_key
75+
}
76+
77+
module "workloads_java" {
78+
source = "../../modules/workloads/java"
79+
80+
eks_cluster_id = module.eks_observability_accelerator.eks_cluster_id
81+
82+
dashboards_folder_id = module.eks_observability_accelerator.grafana_dashboards_folder_id
83+
managed_prometheus_workspace_id = module.eks_observability_accelerator.managed_prometheus_workspace_id
84+
85+
managed_prometheus_workspace_endpoint = module.eks_observability_accelerator.managed_prometheus_workspace_endpoint
86+
managed_prometheus_workspace_region = module.eks_observability_accelerator.managed_prometheus_workspace_region
87+
88+
# optional, defaults to 60s interval and 15s timeout
89+
prometheus_config = {
90+
global_scrape_interval = "60s"
91+
global_scrape_timeout = "15s"
92+
scrape_sample_limit = 2000
93+
}
94+
95+
tags = local.tags
96+
97+
depends_on = [
98+
module.eks_observability_accelerator
99+
]
100+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
output "eks_cluster_id" {
2+
description = "EKS Cluster Id"
3+
value = module.eks_observability_accelerator.eks_cluster_id
4+
}
5+
6+
output "aws_region" {
7+
description = "AWS Region"
8+
value = module.eks_observability_accelerator.aws_region
9+
}
10+
11+
output "eks_cluster_version" {
12+
description = "EKS Cluster version"
13+
value = module.eks_observability_accelerator.eks_cluster_version
14+
}
15+
16+
output "managed_prometheus_workspace_endpoint" {
17+
description = "Amazon Managed Prometheus workspace endpoint"
18+
value = module.eks_observability_accelerator.managed_prometheus_workspace_endpoint
19+
}
20+
21+
output "managed_prometheus_workspace_id" {
22+
description = "Amazon Managed Prometheus workspace ID"
23+
value = module.eks_observability_accelerator.managed_prometheus_workspace_id
24+
}
25+
26+
output "grafana_dashboard_urls" {
27+
description = "URLs for dashboards created"
28+
value = module.workloads_java.grafana_dashboard_urls
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "eks_cluster_id" {
2+
description = "Name of the EKS cluster"
3+
type = string
4+
}
5+
variable "aws_region" {
6+
description = "AWS Region"
7+
type = string
8+
}
9+
variable "managed_prometheus_workspace_id" {
10+
description = "Amazon Managed Service for Prometheus Workspace ID"
11+
type = string
12+
default = ""
13+
}
14+
variable "managed_grafana_workspace_id" {
15+
description = "Amazon Managed Grafana Workspace ID"
16+
type = string
17+
default = ""
18+
}
19+
variable "grafana_api_key" {
20+
description = "API key for authorizing the Grafana provider to make changes to Amazon Managed Grafana"
21+
type = string
22+
default = ""
23+
sensitive = true
24+
}

0 commit comments

Comments
 (0)