{"id":2188,"date":"2022-07-13T03:08:48","date_gmt":"2022-07-13T03:08:48","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2188"},"modified":"2023-08-03T06:47:28","modified_gmt":"2023-08-03T06:47:28","slug":"ec2-using-cloud-formation","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/ec2-using-cloud-formation","title":{"rendered":"How to Launch an EC2 instance using Cloud Formation?"},"content":{"rendered":"<h3>Cloud Formation<\/h3>\n<p><a href=\"https:\/\/www.checkmateq.com\/aws-cloud\">AWS Cloud<\/a> Formation is a Infrastructure as a Code tool, it means that you can create your <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud<\/a> infrastructure with a code\/syntax using cloud formation tool , you can use either json or yml templets where you declare which resource\u00a0 or service you want to deploy , cloud formation will use these templets and will create the declared resources on the cloud ,you can\u00a0 reuse this templets to deploy same<a href=\"https:\/\/www.checkmateq.com\/infrastructure-services\"> infrastructure<\/a> or make changes\/upgrade\u00a0 to already deployed one&#8217;s , In this blog we will learn how to launch a EC2 instance using cloud formation with a yaml templete.<\/p>\n<p>Create a yaml file, something like server.yaml , server is the name of the yaml file I am creating , copy the below yaml syntax and save it, let&#8217;s understand it by going through each line<\/p>\n<p>&nbsp;<\/p>\n<p><strong>server.yaml<\/strong><\/p>\n<pre>1  AWSTemplateFormatVersion: '2010-09-09'\r\n\r\n2  Resources:\r\n3   EC2Instance:\r\n4     Type: AWS::EC2::Instance\r\n5     Properties:\r\n6       InstanceType: <strong>t2.micro<\/strong>\r\n7       ImageId: \"<strong>ami-0186e3fec9b0283ee<\/strong>\"\r\n8       SecurityGroups:\r\n9         - !Ref InstanceSecurityGroup\r\n\r\n10   InstanceSecurityGroup:\r\n11     Type: AWS::EC2::SecurityGroup\r\n12     Properties:\r\n13       GroupDescription: Allow traffic on port 22(SSH) \r\n14       SecurityGroupIngress:\r\n15         - IpProtocol: tcp\r\n16           FromPort: 22\r\n17           ToPort: 22\r\n18           CidrIp: 0.0.0.0\/0\r\n<\/pre>\n<div><strong>Line-1:\u00a0AWSTemplateFormatVersion: &#8216;2010-09-09&#8217;<\/strong><\/div>\n<div>here 2010-09-09 is the latest templet format version available, it indicates the capabilities of the templet, this is the only valid version available.<\/div>\n<div><\/div>\n<div><strong>Line-2:<\/strong> Resources<\/div>\n<div>you will declare AWS resources to be deployed and their configuration such as ec2 instance, s3 bucket, <a href=\"https:\/\/www.checkmateq.com\/kubernetes\">Kubernetes<\/a> cluster<\/div>\n<p>&nbsp;<\/p>\n<div><strong>Line-3: EC2Instance<\/strong> it&#8217;s just a name that I gave for my reference or documentation and referring to it in the same yaml file, this will be reflected as a tag for the resource created, tags are useful in managing and searching your resources.<\/div>\n<p>&nbsp;<\/p>\n<div><strong>Line-4: Type: AWS::EC2::Instance<\/strong><\/div>\n<div>&#8220;Type&#8221; is the key word used to specify the resource type (like ec2 instance, load balancer, security group) that you want to deploy and &#8220;AWS::EC2::Instance&#8221; is the key word for ec2 instance, each resource will have a unique keyword you can get these keywords from AWS cloud formation\u00a0 documentation.<\/div>\n<div class=\"inner-form\">\n<h2>Connect with our technical consultants<\/h2>\n<div role=\"form\" class=\"wpcf7\" id=\"wpcf7-f2262-o1\" lang=\"en-US\" dir=\"ltr\">\n<div class=\"screen-reader-response\"><p role=\"status\" aria-live=\"polite\" aria-atomic=\"true\"><\/p> <ul><\/ul><\/div>\n<form action=\"\/blog\/wp-json\/wp\/v2\/posts\/2188#wpcf7-f2262-o1\" method=\"post\" class=\"wpcf7-form init\" novalidate=\"novalidate\" data-status=\"init\">\n<div style=\"display: none;\">\n<input type=\"hidden\" name=\"_wpcf7\" value=\"2262\" \/>\n<input type=\"hidden\" name=\"_wpcf7_version\" value=\"5.5.6.1\" \/>\n<input type=\"hidden\" name=\"_wpcf7_locale\" value=\"en_US\" \/>\n<input type=\"hidden\" name=\"_wpcf7_unit_tag\" value=\"wpcf7-f2262-o1\" \/>\n<input type=\"hidden\" name=\"_wpcf7_container_post\" value=\"0\" \/>\n<input type=\"hidden\" name=\"_wpcf7_posted_data_hash\" value=\"\" \/>\n<\/div>\n<p><span class=\"wpcf7-form-control-wrap Name\"><input type=\"text\" name=\"Name\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Full Name\" \/><\/span><span class=\"wpcf7-form-control-wrap phone\"><input type=\"text\" name=\"phone\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Phone Number\" \/><\/span><br \/>\n<span class=\"wpcf7-form-control-wrap Email\"><input type=\"text\" name=\"Email\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Business Email\" \/><\/span><span class=\"wpcf7-form-control-wrap Company\"><input type=\"text\" name=\"Company\" value=\"\" size=\"40\" class=\"wpcf7-form-control wpcf7-text wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Company Name\" \/><\/span><br \/>\n<span class=\"wpcf7-form-control-wrap ProjectRequirement\"><textarea name=\"ProjectRequirement\" cols=\"40\" rows=\"10\" class=\"wpcf7-form-control wpcf7-textarea wpcf7-validates-as-required\" aria-required=\"true\" aria-invalid=\"false\" placeholder=\"Project Requirement\"><\/textarea><\/span><br \/>\n<div id=\"cf7sr-6a0903f8b98e6\" class=\"cf7sr-g-recaptcha\" data-sitekey=\"6LenTB4gAAAAAICROHGQZqlqvWO-W5jpf5TyMfPt\"><\/div><span class=\"wpcf7-form-control-wrap cf7sr-recaptcha\"><input type=\"hidden\" name=\"cf7sr-recaptcha\" value=\"\" class=\"wpcf7-form-control\"><\/span><br \/>\n<input type=\"submit\" value=\"Submit\" class=\"wpcf7-form-control has-spinner wpcf7-submit\" \/><\/p>\n<div class=\"wpcf7-response-output\" aria-hidden=\"true\"><\/div><\/form><\/div>\n<\/div>\n<div><strong>Line 5-7: Properties<\/strong> , here you specify the configuration of resource to be deployed , as we are creating an ec2 instance properties like InstanceType and AMI ID(ImageId) are used , I am\u00a0 using instance type t2.micro which will have 1 vCPUs , 1 GiB Memory\u00a0 and\u00a0 ImageId of Red Hat Enterprise Linux(ami-0186e3fec9b0283ee).<\/div>\n<p>&nbsp;<\/p>\n<div><strong>Line 8,9:\u00a0\u00a0<\/strong>if you want to use existing security group give the security group id after the key word <strong>SecurityGroups ,\u00a0<\/strong>but here we are creating a new security group\u00a0 (from line 10-18) and and giving it&#8217;s details using <strong>Ref\u00a0<\/strong>function , this function returns the value of the specified parameter or resource.<\/div>\n<p>&nbsp;<\/p>\n<div>you may be confused here that how a security group is attached after creating instance as we declared instance details first and then the security group creation details in the yaml file, cloud formation will identify\u00a0 required\u00a0 dependencies and\u00a0 creates them\u00a0 first only then it will start creating the main resource, In our case CloudFormation\u00a0 will create security group first and than starts deploying ec2 instance.<\/div>\n<p>&nbsp;<\/p>\n<div>On your aws web console go to Cloud Formation and Click on create stack.<\/div>\n<div><\/div>\n<div><img loading=\"lazy\" class=\" wp-image-2192 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-300x152.jpeg\" alt=\"\" width=\"816\" height=\"414\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-300x152.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-1024x519.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-768x389.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-1536x778.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1-1200x608.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-1.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/div>\n<p>&nbsp;<\/p>\n<p>Select &#8220;Template is ready &#8221; for &#8220;Prepare template&#8221; and &#8220;upload a templet file&#8221;\u00a0 under &#8220;Specify templet&#8221; , as you can see you can also use a templet stored in S3 bucket, after giving the file path if it&#8217;s in s3 bucket\/uploading\u00a0 the file if it&#8217;s in your local machine\u00a0 , click on next.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2193 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-300x155.jpeg\" alt=\"\" width=\"832\" height=\"430\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-300x155.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-1024x530.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-768x398.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-1536x795.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2-1200x621.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-2.jpeg 1893w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In <strong>step-2<\/strong> give a name to your stack , then click on next.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2195 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-300x153.jpeg\" alt=\"\" width=\"826\" height=\"421\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-3.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In <strong>Step-3<\/strong>\u00a0 give tags if required ,and for the <strong>Permissions<\/strong> you can create a IAM role and give it here if you want to restrict Cloud formation from using all the permissions of the user that you have logged in on to the web console , for example if you are using Cloud formation from a root user account and you don&#8217;t want to allow deletion of ec2 instances\u00a0 from cloud formation create a IAM role with permissions to only create\u00a0 and modify ec2 resources.<\/p>\n<p><strong>Note:\u00a0<\/strong>Cloud formation can\u00a0 modify , update or destroy resources which are created by cloud formation only.<\/p>\n<p>and for <strong>Stack failure options\u00a0<\/strong>select <strong>Roll back all stack resources\u00a0<\/strong>which means that you are instructing cloud formation to roll back all the resources created in case if it fails to create any one the resources that you have declared in the templet \/ yaml file\u00a0 and the other option is <strong>Preserve successfully provisioned resources\u00a0<\/strong> , this option will keep the resources that are created successfully\u00a0 even if fails to deploy whole stack .<\/p>\n<p>Leave remaining configuration options to default, scroll down and click on next<\/p>\n<p><img loading=\"lazy\" class=\"wp-image-2196 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-300x153.jpeg\" alt=\"\" width=\"838\" height=\"427\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-4.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In <strong>Step-4<\/strong>\u00a0 review the configuration you have made and click on &#8220;create stack&#8221; ,\u00a0 now you will be redirected to events page.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2197 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-300x153.jpeg\" alt=\"\" width=\"790\" height=\"403\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-1024x522.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-5.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Here you can see the Status of the resources deploying , as you can see in the above image even though I declared creation of security group after declaring the instance creation details in <strong>sever.yaml<\/strong> file\u00a0 cloud formation is creating the security group first and than going on to the ec2 instance which implies that cloud formation will scan your templet file and creates the resources in order even thou you do not follow any order while\u00a0 declaring them in your file.<\/p>\n<p>Now let&#8217;s go and verify\u00a0 whether deployed EC2 instance uses mentioned AMI, security group and instance type(verify the configuration with server.yaml file that we have used to deploy resources).<\/p>\n<p>below are the snapshots of EC2 instance Launched using Cloud Formation and server.yaml file.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2201 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-300x153.jpeg\" alt=\"\" width=\"761\" height=\"388\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-1024x521.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-768x391.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-1536x782.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6-1200x611.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-6.jpeg 1920w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2202 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-300x153.jpeg\" alt=\"\" width=\"766\" height=\"391\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-300x153.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-1024x523.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-768x392.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-1536x785.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7-1200x613.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/cf-7.jpeg 1900w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Please contact our team for any offshore infrastructure management services <a href=\"https:\/\/www.checkmateq.com\/contact-us\">website<\/a>, <a href=\"https:\/\/www.linkedin.com\/company\/checkmatglobal\/\">LinkedIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cloud Formation AWS Cloud Formation is a Infrastructure as a Code tool, it means that you can create your cloud infrastructure with a code\/syntax using cloud formation tool , you can use either json or yml templets where you declare which resource\u00a0 or service you want to deploy , cloud formation will use these templets &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/ec2-using-cloud-formation\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Launch an EC2 instance using Cloud Formation?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2241,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,71,70,69,68,59,11,63,23,16,24,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2188"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2188"}],"version-history":[{"count":32,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2188\/revisions"}],"predecessor-version":[{"id":4181,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2188\/revisions\/4181"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2241"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}