Deploy ASP.NET Core Host on AWS EC2 Instance with Nginx

Exported on 01-Nov-2021 17:04:11

Using Attune to deploy an Ubuntu AWS EC2 instance with NGINX and ASP.NET Core

This Blueprint is used for deploying an Ubuntu AWS EC2 instance with NGINX and ASP.NET Core installed.

ASP.NET Core is a free and open-source web framework, developed by Microsoft.

NginX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

Pre-Blueprint Attune setup
  1. On the Inputs tab, create a Windows Node for the host you wish to run the AWS EC2 shutdown script.
  2. On the Inputs tab, create a Windows Credentials to connect to the host you wish to run the AWS EC2 shutdown script.
  3. On the Inputs tab, create a Text value to store the values below:
    • AccessKey: This is the AWS IAM User access key (DataType: String).
    • SecretKey: This is the AWS IAM User secret key (DataType: String).
    • HashValue: This holds a hash table containing the Region of the virtual machine and KeyPair (DataType: Hashtable).
    • AWSImageId: This holds the AWS Image Id (DataType: String).
    • AWSInstanceType: This holds the AWS Instance Type (DataType: String).

HashValue Syntax:

@{"Region" = "eu-west-2";"KeyPair" = "pemkeyname"}

NOTE: Ensure to edit the value of the parameters AccessKey and SecretKey in Attune to match the AWS IAM User Credential with the privilege to perform this operation.

NOTE: The Region should be edited as well to match the desired region for the EC2 Instance.

NOTE: The KeyPair should be edited as well to match the AWS Key Pair

NOTE: The AWSImageId should be edited as well to match the desired AWS Image ID (Default = ami-0244a5621d426859b)

NOTE: The AWSInstanceType should be edited as well to match the desired AWS Instance Type (Default = t2.micro)


Blueprint Steps
  1. Check and Install the AWS PowerShell Module
  2. Deploy an Ubuntu AWS EC2 instance with NGINX and ASP.NET Core installed

Parameters

Name Type Script Reference Default Value Comment
Attune Node Windows Server attuneNode This is an Attune Node
Attune Node Credential Windows OS Credential attuneNodeCredential This is an Attune Node Credential

1 - Install AWS PowerShell Module

This step installs the AWS PowerShell Module

The Blueprint first gets the Execution Policy of the current PowerShell session.

Then, checks if the Execution Policy is set to Unrestricted.

If it's not, it then sets the Execution Policy to Unrestricted for the current PowerShell session.

Next, it checks if the AWSPowerShell module is installed.

If it's not installed, it then goes ahead to install the module.

The connection details have changed from the last step.

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credential}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region for ExecutionPolicy
# Get Execution Policy of the current process
$Script:ProcessEP = Get-ExecutionPolicy -Scope Process

#Get the value of the Execution Policy and save it in the Variable
$Script:ValueProcessEP = ($Script:ProcessEP).value__

# Check if the Execution Policy of the process is set to Unrestricted
if ($Script:ValueProcessEP -eq 0) {

    # Write the message
    Write-Output "Execution Policy is already set to Unrestricted for the Process"
    # Check if the Execution Policy of the process is already set
}else{

    # Set the ExecutionPolicy of the Process to Unrestricted
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force -Confirm:$false

    # Checks if the Execution Policy has been set
    if ((Get-ExecutionPolicy -Scope Process).value__ -eq 0) {

        # Write the message
        Write-Output "Execution Policy is now set to Unrestricted for the Process"
    }
}
#EndRegion for ExecutionPolicy 


#Region to Check if AWSPowerShell Module is installed 
if ($null -ne (Get-InstalledModule -Name AWSPowerShell -MinimumVersion "4.1.13.0" -ErrorVariable +ErrorAWSV -ErrorAction SilentlyContinue)) {

    # Get the AWS module installed and save it in a variable
    $Script:GetAWSModule = Get-InstalledModule -Name AWSPowerShell -MinimumVersion "4.1.13.0" -ErrorVariable +ErrorAWSV -ErrorAction SilentlyContinue

    # echo the message
    Write-Output "AWS PowerShell Module exists ... checking ..."

    # Gets the build number for the AWS Module 
    $Script:AWSModuleBuild = ($Script:GetAWSModule).Version

    # Checks the build number to meet requirements 
    if ($Script:AWSModuleBuild -like "*4.1.13.0*") {

        # Saves and converts Module version name to a variable
        $Script:OutVersion = ((($Script:GetAWSModule).Version)).tostring()

        # echo the message
        Write-Output "AWSPowerShell Module Version $Script:OutVersion meets the minimum requirement."

        # Check if the build version is on 13
    }else{
        
        # echo the message
        Write-Output "AWS PowerShell Module is updated :)"
    }
}else{
    # echo the message
    Write-Output "AWS PowerShell Module is not installed"
    
    # echo the message
    Write-Output "AWS PowerShell Module is installing..."

    # Install AWS Powershell Module 
    Install-Module -Name AWSPowerShell -MaximumVersion "4.1.13.0" -Scope "CurrentUser" -AllowClobber:$true -Confirm:$false -Force

    # echo the message
    Write-Output "AWS PowerShell Module is installed :)"
}
#EndRegion Check if AWSPowerShell Module is installed

