Let's look at how we can run our microservices on AWS.
We're going to use Amazon ECS to run our Kubernetes cluster as well as exposing an external load balancer
so we can access our Nestjs app.
We're also going to set up a build pipeline so that our Docker images will be rebuilt and pushed to
an AWS image repository whenever there is a new commit to our GitHub repository.
So to get started, head to aws.amazon.com and you can click on log back in from here.
You can sign in if you have an account or if you'd like to create a new account, follow through with
these steps.
So signing up for AWS will be free.
However, actually running the ECS cluster will cost up to $0.10 per hour to run the cluster.
And we're also going to be charged for the underlying compute that we use.
So the instances that we're using, we're going to use two micros in this tutorial, which is a free
instance type.
So we can set this cluster up really cheaply and explore with it.
However, you will need to associate billing details with your account.
All right.
So after you've finished signing up, you should find that you're in this AWS management console.
We're going to start off by setting up an image repository so we have a place where we can push up our
built Docker images to let's go ahead and search for ECR, which stands for Elastic Container Registry,
which as we can see, is a fully managed Docker container registry.
Now that we're at ECR, let's click Get Started to create our repositories.
So we're going to go ahead and create private repositories so that only we and our Kubernetes cluster
can actually pull these images.
We're going to create a repository for each one of our microservices.
So let's go ahead and start off with the reservations Microservice and click Create Repository.
Let's go ahead and repeat the same process for our three other microservices.
The auth microservice.
Notifications.
And lastly, I'll create a payments microservice.
So now we have repositories to push our Docker images to.
Let's go ahead and give this a try locally on the command line to make sure we can correctly authenticate
and push our images up.
All right, so we're going to need the AWS CLI installed on our system to easily interact with the AWS
console from the command line.
So if you don't already have the CLI, go to Aws.amazon.com, slash CLI and then we can click on getting
started here.
We can see how to install the CLI based on your operating system.
So click on your operating system and follow these instructions to easily install the AWS CLI.
So after you've installed the AWS CLI, you should be able to run AWS configure to set up new credentials
to get access to these credentials.
Go back to the UI console and click on your username in the top right corner and then click security
credentials from here.
We'll scroll down until we find access keys and we'll click Create Access Key.
We'll go ahead and continue using a root access key in case you'd like to create a user with finer grained
control over the permissions they have, you can create a new user in IAM, sign in to that user and
then go back to create access key just for that user.
In our case, we want our root user to be able to do anything with this access key because we are using
it ourselves.
So we'll go ahead and grab the access key and paste this in to the CLI.
Then we'll copy the secret access key and paste that in as well.
You can feel free to select whatever default region you'd like.
I'm going to use us East one and we've gone ahead and correctly configured AWS CLI.
Now let's go back to ECR and we can click on any one of these repositories and in the top right we can
click on View Push commands to get a list of commands to actually build and push our image up.
So let's copy this first command, which is going to authenticate our CLI with ECR by calling AWS CLI
and then piping that to Docker login.
So of course make sure you have Docker running on your system before we execute this and then I'll paste
this command in to go ahead and log in.
Now after we've logged in, we'll go ahead and CD into our apps folder and then CD into reservations.
We can copy the next command to build the Docker image and paste it in.
Now we'll make a modification.
We're going to specify that the path to the Docker file is in our current directory, but we want this
to be run from the root context, from the root of our project.
All right.
So once our image has finished building, we'll go ahead and then get this command to tag our image
based off of our repository name so we can copy and paste this command directly.
And finally, we'll run this last command, which will just push the Docker image up to our AWS ECR
repository.
You can see that the push is progressing.
So once the image has fully pushed up, we can refresh the browser and we should see our reservations.
Image has pushed up with the tag of latest.
So now we have the ability to push images up to ECR.
Let's see how we can set up a CI CD pipeline to automate this process for our remaining microservices
and ensure we always have the latest code available in these Docker images.
So we're going to define a list of steps how to automatically build our Docker images and then push
them up to ECR.
In order to do this, we need to create a new file called Build Spec dot YAML and make sure this file
is in the root of our project.
Firstly, we'll go ahead and define the version we're using.
In this case it'll be 0.2 and then we can define some phases for our build.
The first one will be a pre build phase where we will execute a command section and in here we'll have
a list of commands.
So the first command we'll need is to actually log in to ECR.
We can take this command from our AWS ECR console where we click on View Push commands and copy this
first command where we get the log in key and pass it to Docker login.
So go ahead and copy this command and paste it in.
So this is all we need to do inside of the pre build phase.
Next we'll go into the actual build phase itself where we'll have another list of commands and now we're
actually going to execute the Docker commands to build and tag our images so we can copy this first
build command from ECR and paste it.
We're going to specify the path to the Docker file will be in dot slash apps, slash reservations,
slash Docker file and the build context or where we want to build the Docker file from will be the current
root directory.
So next we can go ahead and copy the command to tag the reservations image and paste it in.
Notice we're using the latest image tag, so let's go ahead and copy these two commands and then we
can go ahead and just paste them in a few more times.
And we're only going to have to change the service that we're building.
So we're going to build the auth service and tag the auth service at TAG latest.
We'll go ahead and do the same thing now for the payment service, make sure we go into slash payments
and make sure we tag the payments image correctly.
Finally, we will tag the notifications image.
So now that we've built and tagged our images, we're ready to actually push them up.
And we'll do this in the final stage of the build.
This will be the post build stage where we'll specify a new set of commands to actually push the Docker
images to our repository.
So we'll take the last command of Docker push and then we'll paste this in a few more times, just swapping
out the name of the image to auth.
Payments.
And notifications.
So now we have a completed build spec that will give Codebuild the set of instructions it needs to properly
build, tag and push our images.
So finally, don't forget to actually commit your change so that it will be available when code builds,
scans our GitHub repository.
So I'll commit a message saying add build spec.
And push this to my repository.
So we're ready to utilize code build in that build spec to actually set up our automated builds.
We're going to use code pipeline to accomplish this, which will allow us to easily detect new commits
and build them with our build spec.
So look up code pipeline in the search bar and click on Code pipeline.
We want to create a new pipeline.
So click create pipeline and call it whatever you'd like.
I'll call this sleeper.
Next, we can select a service role.
So click on Create new Service Role and use the default name provided and click next.
Next, we're going to go ahead and specify the source provider for our code.
So in our case, this is going to be GitHub version two, you can select which source provider you use.
However, we're going to use GitHub.
So in order to set up our GitHub provider, we need to create a new connection.
So go ahead and click connect to GitHub to set up a new GitHub connection.
You can call this whatever you would like.
I will call this the name of our app Sleeper and click connect to GitHub.
Next we need to set up the GitHub app.
So click on install a new app to be redirected to GitHub and supply your credentials.
You should be brought to this page where you install an AWS connector for GitHub.
Go ahead and select the repository for your application.
In this case it will be my sleeper repository and go ahead and click install.
After the installation you should have a GitHub app prefilled as form and then we can click on Connect.
We can see that our GitHub connection is ready to use and we can select the repository from this dropdown.
We can also select the name of the branch that we'd like to build from and watch commits.
In this case I'm going to use the main branch and we will keep this box checked that will start a new
pipeline and source code changes.
Finally, for output artifact format, click on code pipeline default and then click next.
Now we have to set up our build stage and we're going to select AWS Codebuild as our provider.
And then we need to select a project in Codebuild to use for this pipeline.
In our case, we don't have one yet, so we'll click on the Create Project button and get redirected
to code build.
So now we're setting up a new Codebuild project and we can call this sleeper as well.
Then scroll down until you find the environment image.
We will use an Amazon Linux two for our code build operating system and for the runtimes we will select
standard and the image we will use AWS code build 86 by 64 standard 4.0.
Then click on the privilege box so that we can build Docker images and create a new service role for
this code build.
Then if we scroll down, we can see the build spec section where we specify the Buildspec.yml.
However, we can actually leave this blank because by default it will look in the root for a buildspec.yml
which we have already provided so we can continue down to the bottom and click continue to code pipeline.
You can see we have successfully created the project and code build and now we can click on next back
in Code Pipeline.
Then for the deploy stage, we are going to click on Skip, deploy stage and then click Skip.
We can see all of these settings we have configured and we will click on Create Pipeline.
Now we can see we are brought to the dashboard where we can see our source has successfully executed
and our build stage is currently in progress.
We can click on the details link and then we're brought to the logs for the current build.
In code build.
You can see on the left side here we are under the code build section.
This is our in-progress build and we can click on tail logs to follow along with this build.
It's currently in the pre build section where it's logging in to Docker and you can see an error has
been thrown because the assumed role is not authorized to perform get authorization token on resource.
This is because our code build service role we've created has not been given permissions to actually
interact with ECR.
Let's go ahead and fix this by going to IAM service console and clicking on IAM and we can see our code
build sleeper service role, click on this role and then click on add permissions so we can add some
new policies to this user.
And then we're going to go ahead and filter out for ECR.
We can see a prebuilt policy here called EC2 instance Profile for Image Builder, ECR container builds.
And if we open this up, we can see a list of permissions that allow this role to actually push images
up to ECR, including that get authorization token permission.
So let's go ahead and click on this permission policy and then click on Add Permissions.
Now you can see this Codebuild role has the ECR permissions it needs, and we can go back to codepipeline
to re trigger a new release.
To trigger this release, we'll click on our pipeline and then click on Release Change and then click
on release.
You can see a new build is in progress and we can click details and tail logs to follow along with this
new build.
After the build has finished completing and all of our images are pushed up to ECR, we can close this
and see that the build was successful.
And if we go back to Codepipeline now into our sleeper pipeline, we can see that the source and build
have both succeeded.
More importantly, if we go back to ECR now.
All of our services have a latest image pushed up to them.
This latest tag has been pushed up from Codebuild.
So now we have our images automatically build any time we make a new git commit and push that up to
our repository, which is great.
Next, let's look at how we can run our Kubernetes deployment on Amazon, ECS and provision a load balancer
so we can access this cluster externally.