Welcome back to BackSpace Academy. In this lab we're going to be extending our 2
knowledge of continuous integration and continuous delivery on AWS by 3
integrating a code pipeline with a build and test process using Mocha and we're 4
going to use Codebuild which will run the node package manager to build the 5
application. Then it will run a test using Mocha and if it passes that test 6
then it will then forward those artifacts over to the AWS Elastic 7
Beanstalk environment to be deployed. This is what it's going to look like. 8
We're going to have our pipeline and, as we've done in the past, we're going to 9
have a source repository with our code in Github. We're going to have our 10
environment as an Elastic Beanstalk environment but we're going to introduce 11
a build and test process there or build and staging process there. We're going 12
to be using Codebuild and NPM to build that and then once it's built 13
that application then it will pass it on to Mocha and that will actually test that 14
for us. If that Mocha test passes then the Elastic Beanstalk will have that 15
deployed to it. Now our deployment package that we're going to be providing 16
to Codepipeline will have some additional things in it that we'll need 17
to run that build and test process. In our buildspec.yml file 18
we're going to define additional steps in there and it's going 19
to refer to the Mocha so we're going to actually have that in our package JSON. 20
We're going to make sure that we actually set up and build Mocha. Once 21
it's done, it's going to run a test which will be located in a test.js file 22
That'll be passed over to Codepipeline and Codepipeline 23
will pass it to Codebuild which will then run NPM 24
which will install all of the packages that are defined in that package JSON 25
file. Once that has been built it will then pass it over and run Mocha. 26
Mocha will run that test.js, if that is successful then 27
AWS Codepipeline will pass it over to Elastic Beanstalk 28
and that will deploy that to that Elastic Beanstalk 29
environment. Now before we start building or creating our Elastic Beanstalk 30
environment what we need to do is go into the Backspace Academy Github and 31
fork the repository for this lab. There is a link there in the lab notes to that. 32
We just go in here first off. We can star it and then we fork it. 33
Okay so now that we've got that forked what we can do is that we can actually 34
download that zip file. Once that zip file has completed we need to actually 35
change it so that the code is not hidden in a folder. First off 36
we'll extract that. 37
Okay so that's extracted and we just need to zip that back up again. Okay so 38
there we've got that zipped for us and we can save that somewhere for easy 39
access. Let's now jump into Elastic Beanstalk 40
and create that environment. Okay so create new application. We'll 41
give that a name and create. Now we go to actions create environment for 42
this application. It's a web server environment. We'll leave the name as it 43
is. We need to select node.js and we'll upload our code. 44
We can create our environment now. 45
Okay so after 10 minutes or so we've got our Elastic Beanstalk environment up and 46
running. So before we jump into Codepipeline, what we'll do is we'll have a 47
look at some of the changes that we've made to to integrate with the build 48
and test environments. Let's have a look first at our buildsspec.yml file. 49
Just have a look at that and see what's going on there. We've got some pre-build 50
commands here, or we've got some install commands. We're going to be 51
installing Mocha. Once we've done that we're going to do NPM install. 52
It'll install all of the packages that are located in our package JSON file. 53
Then we're going to be installing unit.js to run our unit tests. Once that's 54
all done and we've got everything built, we can then run mocha test.js which 55
will run a test using Mocha that will be located in test.js file. 56
Once it is successful, then it will create all these 57
artifacts and pass these artifacts over to Elastic Beanstalk. Let's have a 58
look at the test.js. So that's our test. So this is not a lab on how to use Mocha 59
or anything like that. It's a very simple test just creating a test dot 60
string here starts with hello and then we're passing the word hello 61
back to that. It's obviously going to pass. So if that test pass which it 62
most definitely will then that will proceed to deliver that through to 63
Elastic Beanstalk. Let's jump into Codepipeline and create this code 64
pipeline. We jump into codepipeline and create our pipeline. Again we get 65
there from services codepipeline. Create pipeline. We'll give this a name. 66
We can use an existing service role that we've used before if we've got one. 67
We just use our default location for the artifact store that will be fine. 68
We'll click on next. Our source provider again is going to be in Github. We'll 69
connect to our Github repository. 70
Okay so we've done that we now need to get our repository name. We go back 71
into our Github account and then we can select the name of that repository. 72
We just copy that and paste that in and our branch will be master again. We'll 73
use the Github web hooks. Click on next. We're going to have our build 74
provider here. That is going to be AWS codebuild. We don't have a project 75
name so it's going to create a project for us. We click on create project. 76
That has now opened up another window which is going to have Codebuild there. 77
We give our Codebuild project a name. 78
We won't worry about a description for it. We're going to be using a managed 79
image. Our operating system is going to be Ubuntu it's a linux. Our run 80
time is going to be NodeJS. We'll use the latest version of that. We'll use 81
version 10 of node. We'll select new service role. 82
We leave use buildspec file as it 83
is and we will continue to codepipeline. 84
Okay so we've successfully created backspace'-eb-coidebuild in Codebuild. 85
We'll click on next and we're going to deploy now. We need 86
again to select Elastic Beanstalk. Select our application and our environment. 87
Click on next. There we go. We've got our pipeline there. It's going to 88
have our source being Github. We're going to build stage there which is 89
going to be an AWS codebuild project called backspace-eb-codebuild. 90
We're going to be deploying to Elastic Beanstalk. Let's create that pipeline. 91
Okay so the pipeline has been created and so now it will start to build and 92
test that source and hopefully it will deploy it to Elastic Beanstalk if 93
there are no errors. Ok so after about 10 minutes or so it has gone through and it 94
has built them successfully. It has run that test and it has also 95
passed that on as a deployment. So that is great. So we've successfully created a 96
full CI CD pipeline going from a Github repository all the way through to an 97
Elastic Beanstalk environment. Now one last thing to do is to clean up so that 98
we don't get a bill at the end of the month. The first thing we can do is 99
that we can edit this. Delete it and, delete. 100
That will be deleting. Successfully deleted, that's fine. We go into our 101
Elastic Beanstalk. We go into actions and 102
we can go into our actual application and we can delete our application which 103
will delete those environments as well. Go into our application, delete 104
application. Okay and if we go into this now it will see that it's terminating 105
our environment for us., So that is all complete. So we won't get a bill at the end of the 106
month. I hope you've enjoyed this lab it's been quite a good one and I'll look 107
forward to seeing you in the next one.