Building an Amazon ECS Service with an ASP.NET Website_
Building an Amazon ECS Service with an ASP.NET Website_
NET
Website
Overview
In this hands-on exercise, you will create an Amazon Elastic Container Service (Amazon ECS) service named
WeatherSite that consists of an ASP.NET website and a .NET Web API. The API will use an Amazon DynamoDB table to
retrieve weather data. The API will be deployed to an Amazon ECS service and the website to another Amazon ECS
service.
You can perform this lab on your local machine using Windows, MacOS, or Linux.
Duration
90 minutes
1 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
2 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
3 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
aws configure
4. Give your AWS user the permissions needed to deploy applications to Amazon ECS.
a. In the AWS Management Console, navigate to IAM.
b. Go to Users and choose your AWS user.
c. Choose Add permissions, then choose Attach existing policies directly.
d. Search for and select the check box for each of the following policies:
• PowerUserAccess
• AWSCloudFormationFullAccess
• AmazonECS_FullAccessAmazonEC2ContainerRegistryFullAccess
• AmazonSSMFullAccess
• IAMFullAccess
6. Install the AWS Deploy tool for .NET CLI with the following command:
7. Install an integrated development environment (IDE), such as Microsoft Visual Studio 2022 (Windows), or
Visual Studio Code or JetBrains Rider (Linux, macOS, Windows). Ensure that you have installed the options or
extensions for .NET web development in C#.
8. If you are using Visual Studio, install and configure the AWS Toolkit. With the toolkit, you can examine your
AWS deployments within the IDE.
a. Download and Install
4 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
b. Configure
9. Install Docker Desktop, which must be running on your machine. If you already have Docker, be aware that you
need version 17.05 or later of the Docker Engine.
5 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
4. Choose the Weather table name to get to its detail page, then choose Explore table items.
6 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
5. Add an item using the Form. You can also add items using the JSON view. See the Appendix for instructions.
a. Choose Create item and add an item with the following attributes: Location: Dallas, Timestamp: 2022-
07-23T06:00:00.
b. Choose Add new attribute to add attributes TempC (Number) 33, TempF (Number) 92, and Summary
(String) Hot.
6. Add more items using the JSON view. See the Appendix for instructions.
7 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
The completed table should have six items as listed in the following screenshot.
8 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
9 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
// app.UseHttpsRedirection();
You might or might not see a “Not secure” indicator about HTTPS in the browser, depending on
whether or not you accepted a local SSL certificate.
10 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
6. Configure ports.
The Web API will run at port 8080 (HTTP) and 8443 (SSL).
a. Open Properties/LaunchSettings.json in the code editor.
b. On line 17, change the applicationUrl values to the following:
"applicationUrl": "https://localhost:8443;http://localhost:8080",
11 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
namespace WeatherAPI;
b. On line 10, set RegionEndpoint to the AWS Region you are working in. For example,
RegionEndpoint.USEast1. This code implements a health check method at the root path of the service,
and a WeatherForecast method at /WeatherForecast.
The /WeatherForecast method takes a location parameter and retrieves data for it from the DynamoDB
Weather table. It performs a table scan to find records whose partition key matches the location.
Matching DynamoDB items are stored in a List of WeatherForecast objects. The results are returned as
an array of JSON records.
12 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Microsoft.AspNetCore.Mvc;
namespace WeatherAPI.Controllers;
[ApiController]
[Route("")]
public class WeatherForecastController : ControllerBase
{
static readonly RegionEndpoint region = RegionEndpoint.USWest2; //ß Add your Region here
[HttpGet("")]
public string GetHealthcheck()
{
return "Healthcheck: Healthy";
}
[HttpGet("WeatherForecast")]
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecast(string location =
"Dallas")
{
List<WeatherForecast> forecasts = new List<WeatherForecast>();
try
{
_logger.LogInformation($"00 enter GET, location = {location}");
_logger.LogInformation($"10 table.Scan");
List<Document> matches;
do
{
_logger.LogInformation($"20 table.GetNextSetAsync");
matches = await search.GetNextSetAsync();
foreach (var match in matches)
{
forecasts.Add(new WeatherForecast
{
Location = Convert.ToString(match["Location"]),
Date = Convert.ToDateTime(match["Timestamp"]),
TemperatureC = Convert.ToInt32(match["TempC"]),
TemperatureF = Convert.ToInt32(match["TempF"]),
Summary = Convert.ToString(match["Summary"])
});
}
} while (!search.IsDone);
}
catch (Exception ex)
{
_logger.LogError(ex, "90 Exception");
}
return forecasts.ToArray();
}
}
14 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
15 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
16 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
6. Stop testing.
a. Stop the program from running.
17 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
Note: The following deployment will fail if you do not have the Node.js and Docker
dependencies installed on your local machine. In that case, follow the instructions for
installing them: Node.js and Docker.
c. If you are prompted to Select an existing AWS deployment target, select the choice for Deploy to a
new Cloud Application.
d. Select the choice for ASP.NET Core App to Amazon ECS using Fargate.
e. For name of new CloudFormation stack, press Enter to accept the default name of WeatherAPI.
18 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
2. Edit settings.
a. When Current settings are displayed, enter more to see advanced settings. A numbered list of settings
is displayed.
b. Enter 9 to select Elastic Load Balancer and respond to prompts as follows:
• Create New Load Balancer: y
• Deregistration delay: 60
• Health Check path: /
• Health Check Timeout: 5
• Health Check Interval: 30
• Healthy Threshold Count: 5
• Unhealthy Threshold Count: 2
• Internet-Facing: y
19 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
b. Wait while the service is containerized and deployed, which will take several minutes. When it
completes, note the deployment details at the end of the output.
c. Record the details under Application Endpoint. You will need the endpoint to test the service.
Endpoint URL:
20 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
4. Review Dockerfile.
In your project folder, notice that a Dockerfile file has been added. The AWS deployment tool created
this file and used it with Docker to containerize the project. The Dockerfile should look similar to the
following. You might see 6.0 or 7.0 depending on which version you are using.
21 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
5. Review in the Amazon ECS console. Examine what’s been deployed in the AWS console.
a. In the AWS Management Console, navigate to Amazon ECS.
b. Choose Clusters from the navigation pane, and the WeatherAPI cluster should be listed in the content
pane. You should find one FARGATE service with three running tasks.
c. Choose the WeatherAPI cluster name to examine its detail.
22 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
d. In the content pane for WeatherAPI, explore the tabs to find what was deployed. You should find three
running tasks of launch type FARGATE.
23 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
c. Create the policy and enter (copy and paste) the following JSON. Replace [account] with your 12-digit
AWS account number, and [region] with your AWS Region (for example: us-west-2).
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": "dynamodb:*",
"Resource": "arn:aws:dynamodb:[region]:[account]:table/Weather"
}
]
}
24 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
25 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
c. Choose the Configuration and tasks tab, and then choose the Task definition link.
d. Choose the Task role link. This takes you to the Role definition in the IAM console.
26 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
f. Search for and select the ddb-weather permission. Choose Attach policies.
27 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
28 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
// app.UseHttpsRedirection();
4. Debug the project. The generated project is a simple web application, commonly used in .NET samples.
a. To try it, do either of the following:
i. Press F5 in your IDE.
ii. Run dotnet run from the command line and browse to the endpoint address, with
http://localhost:<port>.
29 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
b. In the browser tab that opens, there is a simple page with a welcome message.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"AppSettings": {
"WeatherAPI": "http://[service-address]"
}
}
30 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
6. Configure ports. The Web site will run at port 80 (HTTP) and 443 (SSL), but you might not be able to use those
ports locally. If your local development machine is running a local web server such as IIS Express, they might
not be available. For now, use ports 8080 and 8443.
a. Open Properties/LaunchSettings.json in the code editor.
b. On line 17, change the applicationUrl values to the following:
"applicationUrl": "https://localhost:8443;http://localhost:8080",
7. Code the WeatherSite record structure. Add file WeatherForecast.cs to the project with the following code. This
record structure matches the items returned by the Weather API.
namespace WeatherSite;
31 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WeatherSite.Pages;
32 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<style>
.styled-table {
border-collapse: collapse;
font-size: 0.9em;
font-family: sans-serif;
min-width: 400px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
margin-left: auto;
margin-right: auto;
}
.styled-table thead tr {
background-color: cornflowerblue;
color: #ffffff;
text-align: left;
}
.styled-table th,
.styled-table td {
padding: 12px 15px;
}
.styled-table tbody tr {
border-bottom: 1px solid #dddddd;
}
</style>
33 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
<div class="text-center">
<h1 class="display-4">@Model.Location Weather</h1>
<p>Weather Forecast</p>
<p>@Model.Message</p>
<p><a href="/?location=Dallas">Dallas</a></p>
<p><a href="/?location=Minneapolis">Minneapolis</a></p>
<tr><td>@forecast.Location</td><td>@forecast.Date.ToString("yyyy-MM-dd
HH:MM")</td><td>@forecast.TemperatureC</td><td>@forecast.TemperatureF</td><td>@for
ecast.Summary</td></tr>
}
</tbody>
</table>
}
</div>
At this point, you have created all the components for the architecture:
database table, API, and website.
34 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
35 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
36 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
Note: If you are using Visual Studio, set WeatherSite as the startup project before starting the
debugger.
2. Choose the Minneapolis link. The page should refresh with data for that city.
37 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
4. Optional: Configure production ports. Perform this step only if you have access to 80 and 443 to use as
production ports for the web site.
a. In the IDE with the WeatherSite project open, open Properties/LaunchSettings.json in the code editor.
b. On line 17, change the applicationUrl values as follows:
"applicationUrl": "https://localhost:443;http://localhost:80",
✓ Confirmed data is retrieved from the WeatherAPI backend Amazon ECS service.
38 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
e. For name of new CloudFormation stack, press Enter to accept the default name of WeatherSite.
39 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
2. Edit settings.
a. When Current settings are displayed, enter more to list the advanced settings. A numbered list of
settings is displayed. Enter 9 to select Elastic Load Balancer and respond to prompts as follows:
• Create New Load Balancer: y
• Deregistration delay: 60
• Health Check path: /
• Health Check Timeout: 5
• Health Check Interval: 30
• Healthy Threshold Count: 5
• Unhealthy Threshold Count: 2
• Internet-Facing: y
40 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
b. Wait while the website is containerized and deployed, which will take several minutes. When it
completes, note the deployment details at the end of the output.
Record the details under Application Endpoint. You will need endpoint to test the website.
Endpoint URL:
41 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
4. Review Dockerfile. In your project folder, notice that a Dockerfile file has been added. The AWS
deployment tool created this and used it with Docker to containerize the project. The Dockerfile should
look similar to the following. You might see 6.0 or 7.0 depending on which version you are using.
42 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
5. Review in the Amazon ECS console. Examine what’s been deployed in the AWS console:
a. In the Amazon ECS console, navigate to ECS.
b. Select Clusters from the navigation pane. The WeatherSite cluster should be listed. There should be
one FARGATE service with three running tasks.
c. Choose the WeatherSite cluster name to view its detail. Explore the tabs to see what was deployed.
6. Test the ECS endpoint.
a. In another browser tab, visit the endpoint URL you recorded earlier in Step 3b. The website should
display, defaulting to location Dallas.
b. Choose the link for Minneapolis, to display the data for that locale.
43 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
Congratulations, you have created a DynamoDB table and a website and API hosted in Amazon ECS on Fargate.
44 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
<p><a href="/?location=Seattle">Seattle</a></p>
45 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
d. When Current settings are displayed, select 1. Desired Task Count and change it from 3 to 2.
46 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
e. Press Enter to begin the deployment, which will take a few minutes.
f. Wait for the update to complete. Notice the endpoint for the Amazon ECS service has not changed,
because this is an update.
4. Confirm task count change in the Amazon ECS console. Navigate to Amazon ECS and choose Clusters. Your
WeatherSite task count, previously 3, should now be 2.
47 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
b. Click the link for Seattle and confirm that the data changes.
48 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
49 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
c. In the AWS Management Console, confirm that the WeatherAPI cluster is no longer listed.
50 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
Summary
In this lab activity, you achieved the following:
1. You created a DynamoDB table and populated it with weather data.
2. You developed a .NET Web API project that retrieves weather data from the DynamoDB table.
3. You deployed the WeatherAPI project to Amazon ECS on Fargate, using the AWS deployment tool for .NET CLI.
4. You developed a website that retrieves data from WeatherAPI.
5. You deployed the WeatherSite project to Amazon ECS on Fargate, using the AWS deployment tool for .NET
CLI.
6. You updated weather data and the website and deployed the updated website to Amazon ECS and modified its
task count.
7. You deleted the deployments for WeatherSite and WeatherAPI, and deleted the Weather DynamoDB table in
the AWS console.
51 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
{
{
"S": "Dallas"
},
{
},
{
"S": "Hot"
},
{
"N": "33"
},
{
"N": "92"
}
}
52 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
c. In the same way, add the second item (Dallas, mid-day) with the following JSON.
{
{
"S": "Dallas"
},
{
},
{
"S": "Scorching"
},
{
"N": "43"
},
{
"N": "109"
}
}
53 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
d. Add the third item (Dallas, evening) with the following JSON.
{
{
"S": "Dallas"
},
{
},
{
"S": "Hot"
},
{
"N": "36"
},
{
"N": "97"
}
}
e. Add the fourth item (Minnesota, morning) with the following JSON.
{
{
"S": "Minneapolis"
},
{
},
{
"S": "Cool"
},
{
"N": "13"
},
{
"N": "56"
}
}
54 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
f. Add the fifth item (Minnesota, mid-day) with the following JSON.
{
{
"S": "Minneapolis"
},
{
},
{
"S": "Balmy"
},
{
"N": "22"
},
{
"N": "72"
}
}
g. Add the sixth item (Minnesota, evening) with the following JSON.
{
{
"S": "Minneapolis"
},
{
},
{
"S": "Cool"
},
{
"N": "19"
},
{
"N": "67"
}
}
55 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Activity: Building an Amazon ECS Service with an ASP.NET
Website
5. Optional: If you want, add more table items for your favorite locations.
56 © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.