This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shardingsphere-on-cloud.git


The following commit(s) were added to refs/heads/main by this push:
     new 87d56a3  feat(terraform): support using cloudwatch collect logs for 
terraform (#151)
87d56a3 is described below

commit 87d56a3507c03f56c20025a02bd24348a12856a4
Author: pierce <[email protected]>
AuthorDate: Tue Dec 20 15:37:16 2022 +0800

    feat(terraform): support using cloudwatch collect logs for terraform (#151)
    
    Signed-off-by: xuanyuan300 <[email protected]>
    
    Signed-off-by: xuanyuan300 <[email protected]>
---
 terraform/main.tf                       |  26 ++++----
 terraform/shardingsphere/cloud-init.yml |  77 +++++++++++++++++++++++
 terraform/shardingsphere/main.tf        | 106 +++++++++++++++++++++++---------
 terraform/shardingsphere/variables.tf   |  24 +++++++-
 4 files changed, 188 insertions(+), 45 deletions(-)

diff --git a/terraform/main.tf b/terraform/main.tf
index 8029a93..5b51a90 100644
--- a/terraform/main.tf
+++ b/terraform/main.tf
@@ -40,16 +40,16 @@ module "zk" {
 }
 
 module "shardingsphere" {
-  depends_on             = [module.zk]
-  source                 = "./shardingsphere"
-  cluster_size           = 3
-  shardingsphere_version = "5.2.1"
-  key_name               = "test-tf"
-  image_id               = "ami-094bbd9e922dc515d"
-  instance_type          = "t3.medium"
-  lb_listener_port       = 3307
-  vpc_id                 = "vpc-0ef2b7440d3ade8d5"
-  subnet_ids             = ["subnet-0f388a6f23063b8c9", 
"subnet-0bc2cd85facb5ca06", "subnet-009077567350ef1b7"]
-  security_groups     = ["sg-008e74936b3f9de19"]
-  zk_servers             = module.zk.zk_node_domain
-}
\ No newline at end of file
+  depends_on                    = [module.zk]
+  source                        = "./shardingsphere"
+  cluster_size                  = 3
+  shardingsphere_proxy_version  = "5.2.1"
+  key_name                      = "test-tf"
+  image_id                      = "ami-094bbd9e922dc515d"
+  instance_type                 = "t3.medium"
+  lb_listener_port              = 3307
+  vpc_id                        = "vpc-0ef2b7440d3ade8d5"
+  subnet_ids                    = ["subnet-0f388a6f23063b8c9", 
"subnet-0bc2cd85facb5ca06", "subnet-009077567350ef1b7"]
+  security_groups               = ["sg-008e74936b3f9de19"]
+  zk_servers                    = module.zk.zk_node_domain
+}
diff --git a/terraform/shardingsphere/cloud-init.yml 
b/terraform/shardingsphere/cloud-init.yml
index 03be62c..a63d69a 100644
--- a/terraform/shardingsphere/cloud-init.yml
+++ b/terraform/shardingsphere/cloud-init.yml
@@ -107,5 +107,82 @@ write_files:
     systemctl enable shardingsphere
     systemctl start shardingsphere
 
+- path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
+  permissions: 0644
+  content: |
+    {
+      "agent": {
+        "metrics_collection_interval": 10,
+        "logfile": 
"/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
+      },
+      "metrics": {
+        "append_dimensions": {
+          "AutoScalingGroupName": "$${aws:AutoScalingGroupName}",
+          "ImageId": "$${aws:ImageId}",
+          "InstanceId": "$${aws:InstanceId}",
+          "InstanceType": "$${aws:InstanceType}"
+        },
+        "metrics_collected": {
+          "cpu": {
+            "measurement": [
+              "cpu_usage_system",
+              "cpu_usage_idle",
+              "cpu_usage_nice",
+              "cpu_usage_guest",
+              "cpu_usage_steal"
+            ]
+          },
+          "mem": {
+            "measurement": [
+              "mem_used_percent"
+            ]
+          },
+          "net": {
+            "resources": [
+              "eth0"
+            ],
+            "measurement": [
+              "bytes_sent",
+              "bytes_recv",
+              "drop_in",
+              "drop_out"
+            ]
+          },
+          "netstat": {
+            "measurement": [
+              "tcp_established",
+              "tcp_syn_sent",
+              "tcp_close"
+            ],
+            "metrics_collection_interval": 60
+          }
+        }
+      },
+      "logs": {
+        "logs_collected": {
+          "files": {
+            "collect_list": [
+              {
+                "file_path": "/usr/local/shardingsphere/logs/*.log",
+                "log_group_name": "shardingsphere-proxy.log",
+                "timezone": "UTC",
+                "timestamp_format": "%Y-%m-%d %H:%M:%S,%f"
+              }
+            ]
+          }
+        },
+        "force_flush_interval" : 15
+      }
+    }
+- path: /run/install_cloudwatch.sh
+  permissions: 0700
+  content: |
+    #!/bin/bash
+    echo Installing CloudWatch Agent
+    rpm -Uvh 
https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
+    systemctl daemon-reload
+    systemctl enable amazon-cloudwatch-agent 
+    systemctl start amazon-cloudwatch-agent
 runcmd:
 - /run/install_shardingsphere.sh
+- /run/install_cloudwatch.sh
diff --git a/terraform/shardingsphere/main.tf b/terraform/shardingsphere/main.tf
index 2891c2a..7481b24 100644
--- a/terraform/shardingsphere/main.tf
+++ b/terraform/shardingsphere/main.tf
@@ -28,16 +28,69 @@ resource "aws_network_interface" "ss" {
   security_groups = var.security_groups
 }
 
+resource "aws_iam_role" "sts" {
+  name = "shardingsphere-proxy-sts-role"
+
+  assume_role_policy = <<EOF
+{
+  "Version": "2012-10-17",
+  "Statement": [
+    {
+      "Action": "sts:AssumeRole",
+      "Principal": {
+        "Service": "ec2.amazonaws.com"
+      },
+      "Effect": "Allow",
+      "Sid": ""
+    }
+  ]
+}
+EOF
+}
+
+resource "aws_iam_role_policy" "ss" {
+  name = "sharidngsphere-proxy-policy"
+  role = aws_iam_role.sts.id
+
+  policy = <<EOF
+{
+  "Version": "2012-10-17",
+  "Statement": [
+    {
+      "Action": [
+        "cloudwatch:PutMetricData",
+        "ec2:DescribeTags",
+        "logs:PutLogEvents",
+        "logs:DescribeLogStreams",
+        "logs:DescribeLogGroups",
+        "logs:CreateLogStream",
+        "logs:CreateLogGroup"
+      ],
+      "Effect": "Allow",
+      "Resource": "*"
+    }
+  ]
+}
+EOF
+}
+
+resource "aws_iam_instance_profile" "ss" {
+  name = "shardingsphere-proxy-instance-profile"
+  role = aws_iam_role.sts.name
+}
+
 resource "aws_launch_template" "ss" {
-  count                                = var.cluster_size
-  name                                 = 
"ss-${element(data.aws_availability_zones.available.names, count.index)}"
+  name                                 = "shardingsphere-proxy-launch-template"
   image_id                             = var.image_id
   instance_initiated_shutdown_behavior = "terminate"
   instance_type                        = var.instance_type
   key_name                             = var.key_name
+  iam_instance_profile {
+    name = aws_iam_instance_profile.ss.name
+  }
 
   user_data = base64encode(templatefile("${path.module}/cloud-init.yml", {
-    version    = var.shardingsphere_version
+    version    = var.shardingsphere_proxy_version
     zk_servers = join(",", var.zk_servers)
   }))
 
@@ -52,38 +105,28 @@ resource "aws_launch_template" "ss" {
     enabled = true
   }
 
-  network_interfaces {
-    delete_on_termination = false
-    device_index          = 0
-    network_interface_id  = element(aws_network_interface.ss.*.id, count.index)
-  }
+  vpc_security_group_ids = var.security_groups
 
   tag_specifications {
     resource_type = "instance"
 
     tags = {
-      Name = "ss-${count.index + 1}"
+      Name = "shardingsphere-proxy"
     }
   }
 }
 
 resource "aws_autoscaling_group" "ss" {
-  count                     = var.cluster_size
-  name                      = "ss-${count.index + 1}"
-  availability_zones        = 
[element(data.aws_availability_zones.available.names, count.index)]
-  desired_capacity          = 1
-  max_size                  = 1
+  name                      = "shardingsphere-proxy-asg"
+  availability_zones        = data.aws_availability_zones.available.names
+  desired_capacity          = var.shardingsphere_proxy_asg_desired_capacity
   min_size                  = 1
-  health_check_grace_period = 300
-  health_check_type         = "EC2"
+  max_size                  = var.shardingsphere_proxy_asg_max_size
+  health_check_grace_period = 
var.shardingsphere_proxy_asg_healthcheck_grace_period
+  health_check_type         = "ELB"
 
   launch_template {
-    id = element(aws_launch_template.ss.*.id,
-      index(
-        aws_launch_template.ss.*.name,
-        "ss-${element(data.aws_availability_zones.available.names, 
count.index)}"
-      )
-    )
+    id      = aws_launch_template.ss.id
     version = "$Latest"
   }
 
