Code Engine - A developers dream!
Tags: code, developer, cloud, cloudnative
Published: 2020-11-23 17:50:18

Code Engine? What is Code Engine? This is a slick new tool that IBM Cloud has released into Beta. It takes your already containerised app OR source code (that's right, it takes both), and then deploys that into the wild. This is literally a developers dream. You code it, they deploy it. It really is that simple. Look, I'll show you the only steps you need to take..

1) [Sign into](https://ibm.biz/BdqNmK) IBM Cloud or [create](https://ibm.biz/BdqNmK) an IBM Cloud account
2) Write some code and put it into GitHub OR containerise your application and store it in a container registry - literally anywhere you want
3) Link up Code Engine and watch that puppy deploy

As always, I will be using a Golang application for this tutorial as that's my favourite flavour but you can use anything you like.

Before we start, you need to get an API key set up. Follow these easy steps:

[Login or Sign up](https://ibm.biz/BdqNmK) for a free IBM Cloud account and then head to **Manage** -> **Access (IAM)** -> **View All of your API keys** -> **Create an IBM Cloud API key**

Give the key a name and description and MAKE A NOTE OF THE KEY. You will not see this key again and you need it later.

Now for the juicy stuff.

Head to the search bar and type in **Code Engine** and select the catalogue option as shown below
![search bar](https://dev-to-uploads.s3.amazonaws.com/i/tlt53rci2ne2mb9xh5vz.png)

![code engine home](https://dev-to-uploads.s3.amazonaws.com/i/2j8k34nlloqx06at4zpf.png)


Now let's be real, playing with containers is great but not everyone knows how to containerise their app or know the ins and outs of containerisation. Since lots of developers just deliver code, we will select the option to **"Run your source code"**. All this needs is a github link and Code Engine does the heavy lifting here.

![run your source code](https://dev-to-uploads.s3.amazonaws.com/i/diun3o0ogyxzv99ho5v7.png)

Select this option and click on "Start creating ->"

Next you will be prompted to select "Application" or "Job".

- Application - A project you intend to keep alive, such as a HTTP server
- Job - A block of code you intend to run to completion, such as a function

For this tutorial, we are going to select "Application" as we want to make a HTTP server. Beneath that option, you will be promped to create a project. A project is what houses this deployed application. Click on "Create" and fill in the details on the panel that pops out and then click "Create" to make the project (shown below).

![application vs job](https://dev-to-uploads.s3.amazonaws.com/i/mk5y1vyqg2bsy32n52ba.png)

![create project](https://dev-to-uploads.s3.amazonaws.com/i/29f7272t6vs9akf3089u.png)

Now we can carry on with the application. Give it a name, select "Source Code" because that's what we are using, specify the `.git` repository url and then click on "Specify build details" - this is the most important part.

* **Note** I am using a simple [golang http server](https://github.com/liamchampton/gohttpserver.git) I had in GitHub - feel free to use it too

![build details](https://dev-to-uploads.s3.amazonaws.com/i/pos8oi05swigw5obemmv.png)

A window will pop out and the details will be pre-configured - only change these if you need to specify something other than the root of the directory and a different branch.

![more build details](https://dev-to-uploads.s3.amazonaws.com/i/jrgax9jr0547jclb1wlt.png)

Next, on the "Strategy" stage you will be asked how you would like this built. Dockerfile or Cloud Native Buildpack? This might seem a bit confusing but fear not, this is just how you want the code to be bundled up and run. For ease, we will just leave it pre-configured as the `Dockerfile`.

![strategy build details](https://dev-to-uploads.s3.amazonaws.com/i/nind807irm4eq2v8mxi7.png)

Finally, the output stage. If you do not already have a container registry / repository in place you need to set one up. This is just so that Code Engine has a place to store the built image of your source code.

It might look a little garish and something like this

![output stage](https://dev-to-uploads.s3.amazonaws.com/i/ddisrug25wrfngwzyfqx.png)

Click on "Add" to add a registry and you will the be presented with a bunch of fields to fill out that look like this..

![add regisrty](https://dev-to-uploads.s3.amazonaws.com/i/btfk45axm8qhpzkpiky6.png)

Now, lets take a second to walk through these bits
1. Registry name - This is the name you will identify the registry with, you can choose whatever you like. As seen above I have chosen "http-project-registry"
2. Registry server - The eagle eyed reader might have noticed that this project is based on a server in Dallas. At the point of writing this, Code Engine is in beta meaning Dallas is the only available deployment location. I am using IBM Cloud container registry so my sever address is `us.icr.io`. You can choose Dockerhub or any other container registry.
3. Password - If you are not using IBM Cloud container registry, you will need to provide your user/password. If you ARE using IBM Cloud container registry like me, you will only be prompted for the password as the "Username" is pre-filled. Remeber that API I asked you to make earlier? - that API key goes in here as the password.
4. Email - This is optional, I haven't put it in but there is no harm in specifying a valid email address.

Click "Add" and you will then get a pop out that looks similar to the one below. You may have to enter a repository name if this is your first project. You can call this anything you like.

![output stage complete](https://dev-to-uploads.s3.amazonaws.com/i/ra7uhy6nsb80iopqksfi.png)

Click on "Done" and then click on "Deploy".

Wait for the applicaiton to be built and deployed. Click on "Application URL" to view the deployed application.

![deployed application](https://dev-to-uploads.s3.amazonaws.com/i/5ebl19obph2en0burqoo.png)

.. And just like that, you have a cloud native application built from raw source code. As I said earlier, you just need to focus on pushing your code and let Code Engine do the rest!

Now close the browser tab / window that is running the application. Have you spotted anything weird in the project window? There might be some lag to this but you might notice you have "0 instances" running.

![0 instances](https://dev-to-uploads.s3.amazonaws.com/i/18o9uleyvsd5x3d5kqhf.png)

This is just another one of those cool perks where you would only pay for what you use on a larger project. Under the covers, Code Engine uses Knative which has this awesome feature of being able to scale down to 0 if no traffic is being received. Checkout the [Knative](https://knative.dev/) docs to read more about this!

### Disclaimer
**At the time of writing this, Code Engine is in beta so you may experience fluctuating behaviour and projects are kept alive for only 7 days.**


If you have any questions or want to see more content like this, feel free to reach out!

[GitHub](https://github.com/liamchampton)
[Twitter](https://twitter.com/liamchampton)
[Instagram](https://www.instagram.com/liamhampton/)
[LinkedIn](www.linkedin.com/in/liam-conroy-hampton)