The course is part of this learning path
This course in intended to anyone interested in getting started with the tools that AWS provides for developers, we will explore all the services in the Developer Tools category, including CodeCommit, CodeDeploy and CodePipeline, by covering the basics for each service. By the end of this course you should be able to describe all these services and work with them.
In order to follow up with the examples showed in this course you should have some general AWS knowledge, it includes be familiar with all the core services of AWS, specially EC2 and IAM, also understand how to use and configure the AWS CLI. If you don't have this knowledge we recommend that you step back and take at least the AWS Compute Fundamentals and the AWS Security Fundamentals course prior this one.
You can find a detailed guide to follow up the examples in this course along with the source code for the applications that we will deploy in this Github repository.
If you have thoughts or suggestions for this course, please contact Cloud Academy at firstname.lastname@example.org.
Hi. In this lecture, we will talk about CodeDeploy. CodeDeploy is a deployment service and it works anywhere. You basically need to install an agent on the machine so it will work in this instance, on Primus, and also, we could make it work on other cloud providers. CodeDeploy works with Windows machines and Linux machines.
So a few key concepts, before we dive into CodeDeploy. Application, as the name suggests, is an application. And everything related to this application will live inside a CodeDeploy application. So in application, we have a revision, a deployment group, and an AppSpec file. A revision is basically a source code plus an AppSpec file. And the revision can leave either on S3 or GitHub. A deployment group is a set of an instance. You can use a deployment group, for example, to separate your production instance from your development instance. And an AppSpec file is a file that lives inside of a revision and it contains the specifications on how to deploy your software. So CodeDeploy will read this AppSpec file every time you ask for deployment.
So let's talk a little bit more about the AppSpec file. AppSpec file is short for 'application specification file'. It comes in a YAML format. And you can map files in it. You can see, for example in this image, that your mapping an index file to the root of the Apache server. You can also set permissions for your files. In this example, we are defining three scripts that are running with the root account. You can also notice that we have a few key words in here. Before install an application, we stop. This is related to the deployment workflow. You can see that CodeDeploy follows a workflow to make each deployment. In the yellow boxes, are the times where you can run scripts. So you can see that in our case, we are running a couple of scripts before install. And we are also running scripts on the application-stopped process. By having a workflow, it's easy for you to identify when CodeDeploy will execute your scripts. So in a nutshell, this is how CodeDeploy works when you ask for a new deployment.
First, CodeDeploy will look for tags in your instance-to-instance. Then it will restart the deploy process. The instances have an agent running on them. It's the CodeDeploy agent. Actually, this is why you can use CodeDeploy either on Primus or AWS. The CodeDeploy agent will download the revision through the GitHub repository then it will process the deployment and send the results back to the CodeDeploy service. By having an overview on how CodeDeploy works, we can guess how it will be the setup.
First, on step one, we would have to define a few tags for our instance to group them in a deployment group. And also, we would have to define a service role. Without the service role, CodeDeploy wouldn't be able to read the text from the instance. Then we would have to install an agent in our instance, unless we would have to launch our instance with an instance role. Without a role, the instance would not be able to download the revision from our S3 Bucket. CodeDeploy is free for EC2 deployments and it costs two cents per on-premises instance update. Let's now get our hands dirty with CodeDeploy.
Let's now deploy the CodeDeploy simple application. Click on 'get started now'. And in here, let's select 'simple deployment'. We want to deploy it on Amazon Linux. I have here, a 'key pair' already created. And here, we have to define some tags to our deployment group, so let's change it. Right now, I don't have any EC2 instances running on this account. I will take the fastest road in clicking here to launch my instance. By clicking here, we are launching a cloud formation template. This template will create our instance profile, meaning our instance role, will create a service role to our CodeDeploy service. And it will also install the agent on the EC2 instance. So basically, we don't have to do anything. So let's wait until it ends and I will get back to the next step.
Now it's time to move forward. In here, we will define our application name. So I'll simply call it, 'my app'. Next, here we can see that our revision file is located on S3. We could download the sample bundle but I would rather just click on 'next'. Here we can define our deployment group name. I would call it 'dev'. And we can see here that the tags that we just defined are right in here. So, we can click 'next'. Here, we need to specify the service role. This role was created with the cloud formation template that we've run. Click on 'next'. And here it's time to define our deployment configuration. This is basically how CodeDeploy will deploy our software. It's self-explanatory. We can deploy our software to one at a time, meaning that it will deploy the app to one instance at a time. Half at a time, and all at once. I'll select the last one. Click 'next'. In here, we can see a review of our configuration. I would just click 'deploy now'.
All right, we did it. Our deployment is completed. Let's click here to view our instance. And in here, we can click to see the events. We can see that the service was validated. We can see the steps that CodeDeploy took. And, that all the steps were succeeded. Back to our instance. Let me select and see the IP address. So I can show you that the app was deployed.
Okay. Congratulations. Our app was deployed. Okay, so we have a few more screens to explore. If we click here on 'applications' and select 'my app', we can see that we have only one deployment group. We can see our last deployment. We could deploy this revision again either on the same deployment group or in another one. But let's do the following. I would like to deploy new version of our software, so let's do this. But this time let's use the AWS command line interface. So I have the load in here, the simple app. You can see the files in here. You can see that we have the AppSpec YAML file, a simple index HTML page and our script. And if we look at the scripts page, these are the scripts that are present in the AppSpec file. So now, let's deploy a new version of our application by using the AWS command line interface. I have in here, my machine already configured with the command line interface. Let's now deploy a new revision of our software.
CodeDeploy has an awesome way to do this. Since we have to upload our bundle to either S3 or GitHub, it would be painful to do it manually. Imagine that you have to zip your files, upload the zip file to S3, and only then you start a new deployment. Well, AWS has a better way to do this. Let's take a look.
So what I'm doing here, I'm telling the CodeDeploy to push a new revision to my application. I'm specifying my application name. I'm specifying an S3 location, where i'd like to save my zip file. And I'm specifying this source location of my revision. In this case, it's the current folder. Hit 'enter'. And AWS will return to us, a comment. This comment is the one that will start the deployment. You can notice that AWS has done almost the entire job for us. Let me copy it then paste. I'll put a simple description. Deployment configuration, remember we can specify one at a time, half a time, and all at once. So let me specify again, all at once.
And now we have to specify our deployment group name. In our case, it's 'dev'. And everything else is already configured. Let's hit 'enter' and wait for the return. Sorry, I have a typo in here. It's 'default', not 'defaults'. Okay, now we have a deployment ID. We can copy this ID. And, we could check in the AWS command line interface, what is the status of this deployment.
And we can see here that the deployment was already succeeded so let's go again to our instance and hit 'refresh'. Our app was deployed again. That's it for AWS CodeDeploy.
About the Author
Eric Magalhães has a strong background as a Systems Engineer for both Windows and Linux systems and, currently, work as a DevOps Consultant for Embratel. Lazy by nature, he is passionate about automation and anything that can make his job painless, thus his interest in topics like coding, configuration management, containers, CI/CD and cloud computing went from a hobby to an obsession. Currently, he holds multiple AWS certifications and, as a DevOps Consultant, helps clients to understand and implement the DevOps culture in their environments, besides that, he play a key role in the company developing pieces of automation using tools such as Ansible, Chef, Packer, Jenkins and Docker.