@@ -107,25 +150,30 @@ resource "aws_lb" "ss" {
   }
 
   tags = {
-    Name = "shardingsphere"
+    Name = "shardingsphere-proxy"
   }
 }
 
 resource "aws_lb_target_group" "ss_tg" {
-  name               = "shardingsphere-lb-tg"
+  name               = "shardingsphere-proxy-lb-tg"
   port               = var.lb_listener_port
   protocol           = "TCP"
   vpc_id             = var.vpc_id
   preserve_client_ip = false
 
+  health_check {
+       protocol = "TCP"
+    healthy_threshold = 2
+    unhealthy_threshold = 2
+  }
+
   tags = {
-    Name = "shardingsphere"
+    Name = "shardingsphere-proxy"
   }
 }
 
 resource "aws_autoscaling_attachment" "asg_attachment_lb" {
-  count                  = var.cluster_size
-  autoscaling_group_name = element(aws_autoscaling_group.ss.*.id, count.index)
+  autoscaling_group_name = aws_autoscaling_group.ss.id
   lb_target_group_arn    = aws_lb_target_group.ss_tg.arn
 }
 
@@ -141,7 +189,7 @@ resource "aws_lb_listener" "ss" {
   }
 
   tags = {
-    Name = "shardingsphere"
+    Name = "shardingsphere-proxy"
   }
 }
 
