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.

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