2 - Deploy ASP.NET Core and NGINX

This step deploys an AWS EC2 Instance of an Ubuntu 18.04 image and installs NGINX and ASP.NET Core

The Blueprint first gets the Execution Policy of the current PowerShell session.

Then, checks if the Execution Policy is set to Unrestricted.

If it's not, it then sets the Execution Policy to Unrestricted for the current PowerShell session.

Next, the AWSPowerShell module is imported to the current session.

Then the values below are set:

  1. AccessKeyValue: This is the AWS IAM User access key corresponding to the AccessKey set in the Inputs Tab.
  2. SecretKeyValue: This is the AWS IAM User secret key corresponding to the SecretKey set in the Inputs Tab.
  3. HashValue: This holds a hashtable containing the Region of the virtual machine and KeyPair in AWS corresponding to the HashValue set in the Inputs Tab.
  4. AWSImageId: This holds the AWS Image Id (Default = ami-0244a5621d426859b) corresponding to the AWSImageId set in the Inputs Tab.
  5. AWSInstanceType: This holds the AWS Instance Type (Default = t2.micro) corresponding to the AWSInstanceType set in the Inputs Tab.

A variable UserDataText holding a Bash installation script is declared.

Below is Bash installation script:

#!/bin/bash
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get install -y dotnet-sdk-5.0
sudo apt-get install -y dotnet-sdk-3.1
sudo apt-get install -y apt-transport-https && \
sudo apt-get install -y aspnetcore-runtime-5.0
sudo apt-get install -y aspnetcore-runtime-3.1
sudo apt-get install -y nginx

Next, the AWS IAM User Credential is set and saved in the local credential store.

Then an AWS EC2 Instance with an Ubuntu Image is deployed with ASP.NET Core and NGINX installed.

Finally, the credential profile created in the session is removed from the local credential store.

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credential}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
# This step deploys an AWS EC2 Instance of an Ubuntu 18.04 image and installs NGINX and ASP.NET Core

The Blueprint first gets the Execution Policy of the current PowerShell session.

Then, checks if the Execution Policy is set to Unrestricted.

If it's not, it then sets the Execution Policy to Unrestricted for the current PowerShell session.

Next, the AWSPowerShell module is imported to the current session.

Then the values below are set:

1. AccessKeyValue: This is the AWS IAM User access key corresponding to the `AccessKey` set in the Inputs Tab.
1. SecretKeyValue: This is the AWS IAM User secret key corresponding to the `SecretKey` set in the Inputs Tab.
1. HashValue: This holds a hashtable containing the Region of the virtual machine and KeyPair in AWS corresponding to the `HashValue` set in the Inputs Tab.
1. AWSImageId: This holds the AWS Image Id `(Default = ami-0244a5621d426859b)` corresponding to the `AWSImageId` set in the Inputs Tab.
1. AWSInstanceType: This holds the AWS Instance Type `(Default = t2.micro)` corresponding to the `AWSInstanceType` set in the Inputs Tab.

A variable `UserDataText` holding a Bash installation script is declared.

Below is Bash installation script:

```bash
#!/bin/bash
wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get install -y dotnet-sdk-5.0
sudo apt-get install -y dotnet-sdk-3.1
sudo apt-get install -y apt-transport-https && \
sudo apt-get install -y aspnetcore-runtime-5.0
sudo apt-get install -y aspnetcore-runtime-3.1
sudo apt-get install -y nginx
```

Next, the AWS IAM User Credential is set and saved in the local credential store.

Then an AWS EC2 Instance with an Ubuntu Image is deployed with ASP.NET Core and NGINX installed.

Finally, the credential profile created in the session is removed from the local credential store.