{"componentChunkName":"component---src-templates-blog-js","path":"/ctf/aws-formation/tp5-auto-scaling-group","result":{"data":{"markdownRemark":{"html":"<h1>TP5-ASG</h1>\n<h2>1. Autoscaling Theory</h2>\n<h3>Definition</h3>\n<p><a href=\"https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html\">https://docs.aws.amazon.com/autoscaling/ec2/userguide/AutoScalingGroup.html</a></p>\n<p>An <strong>Auto Scaling group</strong> :</p>\n<ul>\n<li>\n<p>is <em>a collection of Amazon EC2 instances</em> </p>\n<ul>\n<li>(that are treated as a logical grouping for the purposes of automatic scaling and management)</li>\n</ul>\n</li>\n<li>\n<p>enables you to <em>use EC2 Auto Scaling features</em> : </p>\n<ul>\n<li>such as health check replacements </li>\n<li>and scaling policies.</li>\n</ul>\n</li>\n<li>\n<p>its size (<em>desired capacity</em>) = number of its instances  :</p>\n<ul>\n<li>between the minimum and maximum capacity specified in <em>scaling policies</em></li>\n<li>adjustment possible</li>\n<li>\n<p>maintained by periodic health check</p>\n<ul>\n<li>If an instance becomes unhealthy, the group terminates the unhealthy instance and launches another instance to replace it </li>\n</ul>\n</li>\n<li>\n<p>multiple Availability Zones :</p>\n<ul>\n<li>the desired capacity is distributed across them</li>\n</ul>\n</li>\n</ul>\n</li>\n<li><em>starts by launching enough instances to meet its desired capacity</em></li>\n</ul>\n<h3>spot vs on-demand instances</h3>\n<p>An Auto Scaling group can launch On-Demand Instances, Spot Instances, or both. </p>\n<p><strong>Spot Instances</strong> =  Amazon EC2 capacity at steep discounts relative to <strong>On-Demand</strong> prices.</p>\n<p>key differences :</p>\n<ul>\n<li>Spot price varies based on demand</li>\n<li>\n<p>Amazon EC2 can terminate an individual Spot Instance (in function of its availability / price)</p>\n<ul>\n<li>a Spot Instance is terminated ->  a replacement instance is launched (to maintain the desired capacity)</li>\n</ul>\n</li>\n</ul>\n<h3>3 components</h3>\n<p><img src=\"/assets/TP5-ASG-Autoscalinggroup.png\" alt=\"asg1\" title=\"asg1\"></p>\n<ol>\n<li>\n<p>Group</p>\n<ul>\n<li>Logical component : webserver group or application group or database group</li>\n</ul>\n</li>\n<li>\n<p>Configuration Templates</p>\n<ul>\n<li>group uses a launch template or a launch configuration as a config template for its EC2 instances </li>\n</ul>\n</li>\n<li>\n<p>Scalling option</p>\n<ul>\n<li>Maintain current instance levels at all times</li>\n<li>Scalle manually</li>\n<li>Scale based on schedules</li>\n<li>Scale based on demand</li>\n<li>Use predictive scaling</li>\n</ul>\n</li>\n</ol>\n<h3>launch configuration</h3>\n<p><strong>We recommend that you use launch templates</strong> instead of launch configurations to make sure that you can use the latest features of Amazon EC2.</p>\n<p><a href=\"https://docs.aws.amazon.com/autoscaling/ec2/userguide/LaunchConfiguration.html\">https://docs.aws.amazon.com/autoscaling/ec2/userguide/LaunchConfiguration.html</a></p>\n<p>A launch configuration is an instance configuration template that an Auto Scaling group uses to launch EC2 instances. </p>\n<p>When <strong>launch configuration creation</strong>, you specify information for the instances, including :</p>\n<ul>\n<li>the ID of the Amazon Machine Image (AMI), </li>\n<li>the instance type, </li>\n<li>a key pair, </li>\n<li>one or more security groups, </li>\n<li>and a block device mapping. .</li>\n</ul>\n<p>You can specify your launch configuration with multiple Auto Scaling groups. </p>\n<ul>\n<li>one launch configuration for an Auto Scaling group at a time</li>\n<li>\n<p>you can not modify a launch configuration after you've created it :</p>\n<ul>\n<li>you have to recreated another and update your Auto Scaling group with it</li>\n</ul>\n</li>\n</ul>\n<p>When <strong>Auto Scaling group creation</strong> :</p>\n<ul>\n<li>specify a launch configuration, </li>\n<li>a launch template, </li>\n<li>or an EC2 instance (in this case, a launch configuration by default will be provided). </li>\n</ul>\n<h2>2. Autoscaling Groups Lab</h2>\n<p>ASG :  Auto Scaling groups</p>\n<h3>Creating an Auto Scaling Group Using an EC2 Instance</h3>\n<p><a href=\"https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-asg-from-instance.html\">https://docs.aws.amazon.com/autoscaling/ec2/userguide/create-asg-from-instance.html</a></p>\n<p>When you create an Auto Scaling group, you must specify the necessary information to configure :</p>\n<ul>\n<li>the Amazon EC2 instances, </li>\n<li>the subnets for the instances, </li>\n<li>and the initial number of instances.</li>\n</ul>\n<p>To configure Amazon EC2 instances, you can specify  :</p>\n<ul>\n<li>a launch configuration,</li>\n<li>a launch template,</li>\n<li>or an EC2 instance. </li>\n</ul>\n<p>When you create an Auto Scaling group using an EC2 instance, Amazon EC2 Auto Scaling creates a launch configuration for you and associates it with the Auto Scaling group. </p>\n<p>This launch configuration : </p>\n<ul>\n<li>has the same name as the Auto Scaling group,</li>\n<li>\n<p>and it derives its attributes from the specified instance ;</p>\n<ul>\n<li>such as AMI ID, </li>\n<li>instance type, </li>\n<li>and Availability Zone. </li>\n</ul>\n</li>\n</ul>\n<h3>Manual Scaling for Amazon EC2 Auto Scaling</h3>\n<p><a href=\"https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-manual-scaling.html\">https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-manual-scaling.html</a></p>\n<p>At any time, you can change the size of an existing Auto Scaling group manually.\nYou can either :</p>\n<ul>\n<li>update the desired capacity of the Auto Scaling group, </li>\n<li>or update the instances that are attached to the Auto Scaling group. </li>\n</ul>\n<p>Manually scaling your group can be useful when :</p>\n<ul>\n<li>automatic scaling is not needed </li>\n<li>or when you need to hold capacity at a fixed number of instances. </li>\n</ul>\n<h2>3. HA Architecture</h2>\n<p><strong>HA</strong> : High Availability</p>\n<p>You should always plan for failure</p>\n<p><img src=\"/assets/TP5-ASG-HA-Example.png\" alt=\"asg-example\" title=\"asg-exemple\"></p>\n<p>ASG : Auto Scalling group\nEach SN are in a different AZ</p>\n<p>What is the ideal architecture for this environment, if we need a minimum of 6 instances and mustly available ? </p>\n<ul>\n<li>3 AZ with 3 instances in each Z</li>\n</ul>\n<h3>synthesis</h3>\n<ul>\n<li>Use multiple AZ and multiple Regions where ever you can</li>\n<li>\n<p>MultiAZ != ReadReplicas for RDS</p>\n<ul>\n<li>multiAZ : for disaster recovery</li>\n<li>readreplicas : for performance</li>\n</ul>\n</li>\n<li>\n<p>Scalling out != scalling up</p>\n<ul>\n<li>out : use autoscaling group (we add additional EC2 instances inside our group)</li>\n<li>up : increase the resource inside the EC2 instance (T2macro to 6X increasing the amount of RAM or CPU)</li>\n</ul>\n</li>\n<li>consider the cost elements</li>\n<li>\n<p>there are different S3 storage classes</p>\n<ul>\n<li>\n<p>higlhy available :</p>\n<ul>\n<li>Standard S3</li>\n<li>Standard S3 infrequently access</li>\n</ul>\n</li>\n<li>\n<p>less available  </p>\n<ul>\n<li>using redundancy storage</li>\n<li>S3 single AZ</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n<h2>4. Building a Fault Tolerant WP - HA Word Press Site</h2>\n<p><img src=\"/assets/TP5-ASG-NetworkDiagram.png\" alt=\"asg-network-diagram\" title=\"asg-network-diagram\"></p>\n<p><strong>Steps</strong> : </p>\n<ol>\n<li>S3 bucket</li>\n<li>CloudFront</li>\n<li>VPC -> create a WEBDMZ security group (which allow mysql port)</li>\n<li>RDS database with multi-az deployment ; no public accessible; define initial database-name</li>\n<li>IAM (Identity Access Managment) -> create a new role</li>\n<li>Create an EC2 instance</li>\n</ol>\n<p>At the final check the </p>\n<ul>\n<li>Clound FRont</li>\n<li>RDS</li>\n</ul>\n<h2>5. Building a Fault Tolerant WP - Setting Up EC2</h2>\n<p><strong>Steps</strong> :</p>\n<ol>\n<li>configure the EC2 instance</li>\n<li>install on the EC2 WordPress</li>\n<li>create a post</li>\n<li>add to this post an image</li>\n<li>add a reduncancy by packing up this post to another S3 and checking the resilience between S3</li>\n<li>\n<p>rewrite url access </p>\n<ol>\n<li>edit the <em>.htaccess</em> file (add url rewrite rule out the cloud front)</li>\n<li>edit the <em>httpd.conf</em> file (allow the new url rewrite) (you can make a backup config of the folder)</li>\n</ol>\n</li>\n<li>restart the server to force the change(<code>server httpd.service</code>)</li>\n<li>make the bucket public and edit the public access setting to allow all input traffic</li>\n<li>check in chrome web explorer that the url rewrite has been well done</li>\n<li>create a LoadBalancer</li>\n<li>put the EC2 behind a LoadBalancer</li>\n<li>Set up Route53</li>\n<li>(optional) you can create a record set to save the dns domain to have a simpler url</li>\n</ol>\n<h2>6. Building a Fault Tolerant WP - Adding Resilience And Autoscaling</h2>\n<p><img src=\"/assets/TP5-ASG-Architecture.png\" alt=\"asg architechture\" title=\"asg architecture\"></p>\n<p>We have 2 isntances :</p>\n<ol>\n<li>for Reading Note</li>\n<li>for Writting Note</li>\n</ol>\n<p><strong>Steps</strong> :</p>\n<ol>\n<li>\n<p>edit the etc/crontab file (automating windows tasks) :</p>\n<ol>\n<li>Scan EC2 and copy to S3 every changes (sync to S3 with a perfect copy -> <code>sync --delete EC2from s3to</code>) </li>\n<li>send the image to the cloundfront ass well  (sync to S3 with a perfect copy -> <code>sync --delete EC2from s3to</code>) </li>\n<li>trigger the update task of crontab : <code>restart cron.service</code></li>\n</ol>\n</li>\n<li>\n<p>Create an Autoscaling group</p>\n<ol>\n<li>Create a launch configuration with IAM role of S3 access</li>\n<li>Create the Autoscaling group with IAM S3 access</li>\n<li>Edit the target group from the Autoscaling group</li>\n</ol>\n</li>\n<li>\n<p>edit the etc/crontab file (automating windows tasks) :\n1. Scan S3 and load every changes (sync to EC2 with a perfect copy -> <code>sync --load s3from EC2to</code>) </p>\n<ol start=\"2\">\n<li>trigger the update task of crontab : <code>restart cron.service</code></li>\n</ol>\n</li>\n</ol>\n<h2>7. Building a Fault Tolerant WP - Cleaning Up</h2>\n<p>By doing a reboot you can force a failover from one AZ to another AZ</p>\n<p><strong>Steps</strong></p>\n<ol>\n<li>Reboot</li>\n<li>Delete RDS database,</li>\n<li>Delete Autoscaling which trigger EC2 Instance deleting (except the reading Note)</li>\n<li>Delete your target group</li>\n<li>Delete Application Load Balancer (which you relied to your target group)</li>\n<li>Delete the EC2 instance (the Writting note)</li>\n<li>Delete the cloudfront distribution</li>\n</ol>","frontmatter":{"date":"July 31, 2020","path":"ctf/aws-formation/tp5-auto-scaling-group","title":"TP4-ASG","tags":["cloud","ec2","aws"],"categorie":"ctf","thumbnail":"/assets/alex-machado-80sv993luki-unsplash.jpg"},"fields":{"readingTime":{"text":"7 min read"}}},"file":{"childImageSharp":{"fluid":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAANABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAMEBf/EABUBAQEAAAAAAAAAAAAAAAAAAAED/9oADAMBAAIQAxAAAAGFz11jKaIn/8QAGhAAAgMBAQAAAAAAAAAAAAAAAAECERMSMf/aAAgBAQABBQLm3gxwaJTxe1i8/8QAGBEAAgMAAAAAAAAAAAAAAAAAAAECElH/2gAIAQMBAT8BUdKn/8QAFhEBAQEAAAAAAAAAAAAAAAAAABEB/9oACAECAQE/Abqv/8QAGhAAAQUBAAAAAAAAAAAAAAAAAQACEBESIv/aAAgBAQAGPwJckOjObVBtR//EABwQAQACAgMBAAAAAAAAAAAAAAEAETFBIWFxkf/aAAgBAQABPyEVB9ih2jUSxfkFQDkWX4Ju5grU/9oADAMBAAIAAwAAABDjD//EABYRAQEBAAAAAAAAAAAAAAAAAAEAIf/aAAgBAwEBPxAA1M//xAAWEQEBAQAAAAAAAAAAAAAAAAABIRD/2gAIAQIBAT8QWYTP/8QAGhABAQEAAwEAAAAAAAAAAAAAAREAIUFxwf/aAAgBAQABPxAYkFlcFzRnCFCH3zCCggiKI5gk6hL1lpwEhs0zEABv/9k=","aspectRatio":1.4970059880239521,"src":"/static/11cdcb302d032fa2db3a46428a8f74c9/a7715/alex-machado-80sv993luki-unsplash.jpg","srcSet":"/static/11cdcb302d032fa2db3a46428a8f74c9/8f7df/alex-machado-80sv993luki-unsplash.jpg 250w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/0f3a1/alex-machado-80sv993luki-unsplash.jpg 500w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/a7715/alex-machado-80sv993luki-unsplash.jpg 1000w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/37d86/alex-machado-80sv993luki-unsplash.jpg 1500w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/a41d1/alex-machado-80sv993luki-unsplash.jpg 2000w,\n/static/11cdcb302d032fa2db3a46428a8f74c9/6a059/alex-machado-80sv993luki-unsplash.jpg 6000w","sizes":"(max-width: 1000px) 100vw, 1000px"}}}},"pageContext":{"slug":"ctf/aws-formation/tp5-auto-scaling-group","featuredImage":"alex-machado-80sv993luki-unsplash.jpg"}}}