{"id":17833,"date":"2015-03-09T13:11:07","date_gmt":"2015-03-09T07:41:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17833"},"modified":"2015-08-03T11:15:35","modified_gmt":"2015-08-03T05:45:35","slug":"aws-code-deploy-using-s3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/aws-code-deploy-using-s3\/","title":{"rendered":"AWS CodeDeploy Using S3"},"content":{"rendered":"<p>AWS has great set of tools which helps simplify the\u00a0deployment process in their cloud and one such tool is\u00a0AWS CodeDeploy.\u00a0\u00a0In this blog, we will deploy the application using AWS CodeDeploy using S3.<\/p>\n<p style=\"text-align: left;\"><strong>Read our\u00a0blog on:\u00a0<a href=\"http:\/\/www.tothenew.com\/blog\/aws-code-deploy-a-sample-walkthrough\/\">AWS CodeDeploy \u2013 A Sample Walkthrough<\/a><\/strong><\/p>\n<p style=\"text-align: left;\">Consider a use case where you have 20 instances and you want to deploy your code or change the configuration file of these instances. The only solution would be to login into each particular instance and then changing the configuration file.\u00a0<strong>AWS CodeDeploy\u00a0<\/strong>lets you do this in just few steps . You just create a deploy application and your code will be deployed in all these 20 instances.<\/p>\n<div id=\"attachment_17834\" style=\"width: 543px\" class=\"wp-caption aligncenter\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Code-Deploy.jpg\"><img aria-describedby=\"caption-attachment-17834\" decoding=\"async\" loading=\"lazy\" class=\" wp-image-17834\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Code-Deploy.jpg\" alt=\"Deploying code without using AWS CODE DEPLOY\" width=\"533\" height=\"273\" \/><\/a><p id=\"caption-attachment-17834\" class=\"wp-caption-text\">Deploying code without using AWS\u00a0CodeDeploy<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_17835\" style=\"width: 501px\" class=\"wp-caption aligncenter\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Code-Deploy-1.jpg\"><img aria-describedby=\"caption-attachment-17835\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-17835\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Code-Deploy-1.jpg\" alt=\"Deploying code using AWS CODE DEPLOY\" width=\"491\" height=\"375\" \/><\/a><p id=\"caption-attachment-17835\" class=\"wp-caption-text\">Deploying code using AWS CodeDeploy<\/p><\/div>\n<p>There are two ways to deploy code in\u00a0Amazon Web Services:-<\/p>\n<ul>\n<li>Using\u00a0<strong>GIT\u00a0<\/strong><\/li>\n<li>Using\u00a0<strong>AWS S3 (Simple Storage Service)<\/strong><\/li>\n<\/ul>\n<p>Here, we will deploy the code using Amazon S3 service. Let us also understand few\u00a0useful terms which will be used in the deployment process:<\/p>\n<ol>\n<li><strong>AppSpec file:-\u00a0<\/strong>It is an Application Specification file. It is a unique file that defines a series of deployment actions that you want CodeDeploy to execute.<\/li>\n<li><strong>Deployment Application:-\u00a0<\/strong>The unique name which will be given to your Deployment Application.<\/li>\n<li><strong>Revision:-\u00a0<\/strong>It is a combination of AppSpec file and other files such as scripts, images, index files, media etc.<\/li>\n<li><strong>Deployment Group:-\u00a0<\/strong>It is defined as a group of individual instances and auto-scaled instances.<\/li>\n<li><strong>Deployment Configuration-\u00a0<\/strong>It lets you side that how you want your code to be deployed:- one at a time\/ half at a time\/ all at once.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Deploying Code Using AWS S3<\/strong><\/span><\/p>\n<p>We&#8217;ll take a simple example to deploy the code using S3. We are deploying the code in a single instance and are launching a single\u00a0<strong>t2.micro\u00a0<\/strong>instance. <a href=\"http:\/\/www.tothenew.com\/blog\/route-53-setting-up-private-hosted-zones-phz\/\">Launch the instance and install nginx<\/a> in it as we are going to change the front page or index.html of the nginx default configuration. You can install nginx by logging into the instance and typing the following commands: sudo apt-get update sudo apt-get install nginx -y<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\"><span style=\"text-decoration: underline;\"><em><strong>Now lets move towards Code Deploy into an instance<\/strong><\/em><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p>Before starting with CodeDeploy, we need to have :-<\/p>\n<ul>\n<li>Two <strong>I<\/strong><strong>AM ROLES<\/strong> : one role will be given to <em>EC2-instances to access s3 buckets<\/em> and the other role is given to <em>CodeDeploy service to choose Ec2-instances based on their tags.<\/em><\/li>\n<\/ul>\n<ul>\n<li>One<strong> S3 bucket<\/strong> containing the appspec file, scripts and other files into a tar,gz or bz2 file (compressed format file). You need to store the compressed file into the S3 bucket. The files will automatically be compressed at the time of Deployment.<\/li>\n<\/ul>\n<h3 style=\"text-align: justify; color: #ff9900;\"><\/h3>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">IAM Role Given to AWS CodeDeploy to access your EC2-instance<\/h3>\n<p>&nbsp;<\/p>\n<div id=\"crayon-54fafd12bc7dd377388229-1\" class=\"crayon-line\">\n<p>[js]<br \/>\n{<br \/>\n&quot;Version&quot;: &quot;2012-10-17&quot;,<br \/>\n &quot;Statement&quot;: [<br \/>\n    {<br \/>\n      &quot;Action&quot;: [<br \/>\n         &quot;autoscaling:PutLifecycleHook&quot;,<br \/>\n         &quot;autoscaling:DeleteLifecycleHook&quot;,<br \/>\n         &quot;autoscaling:RecordLifecycleActionHeartbeat&quot;,<br \/>\n         &quot;autoscaling:CompleteLifecycleAction&quot;,<br \/>\n         &quot;autoscaling:DescribeAutoscalingGroups&quot;,<br \/>\n         &quot;autoscaling:PutInstanceInStandby&quot;,<br \/>\n         &quot;autoscaling:PutInstanceInService&quot;,<br \/>\n         &quot;ec2:Describe*&quot;<br \/>\n                 ],<br \/>\n  &quot;Effect&quot;: &quot;Allow&quot;,<br \/>\n  &quot;Resource&quot;: &quot;*&quot;<br \/>\n    }<br \/>\n               ]<br \/>\n }<br \/>\n[\/js]<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">IAM Role Given to EC2-instances to access S3 Buckets<\/h3>\n<p>[js]<\/p>\n<p>{<br \/>\n&quot;Version&quot;: &quot;2012-10-17&quot;,<br \/>\n&quot;Statement&quot;:<br \/>\n     [<br \/>\n         {<br \/>\n             &quot;Action&quot;:<br \/>\n                   [<br \/>\n                     &quot;s3:Get*&quot;,<br \/>\n                     &quot;s3:List*&quot;<br \/>\n                   ],<br \/>\n             &quot;Effect&quot;: &quot;Allow&quot;,<br \/>\n             &quot;Resource&quot;: &quot;*&quot;<br \/>\n         }<br \/>\n     ]<br \/>\n}<\/p>\n<p>[\/js]<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Trusted Relationship With AWS CodeDeploy IAM Role<\/h3>\n<p>[js]<\/p>\n<p>{<br \/>\n\u201cVersion\u201d: \u201c2012-10-17\u2033,<br \/>\n\u201cStatement\u201d: [<br \/>\n    {<br \/>\n    \u201cSid\u201d: \u201c\u201d,<br \/>\n    \u201cEffect\u201d: \u201cAllow\u201d,<br \/>\n    \u201cPrincipal\u201d: {<br \/>\n    \u201cService\u201d:<br \/>\n           [<br \/>\n        &quot;codedeploy.us-east-1.amazonaws.com&quot;,<br \/>\n        &quot;codedeploy.us-west-2.amazonaws.com&quot;<br \/>\n           ]<br \/>\n                 },<br \/>\n    \u201cAction\u201d: \u201csts:AssumeRole\u201d<br \/>\n    }<br \/>\n             ]<br \/>\n}<\/p>\n<p>[\/js]<\/p>\n<p>We also need to install <strong>AWS CodeDeploy Client <\/strong>to our instance. It will allow the code to be deployed into the instance. You can install the code-deploy client onto your instance by the following process:<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Installing AWS CLI and AWS CodeDeploy Agent on Ubuntu 14.04 LTS :<\/h3>\n<p>[js]<\/p>\n<p>sudo apt-get update<br \/>\nsudo apt-get install awscli<br \/>\nsudo apt-get install ruby2.0<br \/>\ncd \/home\/ubuntu<br \/>\nsudo aws s3 cp s3:\/\/aws-codedeploy-us-east-1\/latest\/install . &#8211;region us-east-1<br \/>\nsudo chmod +x .\/install<br \/>\nsudo .\/install auto<\/p>\n<p>[\/js]<\/p>\n<h3><span style=\"text-decoration: underline;\"><strong>Understanding APPSPEC FILE<\/strong><\/span><\/h3>\n<p>&nbsp;<\/p>\n<p>AppSpec is the heart of CodeDeploy and is written in YAML. AppSpec defines how the application code will be deployed on deployment targets and which\u00a0deployment lifecycle event hooks to run in response to various deployment lifecycle events. It should be in the root of an application source code\u2019s directory structure.<\/p>\n<p>High Level Structure of AppSpec File:<\/p>\n<div id=\"crayon-54fafd12bc800927193471\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\">\n<div class=\"crayon-plain-wrap\"><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\" style=\"height: 107px;\" width=\"429\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" style=\"text-align: center;\">1<\/div>\n<div class=\"crayon-num crayon-striped-num\" style=\"text-align: center;\">2<\/div>\n<div class=\"crayon-num\" style=\"text-align: center;\">3<\/div>\n<div class=\"crayon-num crayon-striped-num\" style=\"text-align: center;\">4<\/div>\n<div class=\"crayon-num\" style=\"text-align: center;\">5<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\" style=\"text-align: center;\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-54fafd12bc800927193471-1\" class=\"crayon-line\"><span class=\"crayon-v\">version<\/span><span class=\"crayon-o\">:<\/span> <span class=\"crayon-cn\">0.0<\/span><\/div>\n<div id=\"crayon-54fafd12bc800927193471-2\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-v\">os<\/span><span class=\"crayon-o\">:<\/span> <span class=\"crayon-v\">operating<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">system<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-e\">name<\/span><\/div>\n<div id=\"crayon-54fafd12bc800927193471-3\" class=\"crayon-line\"><span class=\"crayon-v\">files<\/span><span class=\"crayon-o\">:<\/span> <span class=\"crayon-v\">source<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">destination<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">files<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-e\">mappings<\/span><\/div>\n<div id=\"crayon-54fafd12bc800927193471-4\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-v\">permissions<\/span><span class=\"crayon-o\">:<\/span> <span class=\"crayon-v\">permissions<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-e\">specifications<\/span><\/div>\n<div id=\"crayon-54fafd12bc800927193471-5\" class=\"crayon-line\"><span class=\"crayon-v\">hooks<\/span><span class=\"crayon-o\">:<\/span> <span class=\"crayon-v\">deployment<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">lifecycle<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">event<\/span><span class=\"crayon-o\">&#8211;<\/span><span class=\"crayon-v\">mappings<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>Hooks:<\/strong>\u00a0scripts to run at specific deployment lifecycle events during the deployment. The available event hooks are:<\/p>\n<ul>\n<li><strong>ApplicationStop:\u00a0<\/strong>events to be performed when application is stopped<\/li>\n<li><strong>DownloadBundle:<\/strong>\u00a0occurs when CodeDeploy agent downloads bundle from S3 bucket<\/li>\n<li><strong>BeforeInstall:<\/strong>\u00a0occurs before AWSCodeDeploy starts deployment of application code to deployment target<\/li>\n<li><strong>Install:<\/strong>\u00a0AWSCodeDeploy copies files to deployment targets<\/li>\n<li><strong>AfterInstall:<\/strong>\u00a0occurs once files are copied and installed to deployment targets<\/li>\n<li><strong>ApplicationStart:<\/strong>\u00a0occurs just before your application revision is started on the deployment target<\/li>\n<li><strong>ValidateService:\u00a0<\/strong>occurs after the service has been validated<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>The sample <span style=\"color: #000000;\"><b>AppSpec file\u00a0<\/b>used is as shown below:<\/span><\/p>\n<p>[js]<\/p>\n<p>version: 0.0<br \/>\nos: linux<br \/>\nfiles:<br \/>\n&#8211; source: \/<br \/>\ndestination: \/usr\/share\/nginx\/html<br \/>\nhooks:<br \/>\nBeforeInstall:<br \/>\n&#8211; location: scripts\/install_dependencies.sh<br \/>\ntimeout: 300<br \/>\nrunas: root<br \/>\nAfterInstall:<br \/>\n&#8211; location: scripts\/afterinstall<br \/>\ntimeout: 300<br \/>\nrunas: root<br \/>\nApplicationStart:<br \/>\n&#8211; location: scripts\/start_server<br \/>\ntimeout: 300<br \/>\nrunas: root<br \/>\nApplicationStop:<br \/>\n&#8211; location: scripts\/stop_server<br \/>\ntimeout: 300<br \/>\nrunas: root<\/p>\n<p>[\/js]<\/p>\n<p>While creating an instance you need to attach the s3 bucket role with your instance and after that you need to install AWS CLI and AWS Code Deploy Agent using the above procedure. Now you are ready to create the CodeDeploy Application.<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Creating AWS CodeDeploy Application<\/h3>\n<p>Sign in to the AWS Console . Go to the services and click on &#8220;<strong>Code Deploy&#8221;\u00a0<\/strong> as shown below.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_005.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17836\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_005.png\" alt=\"Selection_005\" width=\"1075\" height=\"331\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Now a new window will open as shown below. Click on &#8220;<strong>Create New Application&#8221;\u00a0<\/strong>button. It will open up the prompt to create a new application.<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_004.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17837\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_004.png\" alt=\"Selection_004\" width=\"1066\" height=\"259\" \/><\/a><\/p>\n<ul>\n<li>A new window will appear which ask about the details for creating an application. \u00a0Enter <span style=\"color: #000000;\"><b>Application Name, Application Group Name\u00a0<\/b>and\u00a0choose instances to which you want to deploy the code using the Key and Value. Choose your\u00a0<strong>Deploy Configuration :- \u00a0<\/strong>One at a time \/Half at a time \/All at a time . This configuration lets you choose how you want to deploy your code.<\/span><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div id=\"attachment_17838\" style=\"width: 1007px\" class=\"wp-caption alignnone\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_006.png\"><img aria-describedby=\"caption-attachment-17838\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-17838 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_006.png\" alt=\"Selection_006\" width=\"997\" height=\"342\" \/><\/a><p id=\"caption-attachment-17838\" class=\"wp-caption-text\">Enter the application name and Application Group Name<\/p><\/div>\n<div id=\"attachment_17839\" style=\"width: 899px\" class=\"wp-caption alignnone\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_007.png\"><img aria-describedby=\"caption-attachment-17839\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-17839 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_007.png\" alt=\"Selection_007\" width=\"889\" height=\"517\" \/><\/a><p id=\"caption-attachment-17839\" class=\"wp-caption-text\">Choose instances based om their Key and Value<\/p><\/div>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_008.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17840\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_008.png\" alt=\"Selection_008\" width=\"906\" height=\"298\" \/><\/a><\/p>\n<div class=\"crayon-line crayon-striped-line\">\n<ul>\n<li>Then Click on &#8220;<span style=\"color: #000000;\"><b>CREATE APPLICATION &#8221;\u00a0<\/b>button. Your application will be created and new window will appear as shown below.\u00a0<\/span><\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_010.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17841\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_010.png\" alt=\"Selection_010\" width=\"542\" height=\"423\" \/><\/a><\/p>\n<ul>\n<li>You have to create a new revision. Click on\u00a0<strong>Deploy New Revision\u00a0<\/strong>button to create a new revision.<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_013.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17843\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_013.png\" alt=\"Selection_013\" width=\"839\" height=\"608\" \/><\/a><\/p>\n<ul>\n<li>Now enter the \u00a0<strong>Application Name, Deployment\u00a0Group Name.\u00a0<\/strong>Choose\u00a0<strong>Revision type :- &#8220;<\/strong>My application is stored in Amazon S3.&#8221;. Give the Revision Location i.e. location of Bucket and the file name. (You can also copy the full path of file from AWS S3 and paste it here). After entering all the details, click on\u00a0<strong>Deploy Now.\u00a0<\/strong>Now your application and code is being deployed. Wait for few seconds and then refresh.<\/li>\n<\/ul>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_012.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17844\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_012.png\" alt=\"Selection_012\" width=\"1351\" height=\"270\" \/><\/a> \u00a0 <a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_014.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-17845\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/03\/Selection_014.png\" alt=\"Selection_014\" width=\"1309\" height=\"577\" \/><\/a><\/p>\n<ul>\n<li>The status will appear as <strong>Succeeded.\u00a0<\/strong>You can now hit the IP\u00a0of your instance and you will get the index page that you deployed.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>AWS has great set of tools which helps simplify the\u00a0deployment process in their cloud and one such tool is\u00a0AWS CodeDeploy.\u00a0\u00a0In this blog, we will deploy the application using AWS CodeDeploy using S3. Read our\u00a0blog on:\u00a0AWS CodeDeploy \u2013 A Sample Walkthrough Consider a use case where you have 20 instances and you want to deploy your [&hellip;]<\/p>\n","protected":false},"author":163,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":15},"categories":[1174],"tags":[248,1676,1242,1332],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17833"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/163"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17833"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17833\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}