You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
13 KiB

  1. {
  2. "AWSTemplateFormatVersion": "2010-09-09",
  3. "Parameters":
  4. {
  5. "paramImageID" : {
  6. "Type" : "AWS::EC2::Image::Id",
  7. "Default" : "ami-f573e19a",
  8. "Description" : "Amazon Image ID"
  9. },
  10. "paramInstanceType" : {
  11. "Type" : "String",
  12. "Default" : "t2.nano",
  13. "AllowedValues" : ["t2.nano", "t2.micro", "m1.small"],
  14. "Description" : "Enter t2.micro, m1.small, or m1.large. Default is t2.micro."
  15. },
  16. "paramKeyPair" : {
  17. "Type" : "AWS::EC2::KeyPair::KeyName",
  18. "Description" : "Amazon EC2 Key Pair"
  19. },
  20. "paramIamInstanceRole" : {
  21. "Type" : "String",
  22. "Default": "ec2-s3-vertsys-access-role",
  23. "Description" : "IAM Instance Role"
  24. },
  25. "paramVPC" : {
  26. "Type" : "AWS::EC2::VPC::Id",
  27. "Description" : "VPC"
  28. },
  29. "paramSubnetIDs" : {
  30. "Type" : "List<AWS::EC2::Subnet::Id>",
  31. "Description" : "Subnet IDs"
  32. },
  33. "paramAvailabilityZones" : {
  34. "Type" : "List<AWS::EC2::AvailabilityZone::Name>",
  35. "Description" : "AvailabilityZones"
  36. }
  37. },
  38. "Resources": {
  39. "lcVertSysAutoScaleConfigv11": {
  40. "Type": "AWS::AutoScaling::LaunchConfiguration",
  41. "Properties": {
  42. "AssociatePublicIpAddress": true,
  43. "ImageId": { "Ref" : "paramImageID" },
  44. "InstanceType": { "Ref" : "paramInstanceType" },
  45. "KeyName": { "Ref" : "paramKeyPair" },
  46. "IamInstanceProfile": { "Ref" : "paramIamInstanceRole" },
  47. "SecurityGroups": [ { "Ref": "sgCloudCompDemoSecurityGroup" } ],
  48. "UserData": {
  49. "Fn::Base64": {
  50. "Fn::Join": [
  51. "",
  52. [
  53. "#!/bin/bash\n",
  54. "\n",
  55. "# this script will be run during the boot process by each VertSys instance created\n",
  56. "# in AWS currently this is a plain bash script that requires a RedHat based image\n",
  57. "# (AMI) could be ported to cloud-init for better compatibility with other Linux\n",
  58. "# distros\n",
  59. "#\n",
  60. "# see https://docs.aws.amazon.com/de_de/AWSEC2/latest/UserGuide/user-data.html\n",
  61. "\n",
  62. "# Config\n",
  63. "SCRIPT_ROOT_PATH=\"/tmp/init-script\"\n",
  64. "VERTSYS_PATH=\"$SCRIPT_ROOT_PATH/verteilte-systeme-bsc-ai-examples/VerteilteSysteme-Examples/build/\"\n",
  65. "#JARS = \"TCPServer.jar TCPServerMulti.jar UDPServer.jar UDPServerMulti.jar UDPTimeCounterServer.jar TCPTimeCounterServer.jar TCPPerfServer.jar\"\n",
  66. "JARS=\"TCPServer.jar TCPPerfServer.jar UDPServer.jar UDPTimeCounterServer.jar TCPTimeCounterServer.jar TCPTimeCounterRESTServer.jar\"\n",
  67. "REPO=\"https://gogs.informatik.hs-fulda.de/srieger/verteilte-systeme-bsc-ai-examples.git\"\n",
  68. "\n",
  69. "# Create path to run the script\n",
  70. "mkdir $SCRIPT_ROOT_PATH\n",
  71. "cd $SCRIPT_ROOT_PATH\n",
  72. "\n",
  73. "# fetch user-data (should be the content of this script itself ;)) to be able check it later and run it again\n",
  74. "wget http://169.254.169.254/latest/user-data\n",
  75. "chmod +x user-data\n",
  76. "wget http://169.254.169.254/latest/meta-data/placement/availability-zone\n",
  77. "\n",
  78. "# setting console prompt to include location (availability zone and region in AWS)\n",
  79. "echo \"PS1='[\\u@\\h@\\e[32m`cat /tmp/init-script/availability-zone`\\e[32m \\W]$ '\" >>/root/.bashrc\n",
  80. "echo \"PS1='[\\u@\\h@\\e[32m`cat /tmp/init-script/availability-zone`\\e[39m \\W]$ '\" >>/home/ec2-user/.bashrc\n",
  81. "\n",
  82. "# wait for internet connection, should not be necessary, but just in case ;)\n",
  83. "while ! ping -c 1 -W 1 8.8.8.8; do\n",
  84. " echo \"Waiting for 8.8.8.8 - network interface might be down...\"\n",
  85. " sleep 1\n",
  86. "done\n",
  87. "\n",
  88. "# installation of required packages\n",
  89. "echo \"Installing packages...\"\n",
  90. "sudo yum update -y\n",
  91. "sudo yum install -y java git\n",
  92. "#rm -rf verteilte-systeme-bsc-ai-examples\n",
  93. "if [ ! -d $VERTSYS_PATH ]; then\n",
  94. " echo \"Cloning repo...\"\n",
  95. " git clone $REPO\n",
  96. "fi\n",
  97. "\n",
  98. "# killall running screens, .e.g to cleanup if script is run again after boot\n",
  99. "killall screen\n",
  100. "# start all jars\n",
  101. "echo \"Starting JARS: ${JARS}...\"\n",
  102. "for JAR in $JARS; do\n",
  103. " echo \"Starting ${JAR}...\"\n",
  104. " sudo screen -dmS $JAR -L java -jar $VERTSYS_PATH/$JAR\n",
  105. "done\n",
  106. "\n",
  107. "# wait a second, to allow java services to start\n",
  108. "sleep 1\n",
  109. "\n",
  110. "# output status of ports 36000-36199 and all running processes\n",
  111. "echo \"Status:\"\n",
  112. "sudo netstat -taupen | grep 36[0,1][0-9][0-9]\n",
  113. "sudo ps aux | grep java\n",
  114. "\n",
  115. "# Example for path and contents:\n",
  116. "#\n",
  117. "# ./verteilte-systeme-bsc-ai-examples/VerteilteSysteme-Examples/build/\n",
  118. "#\n",
  119. "#build-server-jars.xml TCPServerMulti.jar\n",
  120. "#RMIEchoServer.jar UDPServer.jar\n",
  121. "#RMIMandelbrotCalculationsServer.jar UDPServerMulti.jar\n",
  122. "#TCPPerfServer.jar UDPTimeCounterServer.jar\n",
  123. "#TCPServer.jar\n"
  124. ]
  125. ]
  126. }
  127. }
  128. }
  129. },
  130. "sgCloudCompDemoSecurityGroup": {
  131. "Type": "AWS::EC2::SecurityGroup",
  132. "Properties": {
  133. "GroupDescription": "CloudComp Counter Demo",
  134. "VpcId": { "Ref": "paramVPC" }
  135. }
  136. },
  137. "ingress1": {
  138. "Type": "AWS::EC2::SecurityGroupIngress",
  139. "Properties": {
  140. "GroupId": { "Ref": "sgCloudCompDemoSecurityGroup" },
  141. "IpProtocol": "tcp",
  142. "FromPort": "36037",
  143. "ToPort": "36137",
  144. "CidrIp": "0.0.0.0/0"
  145. }
  146. },
  147. "ingress2": {
  148. "Type": "AWS::EC2::SecurityGroupIngress",
  149. "Properties": {
  150. "GroupId": { "Ref": "sgCloudCompDemoSecurityGroup" },
  151. "IpProtocol": "tcp",
  152. "FromPort": "22",
  153. "ToPort": "22",
  154. "CidrIp": "0.0.0.0/0"
  155. }
  156. },
  157. "ingress3": {
  158. "Type": "AWS::EC2::SecurityGroupIngress",
  159. "Properties": {
  160. "GroupId": { "Ref": "sgCloudCompDemoSecurityGroup" },
  161. "IpProtocol": "udp",
  162. "FromPort": "36037",
  163. "ToPort": "36137",
  164. "CidrIp": "0.0.0.0/0"
  165. }
  166. },
  167. "egress1": {
  168. "Type": "AWS::EC2::SecurityGroupEgress",
  169. "Properties": {
  170. "GroupId": { "Ref": "sgCloudCompDemoSecurityGroup" },
  171. "IpProtocol": "-1",
  172. "CidrIp": "0.0.0.0/0"
  173. }
  174. },
  175. "asgCloudCompAutoScaleGroup": {
  176. "Type": "AWS::AutoScaling::AutoScalingGroup",
  177. "Properties": {
  178. "AvailabilityZones": { "Ref" : "paramAvailabilityZones" },
  179. "TargetGroupARNs": [ { "Ref": "elbCloudCompTargetGroup" } ],
  180. "Cooldown": "30",
  181. "DesiredCapacity": "1",
  182. "HealthCheckGracePeriod": "60",
  183. "HealthCheckType": "EC2",
  184. "MaxSize": "3",
  185. "MinSize": "1",
  186. "VPCZoneIdentifier": { "Ref": "paramSubnetIDs" },
  187. "LaunchConfigurationName": {
  188. "Ref": "lcVertSysAutoScaleConfigv11"
  189. },
  190. "MetricsCollection": [
  191. {
  192. "Granularity": "1Minute",
  193. "Metrics": [
  194. "GroupPendingInstances",
  195. "GroupMinSize",
  196. "GroupDesiredCapacity",
  197. "GroupTerminatingInstances",
  198. "GroupInServiceInstances",
  199. "GroupStandbyInstances",
  200. "GroupMaxSize",
  201. "GroupTotalInstances"
  202. ]
  203. }
  204. ],
  205. "TerminationPolicies": [ "Default" ]
  206. }
  207. },
  208. "sgCloudCompScalePolicy": {
  209. "Type": "AWS::AutoScaling::ScalingPolicy",
  210. "DependsOn": "elbCloudCompListener",
  211. "Properties": {
  212. "PolicyType": "TargetTrackingScaling",
  213. "EstimatedInstanceWarmup": 60,
  214. "TargetTrackingConfiguration": {
  215. "DisableScaleIn": false,
  216. "TargetValue": 5,
  217. "PredefinedMetricSpecification": {
  218. "PredefinedMetricType": "ALBRequestCountPerTarget",
  219. "ResourceLabel": { "Fn::Join": [ "/", [
  220. "app/elbCloudCompLoadBalancer", { "Fn::Select": [ "3", { "Fn::Split": [ "/", { "Ref": "elbCloudCompLoadBalancer" } ] } ] },
  221. "targetgroup/elbCloudCompTargetGroup", { "Fn::Select": [ "2", { "Fn::Split": [ "/", { "Ref": "elbCloudCompTargetGroup" } ] } ] }
  222. ]
  223. ]
  224. }
  225. }
  226. },
  227. "AutoScalingGroupName": {
  228. "Ref": "asgCloudCompAutoScaleGroup"
  229. }
  230. }
  231. },
  232. "elbCloudCompLoadBalancer": {
  233. "Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
  234. "Properties": {
  235. "Name": "elbCloudCompLoadBalancer",
  236. "IpAddressType": "ipv4",
  237. "Type": "application",
  238. "Scheme": "internet-facing",
  239. "SecurityGroups": [ { "Ref": "sgCloudCompDemoSecurityGroup" } ],
  240. "Subnets": { "Ref": "paramSubnetIDs" }
  241. }
  242. },
  243. "elbCloudCompTargetGroup": {
  244. "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
  245. "Properties": {
  246. "Port": 36042,
  247. "HealthCheckIntervalSeconds": 30,
  248. "HealthCheckTimeoutSeconds": 5,
  249. "HealthyThresholdCount": 2,
  250. "UnhealthyThresholdCount": 2,
  251. "HealthCheckPath": "/counter",
  252. "HealthCheckProtocol": "HTTP",
  253. "TargetGroupAttributes": [
  254. {
  255. "Key": "deregistration_delay.timeout_seconds",
  256. "Value": "20"
  257. }
  258. ],
  259. "Protocol": "HTTP",
  260. "TargetType": "instance",
  261. "Matcher": {
  262. "HttpCode": "200"
  263. },
  264. "Name": "elbCloudCompTargetGroup",
  265. "VpcId": { "Ref": "paramVPC" }
  266. }
  267. },
  268. "elbCloudCompListener": {
  269. "Type": "AWS::ElasticLoadBalancingV2::Listener",
  270. "Properties": {
  271. "LoadBalancerArn": {
  272. "Ref": "elbCloudCompLoadBalancer"
  273. },
  274. "Protocol": "HTTP",
  275. "Port": 36042,
  276. "DefaultActions": [
  277. {
  278. "Type": "forward",
  279. "TargetGroupArn": { "Ref": "elbCloudCompTargetGroup" }
  280. }
  281. ]
  282. }
  283. }
  284. },
  285. "Outputs":
  286. {
  287. "LoadBalancer" : {
  288. "Description": "Load Balancer",
  289. "Value": { "Ref" : "elbCloudCompLoadBalancer" }
  290. },
  291. "LoadBalancerDns" : {
  292. "Description": "Load Balancer DNS",
  293. "Value": { "Fn::GetAtt" : ["elbCloudCompLoadBalancer", "DNSName"] }
  294. },
  295. "LoadBalancerURL" : {
  296. "Description": "Load Balancer URL",
  297. "Value": { "Fn::Join": [ ":", [ { "Fn::GetAtt" : ["elbCloudCompLoadBalancer", "DNSName"] }, "36042/counter" ] ] }
  298. }
  299. },
  300. "Description": "CloudComp Counter Demo"
  301. }