Containerizing a Running Application with AWS App2Container
Tags: dotnet, aws, docker, app2container
Published: 2022-07-05 13:25:39

Now that we have gone through containerizing an already existing application where you have access to the source code, let’s look at containerizing a .NET application in a different way. This is for those applications you may have that are running and where you may not have access to the source code, or you don’t deploy it, or there are other reasons where you don’t want to change the source code as we just went over earlier. Instead, you want to containerize the application by just “picking it up off its server” and moving it into a container. Up until recently, that was not a simple thing to do. However, AWS created a tool to help you do just that. Let’s look at that now.

## What is AWS App2Container?

AWS App2Container is a command-line tool that is designed to help migrate .NET web applications into a container format. You can learn more about and download this tool at [https://aws.amazon.com/app2container/](https://aws.amazon.com/app2container/).  It also does Java, but hey, we're all about .NET, so we won’t talk about that anymore! You can see the process in Figure 1, but at a high level, there are five major steps.

![Figure 1. How AWS App2Container works](https://billthevestguy.com/wp-content/uploads/2022/07/image.png)_Figure 1. How AWS App2Container works_

These steps are:

1. **Inventory** – This step goes through the applications running on the server looking for running applications. At the time of writing, App2Container supports ASP.NET 3.5, and greater, applications running in IIS 7.5+ on Windows.
2. **Analyze** – A chosen application is analyzed in detail to identify dependencies including known cooperating processes and network port dependencies. You can also manually add any dependencies that App2Container was unable to find.
3. **Containerize** – In this step, all the application artifacts discovered during the “Analyze” phase are “dockerized.” 
4. **Create** – This step creates the various deployment artifacts (generally as CloudFormation templates) such as ECS task or Kubernetes pod definitions.
5. **Deploy** – Store the image in Amazon ECR and deploy to ECS or EKS as desired.

There are three different modes in which you can use App2Container. The first is a mode where you perform the steps on two different machines. If using this approach, App2Container must be installed on both machines. The first machine, the _Server_, is the machine on which the application(s) that you want to containerize is running. You will run the first two steps on the server. The second machine, the _Worker_, is the machine that will perform the final three steps of the process based on artifacts that you copy from the server. The second mode is when you perform all the steps on the same machine, so it basically fills both the server and worker roles. The third mode is when you run all the commands on your worker machine, connecting to the server machine using the Windows Remote Management (WinRM) protocol. This approach has the benefit of not having to install App2Container on the server, but it also means that you must have WinRM installed and running. We will not be demonstrating this mode.

App2Container is a command-line tool that has some prerequisites that must be installed before the tool will run. These prerequisites are listed below.

- **AWS CLI** – must be installed on both server and worker
- **PowerShell 5.0+** – must be installed on both server and worker
- **Administrator rights** – You must be running as a Windows administrator
- **Appropriate permissions** – You must have AWS credentials stored on the worker machine as was discussed in the earlier chapters when installing the AWS CLI.
- **Docker tools** – Docker version 17.07 or later must be installed on worker
- **Windows Server OS** – Your worker system must run on Windows OS versions that support containers, namely Windows Server 2016 or 2019. If working in server\worker mode, the server system must be Windows 2008+.
- **Free Space** – 20-30 GB of free space should be available on both server and worker

The currently supported types of applications are

- Simple ASP.NET applications running on a single server
- A Windows service running on a single server
- Complex ASP.NET applications that depend on WCF, running on a single server or multiple servers
- Complex ASP.NET applications that depend on Windows services or processes outside of IIS, running on a single server or multiple servers
- Complex, multi-node IIS or Windows service applications, running on a single server or multiple servers

There are also two types of applications that are not supported:

- ASP.NET applications that use files and registries outside of IIS web application directories
- ASP.NET applications that depend on features of a Windows operating system version prior to Windows Server Core 2016

Now that we have described App2Container as well as the .NET applications on which it will and will not work, the next step is to show how to use the tool.

## Using AWS App2Container to Containerize an Application

We will first describe the application that we are going to containerize. We have installed a .NET Framework 4.7.2 application onto a Windows EC2 instance that supports containers; the AMI we used is shown in Figure 2. Please note that since EC2 regularly revises its AMIs, you may see a different Id.

![Figure 2. AMI used to host the website to containerize](https://billthevestguy.com/wp-content/uploads/2022/07/image-1.png)_Figure 2. AMI used to host the website to containerize_

The application is connected to an RDS SQL Server instance for database access using Entity Framework, and the connection string is stored in the _web.config_ file.

The next step, now that we have a running application, is to download the AWS App2Container tool. You can access the tool by going to [https://aws.amazon.com/app2container/](https://aws.amazon.com/app2container/) and clicking the **Download AWS App2Container** button at the top of the page. This will bring you to the _Install App2Container_ page in the documentation which has a link to download a zip file containing the App2Container installation package. Download the file and extract it to a folder on the server. If you are doing the work using the server\worker mode, then download and extract the file on both servers. After you unzip the downloaded file, you should have 5 files, one of which is another zipped file.

Open PowerShell and navigate to the folder containing App2Container. You must then run the install script.

```
PS C:\App2Container> .\install.ps1
```

You will see the script running through several checks and then present some terms and conditions text that will require you to respond with a y to continue. You will then be able to see the tool complete its installation.

The next step is to initialize and configure App2Container. If using server/worker mode, then you will need to do this on each machine. You start the initializing with the following command.

```
PS C:\App2Container> app2container init
```

It will then prompt you for a _Workspace directory path for artifacts_ value. This is where the files from the analysis and any containerization will be stored. Click **Enter** to accept the default value or enter a new directory. It will then ask for an _Optional AWS Profile_. You can click **Enter** if you have a default profile setup or you can enter the name of the profile to use if different.

_**Note**: It is likely that a server running the application you want to containerize does not have the appropriate profile available. If not, you can set one up by running the aws configure command to set up your CLI installation that App2Container will use to create and upload the created container._

Next, the initialization will ask you for an _Optional S3 bucket for application artifacts_. Providing a value in this step will result in the tool output also being copied to the provided bucket. You can click **Enter** to use the default of “no bucket” however, at the time of this writing you must have this value configured so that it can act as storage for moving the container image into ECR. We used an S3 bucket called “prodotnetonaws-app2container”. The next initialization step is whether you wish to _Report usage metrics to AWS? (Y/N)_. No personal or confidential information is gathered, so we recommend that you click **Enter** to accept the default of “Y”. The following initialization prompt asks if you want to _Automatically upload logs and App2Container generated artifacts on crashes and internal errors? (Y/N)_. We want AWS to know as soon as possible if something went wrong so we selected “y”. The last initialization prompt is asking whether to _Require images to be signed using Docker Content Trust (DCT)? (Y/N)_. We selected the default value, “n”. The initialization will then display the path in which the artifacts will be created and stored. Figure 3 shows our installation when completed.

![Figure 3. Output from running the App2Container initialization](https://billthevestguy.com/wp-content/uploads/2022/07/image-2.png)_Figure 3. Output from running the App2Container initialization_

For those of you using the server/worker mode approach, take note of the application artifact directory displayed in the last line of the command output as this will contain the artifacts that you will need to move to the worker machine. Now that the application is initialized, the next step is to take the inventory of eligible applications running on the server. You do this by issuing the following command:

```
PS C:\App2Container> app2container inventory
```

The output from this command is a JSON object collection that has one entry for each application. The output on our EC2 server is shown below:

```
{
     "iis-demo-site-a7b69c34": {
          "siteName": "Demo Site",
          "bindings": "http/*:8080:",
          "applicationType": "IIS"
      },
      "iis-tradeyourtools-6bc0a317": {
          "siteName": "TradeYourTools",
          "bindings": "http/*:80:",
          "applicationType": "IIS"
      }
}
```
As you can see, there are two applications on our server, the “Trade Your Tools” app we described earlier as well as another website “Demo Site” that is running under IIS and is bound to port 8080. The initial key is the application ID that you will need moving forward.

_**Note**: You can only containerize one application at a time. If you wish to containerize multiple applications from the same server you will need to repeat the following steps for each one of those applications._

The next step is to analyze the specific application that you are going to containerize. You do that with the following command, replacing the application ID (APPID) in the command with your own.

```
PS C:\App2Container> app2container analyze --application-id APPID
```

You will see a lot of flashing that shows the progress output as the tool analyzes the application, and when it is complete you will get output like that shown in Figure 4.

![Figure 4. Output from running the App2Container analyze command](https://billthevestguy.com/wp-content/uploads/2022/07/image-3.png)_Figure 4. Output from running the App2Container analyze command_

The primary output from this analysis is the analysis.json file that is listed in the command output. Locating and opening that file will allow you to see the information that the tool gathered about the application, much of which is a capture of the IIS configuration for the site running your application. We won’t show the contents of the file here as it is several hundred lines long, however, much of the content of this file can be edited as you see necessary. 

The next steps branch depending upon whether you are using a single server or using the server/worker mode.

### When containerizing on a single server

Once you are done reviewing the artifacts created from the analysis, the next step is to containerize the application. You do this with the following command

```
PS C:\App2Container> app2container containerize --application-id APPID
```

The processing in this step may take some time to run, especially if, like us, you used a free-tier low-powered machine! Once completed, you will see output like Figure 5.

![Figure 5. Output from containerizing an application in App2Container](https://billthevestguy.com/wp-content/uploads/2022/07/image-4.png)_Figure 5. Output from containerizing an application in App2Container_

At this point, you are ready to deploy your container and can skip to the “Deploying…” section if you don’t care about containerizing using server/worker mode.

### When containerizing using server/worker mode

Once you are done reviewing the artifacts created from the analysis, the next step is to extract the application. This will create the archive that will need to be moved to the worker machine for containerizing. Also, the tool will upload the archive to the S3 bucket provided during initialization. Since we didn’t provide a bucket, we must manually copy the file. The command to extract the application is:

```
PS C:\App2Container> app2container extract --application-id APPID
```

This command will process, and you should get a simple “Extraction successful” message. 

Returning to the artifact directory that was displayed when initializing App2Container, you will see a new zip file named with your Application ID. Copy this file to the worker server.
Once you are on the worker server and App2Container has been initialized, the next step is to containerize the content from the archive. You do that with the following command

```
PS C:\App2Container> app2container containerize --input-archive PathToZip
```

The output from this step matches the output from running the containerization on a single server and can be seen in Figure 5 above.

The next article will show how to deploy this containerized application into AWS.