.NET 8 Application 0-To published on AWS in a half-hour

·

6 min read

Last week I had scheduled Hour of Code with my local middle school. Hour of Code is a lot of work to organize yearly, but it is a labor of love. The kids enjoy it, the volunteers have a good time, the school thinks it's wonderful... and it's an opportunity to give back to the community.

This year, I worked with what we call the "demo crew" for our Hour of Code session. This meant that I had to set up and run a technical demo for some of the students who came through the area. With working full-time for Caylent, carrying two college classes, and trying to keep up with real life... putting together a demo slipped by the wayside until I was running short on time.

With only days to go, I needed to get a .NET Demo up and running on AWS.

The project

Last year, I wrote a project designed to showcase Amazon Rekognition for image moderation and labeling. I decided to use this as my project for the demo environment. However, this project was written when .NET 6 was the current version. The first task was to upgrade the project to .NET 8. Luckily, Microsoft makes this extremely easy to do.

With a simple right-click on the project and select properties, I can upgrade my application to .NET 8. (Assuming I have already installed the new SDK on my development machine.)

Back in the .NET Framework days, this was a risky proposition. Incompatibilities from one version to the next were commonplace. I remember more than once finding that creating a new project and bringing code over gradually was easier than upgrading an existing application from one version of the framework to the next.

Note: If you are upgrading your project, don't forget to upgrade your test project. Because... of course, you have a test project, right... :-)

The upgrade was complete; my application compiled on the first attempt and ran on the second attempt. (I forgot to do my AWS SSO Login the first time.)

Creating my .NET 8 container

So now I have my shiny new .NET 8 application running on my machine. However, I didn't want to give the middle school students a "It works on my machine" demo, even though this would be a great "Real world" lesson. I wanted my application to be up and running inside AWS.

Luckily the AWS .NET Toolkit for Visual Studio made this very easy.

Considering that the application is running .NET 8, and there are no services ready to support .NET 8 yet, I decided to deploy my application as a container. I added a docker file to my project in Visual Studio with a few clicks.

Next, with a Right Click on the project, I have the "Publish to AWS..." option available to me.

Right away, the toolkit looks at my project and figures out some of the likely deployment methods that I might want to use for my project.

One slight oddity with the toolkit. If you do not have a docker file as part of your project, the option to publish to Elastic Container Registry (ECR) does not appear... even though all the other options also use containers and offer to build the docker file for you automatically.

Because my project already has a docker file generated by Visual Studio, I am going to use the option to deploy to Elastic Container Registry (ECR). I don't really like the random number tag name for my container, so I am going to click "Edit Settings" and change that.

And then click on Publish to get my application published to AWS:

The total elapsed time from opening up my project, upgrading to .NET 8, rebuilding and getting the container published to a new instance of ECR... less than 5 minutes. Updating Visual Studio took longer.

Getting my app running

Now that I have a container published to ECR, I want to get my application up and running quickly. Luckily AWS has an "Easy Button" for just this situation. The Easy Button is called App Runner. With App Runner, I have a fully managed service that takes my container and does all the underlying work necessary to make it work. If you have ever used something like Elastic Beanstalk, App Runner is like next-level Beanstalk without the frustration.

Inside the App Runner service console, I can just click "Create an App Runner service" to get started.

App runner is all ready to create a service based on a container image. If the Registry is in my account I can click the Browse button to find it.

I can select my Repository from my account, and the tag that I use to keep track of my latest version.

Now, of course, I want to be able to have App Runner automatically update my application in production when I update the source code. Once again this is incredibly easy, All I need to do is tell App runner to automatically update, and provide a service role with the necessary permissions. In my case I already had a service role available from a previous effort with App Runner, however, if you don't the "Create new service role" will do all the work for you.

Next, I supply a service name and the resources for the service. I am leaving the defaults for the resources as I am not expecting a huge load on my service.

The one thing you need to be on the lookout for is the instance role. This optional field defines the permissions that your application needs to run. If you miss this, then your application will likely fail as it will not be able to access any AWS resources. In my case, I had a role already created with the necessary permissions.

When you create your own role, make sure to set up the Trust relationship as follows:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "tasks.apprunner.amazonaws.com"
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

This will allow App Runner to use the role.

Once you have selected your Instance Role, you are ready to publish and get the application running in your account.

Summary

With Visual Studio and the AWS Toolkit for Visual Studio, I was able to get a >NET 6 application upgraded to .NET 8, and have that application up and running inside of AWS in less than half an hour. This process not only created a container-based deployment for my application but also set up automated updates to my application when the container image gets updated. All of this was possible today using standard AWS services without any changes necessary for .NET 8.

Did you find this article valuable?

Support Tom Moore by becoming a sponsor. Any amount is appreciated!