# Create and deploy a new web app to Cloud Run with Duet AI

Author: [Mete Atamel](https://atamel.dev/) | Published: 2024-01-23 | Topics: GenAI, DuetAI, Google Cloud Platform | Canonical: https://atamel.dev/posts/2024/01-23_duetai_create_deploy_webapp_clourun/


![Duet AI](/img/2024/duetai_logo.png)

I’ve been playing with [Duet AI](https://cloud.google.com/duet-ai), Google’s
AI-powered collaborator, recently to see how useful it can be for my development
workflow. I'm pleasantly surprised how helpful Duet AI can be when provided with
specific questions with the right context.

In this blog post, I document my journey of creating and deploying a new web
application to Cloud Run with Duet AI's help. I also capture some lessons learned
along the way to get the most out of Duet AI.

## Before you start

I’m using Duet AI with Cloud Code in Visual Studio Code but you can use Duet AI
from other IDEs supported by Cloud Code such as IntelliJ. You can also use it in
the Cloud Shell Editor or Cloud Workstations from Google
Cloud Console. Check out [Duet AI for
Developers](https://cloud.google.com/duet-ai/docs/discover/developers) page for
more details on how to get setup with Duet AI. 

## Create a new web app with Duet AI

I start by asking Duet AI how to create a new web app. In this case, I’m going
with [ASP.NET](ASP.NET) framework but you can choose your favorite framework/language.

**Prompt**
> How can I create a new ASP.NET web application from the terminal using dotnet
> command line tool? I want the app to be in MyWebApp folder.

**Answer**
> To create a new ASP.NET web application from the terminal using the dotnet
> command line tool, you can follow these steps:

> Open the terminal or command prompt. Navigate to the folder where you want to
> create the new ASP.NET web application. In this case, it is the `MyWebApp`
> folder.

> Run the following command:

> ```sh
> dotnet new web
> ```

Notice how specific my prompt is. I don't just say "How can I create a web
app?". That would be too general for Duet AI to provide useful answers. Rather,
I specify what tech stack to use and what tool to use to create the app. As a
result, the returned command is correct. Good start!

Next, let's test and make sure that the app runs fine locally, again with
specific instructions on what to test and with what tool.

**Prompt**
> How can I run `Program.cs` locally and then test it using curl?

**Answer**
> To run the` Program.cs` file locally, you can use the following steps:
> * Open the terminal or command prompt.
> * Navigate to the folder where the Program.cs file is located.
> * Run the following command:

> ```sh
> dotnet run
> `````

> This will start the web application and listen for requests on port 5000.
>
> To test the web application using curl, you can use the following command:

> ```sh
> curl http://localhost:5000
> ```

Everything looks good except the port number. I'm using .NET 8 which uses 5074
as the default port but Duet AI probably assumed an earlier version of .NET.
Regardless, it's an easy enough fix for me.

I got a new web app created and running locally pretty quickly.

## Add a new route with Duet AI

Next, let’s see if Duet AI can help me to add a new route to the web app.

In `Program.cs`, I add the following comment:

```csharp
// create a /greeting GET route with a `name` parameter
```

Duet AI generates the following code:

```csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

// create a /greeting GET route with a `name` parameter
app.MapGet("/greeting", (string name) => $"Hello {name}!");

app.Run();
```

That looks right. Let's test to make sure it works. While highlighting the newly
generated code, I ask the following:

**Prompt**
> How can I test the /greeting endpoint using curl? The server is running on port 5074

**Answer**
> To test the /greeting endpoint using curl, you can use the following command:

> ```sh
> curl http://localhost:5074/greeting?name=John
> ```

> This will send a request to the /greeting endpoint with the name parameter set
> to John.

Nice! I got a simple route created and tested with no effort. As you can see,
I’m being quite specific with my prompts and as a result, I get pretty good code
and commands.

## Deploy to Cloud Run with Duet AI

Now, we're ready to deploy our app to Cloud Run. First, let's ask Duet AI if we
need to change the code before deploying to Cloud Run.

**Prompt**
> Do I need to change anything in `Program.cs` to deploy and run it on Cloud Run?

**Answer**
> No, you do not need to make any changes to the Program.cs file to deploy and
> run it on Cloud Run. The code you have provided is sufficient to create a
> simple web application that can be deployed to Cloud Run.

> However, there are a few things you may want to consider when deploying your
> application to Cloud Run.

> First, you will need to create a Cloud Run service. You can do this by going
> to the Cloud Run page in the Google Cloud console and clicking the Create
> Service button.

> When you create your service, you will need to specify a region. You can
> choose a region that is close to your users or that has the resources you
> need.

> You will also need to specify a container image. You can use the container
> image that you built locally, or you can use a container image that is
> available in Artifact Registry.

> Once you have created your service, you can deploy your application by
> clicking the Deploy button.

This wasn't correct. For Cloud Run to work, the server needs to listen on port
8080 or `PORT` env variable. In hindsight, the "change anything" in my prompt
was not specific enough for Duet AI to provide helpful suggestions.

Let's go ahead and add the following comment in the code before `app.Run()`:

```csharp
// listen on PORT env variable or 8080
```

This generates the following code:

```csharp
// listen on PORT env variable or 8080
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
var url = $"http://0.0.0.0:{port}";
app.Urls.Add(url);

app.Run();
```

**Tip**: If the code generation does not kick in, use `Ctrl+Enter` to generate the code.

That's pretty good code and works!

Now, we're ready to deploy to Cloud Run. We can ask Duet AI "How can I deploy to
Cloud Run?" but that wouldn't be specific enough. Cloud Run can deploy code form
source code or a container. You can also use `gcloud` to deploy or use Google
Cloud Console.

With this knowledge, I try a more specific prompt to get the right command:

**Prompt**
> What's the gcloud command to deploy to Cloud Run from source code?

**Answer**
> To deploy your application to Cloud Run from source code, you can use the
> following gcloud command:

```sh
gcloud run deploy SERVICE --source .
Replace SERVICE with the name you want to give your service.
```

That is a good answer and allows us to deploy my web app to Cloud Run. 

Mission accomplished!

## Lessons learned

Let’s recap some lessons learned as I played with different prompts.

* **Be clear** with your prompts. E.g. “Create an application from the console”
  is ambiguous, it can mean terminal but it can also mean Google Cloud console.
* **Be specific** with your prompts. E.g. You can create an [ASP.NET](ASP.NET)
  web app from the IDE or using the dotnet command line too. Tell Duet AI which
  method you want for better answers.
* **Don’t expect perfection**. E.g. Duet AI thought the default port number is
  5000 but that’s probably for an earlier version of ASP.NET. Don’t expect to
  copy/paste answers but rather see them as approximate to what you need to do.
* **Nudge Duet AI** with your knowledge when it's not right. E.g. Duet AI
  doesn't tell us that you need to listen on PORT or 8080 on Cloud Run. But if
  you know that, you can use Duet AI to generate code for that.
* **Trigger code generation manually**. Duet AI code generation might get
  confused between generating comments and code. In that case, you can force
  code generation with `Ctrl+Enter`.
* **Be specific again!**. I can't emphasize this enough. E.g. Don't ask how to
  deploy to Cloud Run because that can involve code or containers but instead
  ask how to deploy from the source code using gcloud specifically.

---

In this blog post, I created a simple web application and deployed to Cloud Run
using Duet AI's help. With right questions and enough detail, Duet AI really
helps to speed up the process. This is great for a new and small app but how
about for an existing app? Does Duet AI help in that case. Stay tuned for the
next blog post.

As always, if you have any questions or feedback, feel free to reach out to me
on Twitter @[meteatamel](https://twitter.com/meteatamel).

