Deploy an ASP.NET Core Application on Windows Server with AWS Lightsail
Deploying a .NET application in the cloud is similar to deploying on-premise or at a datacenter. This tutorial demonstrates how deploy an application using a virtual private server managed by AWS Lightsail.
About | |
---|---|
✅ AWS Level | Intermediate - 200 |
⏱ Time to complete | 45 mins |
💰 Cost to complete | Free Tier eligible |
🧩 Prerequisites | - An AWS account: If you don't have an account, follow the Setting Up Your AWS Environment tutorial for a quick overview. For a quick overview for creating account follow Create Your AWS Account. - AWS credentials: Follow the instructions in Access Your Security Credentials to get your AWS credentials - A git client: Follow the instructions to Install Git for your operating system. - .NET installed - Powershell for your operating system. |
💻 Code Sample | Code sample used in tutorial on GitHub |
📢 Feedback | Any feedback, issues, or just a 👍 / 👎 ? |
⏰ Last Updated | 2023-06-30 |
practical-cloud-guide-code
repository. Build and zip the application.1
git clone https://github.com/build-on-aws/practical-cloud-guide-code
publish
directory.1
2
cd ./practical-cloud-guide-code/run-to-build/windows-app-deploy/aspnetcoreapp/
dotnet publish -c release
publish
directory when compressing the application. When the zip file is uncompressed, all the files will be in the root directory of th website.1
2
cd ./practical-cloud-guide-code/run-to-build/windows-app-deploy/aspnetcoreapp/bin/Release/net6.0/publish
Compress-Archive -Path ./ -DestinationPath ./deploy/app.zip
1
2
cd ./practical-cloud-guide-code/run-to-build/windows-app-deploy/aspnetcoreapp/bin/Release/net6.0/publish
zip ./windows-app-deploy/deploy/app.zip ./*
<my>-practical-cloud-guide
. Note that bucket names must be globally unique. Select Create Bucket.practical-cloud-guide
bucket, choose Objects.app.zip
and deploy_iis.ps1
from ./practical-cloud-guide-code/run-to-build/windows-app-deploy /deploy/
and choose Open.1
2
3
4
5
6
7
8
9
10
11
12
<powershell>
iex ($YourAccessKey = '<your-access-key>')
iex ($YourSecretKey = '<your-secret-key>')
iex ($YourRegion = '<your-region>')
iex (Set-DefaultAWSRegion -Region $YourRegion)
iex (Set-AwsCredential -AccessKey $YourAccessKey -SecretKey $YourSecretKey -StoreAs default)
iex (New-Item -Path 'C:\deploy' -ItemType Directory)
iex ($YourBucketName = '<my>-practical-cloud-guide')
iex ($YourAppKey = 'deploy_iis.ps1')
iex (Copy-S3Object -BucketName practical-cloud-guide -Key deploy_iis.ps1 -LocalFoil C:\deploy\deploy_iis.ps1)
iex (Copy-S3Object -BucketName $YourBucketName -Key $YourAppKey -LocalFile C:\deploy\$YourAppKey)
</powershell>
Note: Using access keys is not recommended practice, but for purposes of demonstration access keys are used in this tutorial. The keys will be deleted after the deployment.
Windows_Server_IIS
. Then choose Create Instance.C:\deploy
and use notepad to view the deploy_iis.ps1
script.1
2
cd C:\deploy
notepad.exe ./deploy_iis.ps1
SilentlyContinue
to prevent cmdlet outputs from writing to the terminal.1
2
3
4
5
6
7
8
9
10
11
12
13
14
Set-Variable $global:ProgressPreference SilentlyContinue
# Install IIS
Install-WindowsFeature Web-Server -IncludeManagementTools
# Download and install the ASP.NET Core 6.0 Hosting Bundle
$filein = "https://download.visualstudio.microsoft.com/download/pr/7ab0bc25-5b00-42c3-b7cc-bb8e08f05135/91528a790a28c1f0fe39845decf40e10/dotnet-hosting-6.0.16-win.exe"
Invoke-WebRequest -Uri $filein -OutFile "$(pwd)\dotnet-hosting-6.0.16-win.exe"
Start-Process -FilePath "$(pwd)\dotnet-hosting-6.0.16-win.exe" -Wait -ArgumentList /passive
# Stop and start IIS
net stop was /y
net start w3svc
app.zip
from the S3 bucket created earlier and unzips it on the directory.1
2
3
4
5
6
7
# download and unzip the application
New-Item -Path 'C:\inet\newsite' -ItemType Directory
$YourBucketName = "<my>-practical-cloud-guide"
$AppKey = "app.zip"
$AppFile = "C:\inet\newsite\" + $AppKey
Copy-S3Object -BucketName $YourBucketName -Key $AppKey -LocalFile $AppFile
Expand-Archive $AppFile -DestinationPath "C:\inet\newsite"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Create application pool
$appPoolName = 'DemoAppPool'
New-WebAppPool -Name $appPoolName -force
# Create website
New-Item IIS:\Sites\DemoSite -physicalPath C:\inet -bindings @{protocol="http";bindingInformation=":8080:"}
Set-ItemProperty IIS:\Sites\DemoSite -name applicationPool -value $appPoolName
# Add application to website
New-Item IIS:\Sites\DemoSite\DemoApp -physicalPath C:\inet\newsite -type Application
Set-ItemProperty IIS:\sites\DemoSite\DemoApp -name applicationPool -value $appPoolName
# start new website
Start-WebAppPool -Name $appPoolName
Start-WebSite -Name "DemoSite"
# Open application on Edge
start microsoft-edge:http://localhost:8080/DemoApp
# delete AWS credentials
Remove-AWSCredentialProfile -Force -ProfileName default
1
C:\deploy\deploy_iis.ps1
deploy_iis.ps1
Powershell script shows how you can use familiar scripting tools and commands to automate configuring Windows services such as IIS while interacting with AWS resources. Although a simple example, this shows how to script can implement a continuous deployment in a DevOps workflow. In future articles, we will examine how to build a Continuous Integration/Continuous Deployment pipeline to automate the delivery of applications.Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.