Deploy an ASP.NET Core App to Google Cloud
In this post, I will show how to deploy an ASP.NET Core web application to Google Cloud Platform (GCP).
I will use the following tools:
- Google Cloud SDK
- Visual Studio 2017 Community Edition
- .NET Core SDK 2.2
- Google Cloud Tools for Visual Studio
The sections of this post will be as follows:
- Sign Up for Google Cloud
- Google Cloud SDK Installation and Creating the GCP project
- App Engine Initialization
- Enable Billing
- Google Cloud Tools for Visual Studio Installation
- Creating an ASP.NET Core Project and Linking to the GCP Project
- Publishing to Google Cloud
- Cleaning Up the GCP Project
If you are ready, let’s get started.
Sign up for Google Cloud
First, go to Google Cloud web site and click Get started for free button. Sign up with your Gmail account and get $300 (for free) to spend on Google Cloud Platform over the next 12 months.
Google Cloud SDK Installation and Creating the GCP project
Now, download the Google Cloud SDK.
After installation has completed, accept the following options:
- Start Cloud SDK Shell
- Run gcloud init
Next, a web browser will be opened and you will be requested to login to your Google account (select the one you used to register to GCP).
For future reference, the following command accomplishes the same mission:
gcloud auth login
Then, you will be prompted to use an existing project or create a new one, select the latter one and create a new project.
The project ID must be a unique name and keep in mind that at the end your application’s URL will be like:
https://[Your-project_id].appspot.com
You can also use the following command to create a project:
gcloud projects create [Your-project_id]
I created the following project:
gcloud projects create aspnetcoreapp-cloud
We can verify the project was created with the following command:
gcloud projects describe [Your-project_id]
App Engine Initialization
Now, we will initialize our App Engine app with our project and choose its region:
Run the following command and select a region when prompted:
gcloud app create --project=[Your-project_id]
Enable Billing
Now, we will enable billing for our project.
A billing account needs to be linked to your project in order for the application to be deployed to App Engine.
Go to Google Cloud Console and select the project you created from the top menu and click Open:
Next, in the search box type Billing and choose the Billing menu:
In the next dialog, select Link a billing account and then set your account.
Your flexible environment deployment will incur costs while it is active. We will clean up our project when we are finished to avoid ongoing costs at the end of this tutorial.
Google Cloud Tools for Visual Studio Installation
Open Visual Studio and go to Tools -> Extensions and Updates
Search for Google Cloud Tools for Visual Studio and Install (It’s already installed on my computer):
You should restart Visual Studio after this operation.
As a side note, Cloud Tools for Visual Studio is not supported in VS 2019. Cloud Tools for Visual Studio is an open source project and there is an issue about this subject. The last message in this issue is as follows:
So, I am using VS 2017 for this tutorial.
Creating an ASP.NET Core Project and Linking to GCP Project
Now, we will create our web application. Open File -> New -> Project and select ASP.NET Core Web Application and then name the project and click OK.
In the next dialog, select Web Application:
Next, we will link our application to our GCP project.
Open Tools -> Google Cloud Tools -> Manage Accounts:
and select Add account. Login to the account that you used to register to the GCP.
You will see your account logged on the top right corner of Visual Studio. There is a No Project label near this account and click that and click Select Project:
Select the project that you created above.
app.yaml
Next, add a new file called app.yaml to the project and add the following code to this file:
runtime: aspnetcore
env: flex
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/dotnet/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
The app.yaml file describes an app's deployment configuration. Here, app.yaml specifies the runtime used by the app, and sets
env: flex
, specifying that the app uses the flexible environment.
At this point, we should build the project.
Publishing to Google Cloud
Now, right-click on the project and select Publish To Google Cloud…
In the next dialog, select App Engine Flex:
Next, you will get the following dialog:
Enable the services link did not work for me. If you experience the same issue, then go to the Google Cloud Console and write APIs & Services in the search box and then search for app engine admin api in the library:
and Enable this API.
Do the same operation for the following API too:
Next, select Publish to Google Cloud again and click Publish in the next dialog:
I got the following error in this step:
The “TransformWebConfig” task failed unexpectedly.
I followed the solution in this StackOverflow post and changed InProcess
in csproj file with lowercase as follows:
<AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>
I published again and deployment succeeded, finally :) It took 9–10 minutes for the deployment operation to complete. After it is completed, a web browser is launched automatically :
As I mentioned above, your application’s URL is
https://[Your-project_id].appspot.com
The web page is delivered by a web server running on an App Engine instance on GCP.
Cleaning Up the Project
To avoid incurring charges, we can delete our GCP project to stop billing for all the resources used within the project.
In the Google Cloud Console, search for Manage Resources. In this menu, select your project and click Delete:
I hope you found this post helpful and easy to follow. Please let me know if you have any questions and/or corrections.
And if you liked this post, please clap your hands 👏👏👏
If you want to find out more about Google Cloud Platform, you can have a look at this GCP Essentials quest. After completing this quest, you will have a monthly subscription for free and you can take other quests in Qwiklabs.
Bye!
UPDATE: Check the following post if you are interested in learning how to deploy an ASP.NET Core application with database access to Google Cloud:
References
https://cloud.google.com/appengine/docs/flexible/dotnet/quickstart