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

wuweijie 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 ef7ff48  feat(cloudformation): Support using cloudwatch collect 
`ShardingSphereProxy` logs (#147)
ef7ff48 is described below

commit ef7ff48f86d9d04c2b322856e2d8ba363c9ae06b
Author: pierce <[email protected]>
AuthorDate: Mon Dec 12 15:11:09 2022 +0800

    feat(cloudformation): Support using cloudwatch collect 
`ShardingSphereProxy` logs (#147)
    
    * feat(cloudformation): Support using cloudwatch collect 
`ShardingSphereProxy` logs.
    
    Signed-off-by: shardingsphere <[email protected]>
    
    * feat(cloudformation): Support using cloudwatch collect 
`ShardingSphereProxy` logs.
    
    Signed-off-by: shardingsphere <[email protected]>
    Signed-off-by: xuanyuan300 <[email protected]>
    
    Signed-off-by: shardingsphere <[email protected]>
    Signed-off-by: xuanyuan300 <[email protected]>
    Co-authored-by: shardingsphere <[email protected]>
---
 .licenserc.yaml                                    |   3 +
 cloudformation/multi-az/cf.rb                      | 183 +++++++++++++++++----
 cloudformation/multi-az/cfn-hup.conf               |   4 +
 cloudformation/multi-az/cfn-hup.service            |   8 +
 .../multi-az/cloudwatch-agent-auto-reloader.conf   |   5 +
 cloudformation/multi-az/cloudwatch-agent.json      |  64 +++++++
 .../multi-az/shardingsphere-cloud-init.yml         |   8 +
 7 files changed, 242 insertions(+), 33 deletions(-)

diff --git a/.licenserc.yaml b/.licenserc.yaml
index 9db2313..f21aada 100644
--- a/.licenserc.yaml
+++ b/.licenserc.yaml
@@ -36,5 +36,8 @@ header:
     - '**/go.sum'
     - 'shardingsphere-operator/build/.dockerignore'
     - 'shardingsphere-operator/api/v1alpha1/zz_generated.deepcopy.go'
+    - 'cloudformation/multi-az/*.conf'
+    - 'cloudformation/multi-az/*.service'
+    - 'cloudformation/multi-az/*.json'
 
   comment: on-failure
\ No newline at end of file
diff --git a/cloudformation/multi-az/cf.rb b/cloudformation/multi-az/cf.rb
index 9d341f8..d5b7e97 100644
--- a/cloudformation/multi-az/cf.rb
+++ b/cloudformation/multi-az/cf.rb
@@ -18,12 +18,14 @@
 CloudFormation {
   Description "Deploy a ShardingSphere Proxy Cluster in MultiAz mode"
 
+
+
   Parameter("ZookeeperInstanceType") {
     String
     Default "t2.nano"
   }
 
-  Parameter("ShardingSphereInstanceType") {
+  Parameter("ShardingSphereProxyInstanceType") {
     String
     Default "t2.micro"
   }
@@ -69,12 +71,12 @@ CloudFormation {
     Description "The zone id corresponding to HostedZoneName"
   }
 
-  Parameter("ShardingSpherePort") {
+  Parameter("ShardingSphereProxyPort") {
     Integer
     Default 3307
   }
 
-  Parameter("ShardingSphereVersion") {
+  Parameter("ShardingSphereProxyVersion") {
     String
     Default "5.2.1"
   }
@@ -98,7 +100,7 @@ CloudFormation {
 
   Parameter("ShardingSphereProxyAsgHealthCheckGracePeriod") {
     Integer
-    Default 60
+    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";
   }
 
@@ -155,15 +157,121 @@ CloudFormation {
     }
   end
 
-  launchtemplate_resource_name = "shardingsphereproxyLaunchtemplate"
-  launchtemplate_name = "shardingsphereproxy-launchtemplate"
+  role_name = "ShardingSphereProxySTSRole"
+  IAM_Role(role_name) {
+    RoleName role_name
+    AssumeRolePolicyDocument(
+        :Version => "2012-10-17",
+        :Statement => [
+            {
+              :Action => "sts:AssumeRole",
+              :Principal => {
+                :Service => "ec2.amazonaws.com"
+              },
+              :Effect => "Allow"
+            }
+        ]
+    )
+  }
+
+  policy_name = "ShardingSphereProxyAccessPolicy"
+  IAM_Policy(policy_name) do
+    PolicyName policy_name
+    PolicyDocument(
+      :Version => "2012-10-17",
+      :Statement => [
+          {
+            "Action": [
+              "cloudwatch:PutMetricData",
+              "ec2:DescribeTags",
+              "logs:PutLogEvents",
+              "logs:DescribeLogStreams",
+              "logs:DescribeLogGroups",
+              "logs:CreateLogStream",
+              "logs:CreateLogGroup"
+            ],
+            "Effect": "Allow",
+            "Resource": "*"
+          }
+      ]
+    )
+    Role Ref(role_name)
+  end
+
+  instance_profile_name = "ShardingSphereProxyInstanceProfile"
+  IAM_InstanceProfile(instance_profile_name) do
+    InstanceProfileName instance_profile_name
+    Roles [Ref(role_name)]
+  end
+
+  asg_name = "ShardingSphereProxyASG"
+  launchtemplate_name = "ShardingSphereProxyLaunchtemplate"
+
+  EC2_LaunchTemplate(launchtemplate_name) {
+    Metadata(
+      "AWS::CloudFormation::Init" => {
+          :configSets => {
+              :default => [
+                  "01_setupCfnHup", "02_config-amazon-cloudwatch-agent", 
"03_restart_amazon-cloudwatch-agent"
+              ],
+              :UpdateEnvironment => [ "02_config-amazon-cloudwatch-agent", 
"03_restart_amazon-cloudwatch-agent" ],
+          },
+
+          "02_config-amazon-cloudwatch-agent" => {
+              :files => {
+                  
"/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json" => {
+                      :content => IO.read("./cloudwatch-agent.json")
+                  }
+              }
+          },
+          "03_restart_amazon-cloudwatch-agent" => {
+              :commands => {
+                  "01_stop_service" => {
+                      :command => 
"/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop"
+                  },
+                  "02_start_service" => {
+                      :command => 
"/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a 
fetch-config -m ec2 -c 
file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s"
+                  }
+             }
+          },
+          "01_setupCfnHup" => {
+              :files => {
+                  "/etc/cfn/cfn-hup.conf" => {
+                      :content => FnSub(IO.read("./cfn-hup.conf")),
+                      :mode => "000400",
+                      :owner => "root",
+                      :group => "root",
+                  },
+                  
"/etc/cfn/hooks.d/amazon-cloudwatch-agent-auto-reloader.conf" => {
+                      :content => 
FnSub(IO.read("./cloudwatch-agent-auto-reloader.conf"), :LaunchTemplateName => 
launchtemplate_name),
+                      :mode => "000400",
+                      :owner => "root",
+                      :group => "root",
+                  },
+                  "/lib/systemd/system/cfn-hup.service" => {
+                      :content => FnSub(IO.read("./cfn-hup.service"))
+                  }
+              },
+              :commands => {
+                  "01enable_cfn_hup" => {
+                      :command => FnSub("systemctl enable cfn-hup.service")
+                  },
+                  "02start_cfn_hup" => {
+                      :command => FnSub("systemctl start cfn-hup.service")
+                  }
+              }
+          }
+      }
+    )
 
-  EC2_LaunchTemplate(launchtemplate_resource_name) {
     LaunchTemplateName launchtemplate_name
     LaunchTemplateData do
       ImageId Ref("ImageId")
-      InstanceType Ref("ShardingSphereInstanceType")
+      InstanceType Ref("ShardingSphereProxyInstanceType")
       KeyName Ref("KeyName")
+      IamInstanceProfile do
+        Name Ref(instance_profile_name)
+      end
 
       MetadataOptions do
         HttpEndpoint "enabled"
@@ -181,7 +289,7 @@ CloudFormation {
           :Tags => [
             {
               :Key => "Name",
-              :Value => "shardingsphere-proxy"
+              :Value => "ShardingSphereProxy"
             }
           ]
         }
@@ -191,15 +299,19 @@ CloudFormation {
         FnSub(
           IO.read("./shardingsphere-cloud-init.yml"),
           :ZK_SERVERS => FnSub((0..2).map{|i| 
"zk-#{i+1}.${HostedZoneName}:2181" }.join(",")),
-          :VERSION => Ref("ShardingSphereVersion"),
-          :JAVA_MEM_OPTS => Ref("ShardingSphereJavaMemOpts")
+          :VERSION => Ref("ShardingSphereProxyVersion"),
+          :JAVA_MEM_OPTS => Ref("ShardingSphereJavaMemOpts"),
+          :LaunchTemplateName => launchtemplate_name,
+          :ASGName => asg_name,
         )
       )
     end
   }
 
-  ElasticLoadBalancingV2_LoadBalancer("ssinternallb") {
-    Name "shardingsphere-internal-lb"
+  lb_name = "ShardingSphereProxyLB"
+
+  ElasticLoadBalancingV2_LoadBalancer(lb_name) {
+    Name lb_name
     Scheme "internal"
     Type "network"
     
@@ -212,16 +324,18 @@ CloudFormation {
     Tags [
       Tag do
         Key "Name"
-        Value "shardingsphere"
+        Value "ShardingSphereProxy"
       end
     ]
   }
 
 
-  ElasticLoadBalancingV2_TargetGroup("sslbtg") {
-    Name "shardingsphere-lb-tg"
-    Port Ref("ShardingSpherePort")
+  tg_name = "ShardingSphereProxyLBTG"
+  ElasticLoadBalancingV2_TargetGroup(tg_name) {
+    Name tg_name
+    Port Ref("ShardingSphereProxyPort")
     Protocol "TCP"
+    HealthyThresholdCount 2
     VpcId Ref("VpcId")
     TargetGroupAttributes [
       TargetGroupAttribute do
@@ -232,14 +346,13 @@ CloudFormation {
     Tags [
       Tag do
         Key "Name"
-        Value "shardingsphere"
+        Value "ShardingSphereProxy"
       end
     ]
   }
 
-  asg_resource_name = "shardingsphereproxyAsg"
-  asg_name = "shardingsphereproxy-asg"
-  AutoScaling_AutoScalingGroup(asg_resource_name) {
+
+  AutoScaling_AutoScalingGroup(asg_name) {
     AutoScalingGroupName asg_name
     AvailabilityZones FnGetAZs(Ref("AWS::Region"))
     DesiredCapacity Ref("ShardingSphereProxyAsgDesiredCapacity")
@@ -248,40 +361,44 @@ CloudFormation {
     HealthCheckGracePeriod  Ref("ShardingSphereProxyAsgHealthCheckGracePeriod")
     HealthCheckType "ELB"
 
-    TargetGroupARNs [ Ref("sslbtg")]
+    TargetGroupARNs [ Ref(tg_name)]
 
     LaunchTemplate do
       LaunchTemplateName launchtemplate_name
-      Version FnGetAtt(launchtemplate_resource_name, "LatestVersionNumber")
+      Version FnGetAtt(launchtemplate_name, "LatestVersionNumber")
     end
+
+    CreationPolicy("ResourceSignal", { :Count => 3,  :Timeout => "PT15M" })
   }
 
-  ElasticLoadBalancingV2_Listener("sslblistener") {
-    Port Ref("ShardingSpherePort")
-    LoadBalancerArn Ref("ssinternallb")
+  listener_name = "ShardingSphereProxyLBListener"
+  ElasticLoadBalancingV2_Listener(listener_name) {
+    Port Ref("ShardingSphereProxyPort")
+    LoadBalancerArn Ref(lb_name)
     Protocol "TCP"
     DefaultActions [
       {
         :Type => "forward",
-        :TargetGroupArn => Ref("sslbtg")
+        :TargetGroupArn => Ref(tg_name)
       }
     ]
   }
 
-  Route53_RecordSet("ssinternaldomain") {
+  domain_name = "ShardingSphereProxyInternalDomain"
+  Route53_RecordSet(domain_name) {
     HostedZoneId Ref("HostedZoneId")
     Name FnSub("proxy.${HostedZoneName}")
     Type "A"
     AliasTarget do 
-      HostedZoneId FnGetAtt("ssinternallb", "CanonicalHostedZoneID")
-      DNSName FnGetAtt("ssinternallb", "DNSName")
+      HostedZoneId FnGetAtt(lb_name, "CanonicalHostedZoneID")
+      DNSName FnGetAtt(lb_name, "DNSName")
       EvaluateTargetHealth true
     end
   }
 
-  Output("ssinternaldomain") do
-    Value Ref("ssinternaldomain")
-    Export FnSub("${AWS::StackName}-ShardingSphere-Internal-Domain")
+  Output(domain_name) do
+    Value Ref(domain_name)
+    Export FnSub("${AWS::StackName}-ShardingSphereProxy-Internal-Domain")
   end
 
   (0..2).each do |i|
diff --git a/cloudformation/multi-az/cfn-hup.conf 
b/cloudformation/multi-az/cfn-hup.conf
new file mode 100644
index 0000000..f97ee69
--- /dev/null
+++ b/cloudformation/multi-az/cfn-hup.conf
@@ -0,0 +1,4 @@
+[main]
+stack=${AWS::StackId}
+region=${AWS::Region}
+interval=3
diff --git a/cloudformation/multi-az/cfn-hup.service 
b/cloudformation/multi-az/cfn-hup.service
new file mode 100644
index 0000000..7d39ff9
--- /dev/null
+++ b/cloudformation/multi-az/cfn-hup.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=cfn-hup daemon
+[Service]
+Type=simple
+ExecStart=/opt/aws/bin/cfn-hup
+Restart=always
+[Install]
+WantedBy=multi-user.target
diff --git a/cloudformation/multi-az/cloudwatch-agent-auto-reloader.conf 
b/cloudformation/multi-az/cloudwatch-agent-auto-reloader.conf
new file mode 100644
index 0000000..fe53d56
--- /dev/null
+++ b/cloudformation/multi-az/cloudwatch-agent-auto-reloader.conf
@@ -0,0 +1,5 @@
+[cfn-auto-reloader-hook]
+triggers=post.update
+path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init.02_config-amazon-cloudwatch-agent
+action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource 
${LaunchTemplateName} --region ${AWS::Region} --configsets UpdateEnvironment
+runas=root
diff --git a/cloudformation/multi-az/cloudwatch-agent.json 
b/cloudformation/multi-az/cloudwatch-agent.json
new file mode 100644
index 0000000..790a9eb
--- /dev/null
+++ b/cloudformation/multi-az/cloudwatch-agent.json
@@ -0,0 +1,64 @@
+{
+  "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
+  }
+}
\ No newline at end of file
diff --git a/cloudformation/multi-az/shardingsphere-cloud-init.yml 
b/cloudformation/multi-az/shardingsphere-cloud-init.yml
index e6f2fa0..3d725f5 100644
--- a/cloudformation/multi-az/shardingsphere-cloud-init.yml
+++ b/cloudformation/multi-az/shardingsphere-cloud-init.yml
@@ -110,5 +110,13 @@ write_files:
     systemctl enable shardingsphere
     systemctl start shardingsphere
 
+- path: /run/install_cloudwatch.sh
+  permissions: 0700
+  content: |
+    echo Installing CloudWatchAgent 
+    rpm -Uvh 
https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
+    /opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource 
${LaunchTemplateName} --region ${AWS::Region} --configsets default
+    /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource 
${ASGName} --region ${AWS::Region}
 runcmd:
 - /run/install_shardingsphere.sh
+- /run/install_cloudwatch.sh

Reply via email to