@@ -160,4 +208,4 @@ resource "aws_route53_record" "ss" {
     zone_id                = aws_lb.ss.zone_id
     evaluate_target_health = true
   }
-}
\ No newline at end of file
+}
diff --git a/terraform/shardingsphere/variables.tf 
b/terraform/shardingsphere/variables.tf
index 5edfe24..54dbe10 100644
--- a/terraform/shardingsphere/variables.tf
+++ b/terraform/shardingsphere/variables.tf
@@ -19,9 +19,27 @@ variable "cluster_size" {
   description = "The cluster size that same size as available_zones"
 }
 
-variable "shardingsphere_version" {
+variable "shardingsphere_proxy_version" {
   type        = string
-  description = "The shardingsphere version"
+  description = "The shardingsphere proxy version"
+}
+
+variable "shardingsphere_proxy_asg_desired_capacity" {
+  type        = string
+  default     = "3"
+  description = "The desired capacity is the initial capacity of the Auto 
Scaling group at the time of its creation and the capacity it attempts to 
maintain. see 
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-desiredcapacitytype,
 The default value is 3"
+}
+
+variable "shardingsphere_proxy_asg_max_size" {
+  type        = string
+  default     = "6"
+  description = "The maximum size of ShardingSphere Proxy Auto Scaling Group. 
The default values is 6"
+}
+
+variable "shardingsphere_proxy_asg_healthcheck_grace_period" {
+  type        = number
+  default     = 120
+  description = "The amount of time, in seconds, that Amazon EC2 Auto Scaling 
waits before checking the health status of an EC2 instance that has come into 
service and marking it unhealthy due to a failed health check. see 
https://docs.aws.amazon.com/autoscaling/ec2/userguide/health-check-grace-period.html";
 }
 
 variable "image_id" {
@@ -52,7 +70,7 @@ variable "subnet_ids" {
 variable "security_groups" {
   type        = list(string)
   default     = []
-  description = "List of The Security groups"
+  description = "List of The Security group IDs"
 }
 
 variable "lb_listener_port" {

Reply via email to