-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deploy ASP.NET Core and NGINX.ps1
99 lines (71 loc) · 3.33 KB
/
Deploy ASP.NET Core and NGINX.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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 Deploy Ubuntu and install NGINX and ASP.NET Core
# Import Module for AWS PowerShell
Import-Module -Name AWSPowerShell
# Save accesskey to this Variable
$Script:AccessKeyValue = "{accesskey.value}"
# Save secretkey to this variable
$Script:SecretKeyValue = "{secretkey.value}"
# # Set value to store profile
$Script:ProfileNameVaule = "DefaultSetKeys"
# Set the AWS Image ID
$Script:AWSImageId = "{awsimageid.value}"
# Set the AWS Instance Type
$Script:AWSInstanceType = "{awsinstancetype.value}"
# Hash Table containing Region of the virtual machine and KeyPair
$Script:HashValue = {hashvalue.value}
# UserData script for installation of DotNet and Nginx
$Script:UserDataText = "#!/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"
# Set AWS Credentials
Set-AWSCredential -AccessKey $Script:AccessKeyValue -SecretKey $Script:SecretKeyValue -StoreAs $Script:ProfileNameVaule
# check Hash Table
if ($null -eq $Script:HashValue['KeyPair'] -or $null -eq $Script:HashValue['Region']) {
# Write the message
Write-Output "Please check the Hash Table"
}else {
# Write the message
Write-Output "All keys are present in the Hash Table... checking keypair"
# Checking KeyPair in hashtable
if (!($Script:HashValue.ContainsKey('KeyPair')) -or $Script:HashValue.KeyPair -like "") {
# Write the message
Write-Output "KeyPair does not exist in the Hash Table or it is an empty string"
}else {
# Write the message
Write-Output "Creating Ubuntu EC2 instance with NGINX and ASP.NET Core"
# Creat New EC2 Instance
New-EC2Instance -ImageId $Script:AWSImageId -InstanceType $Script:AWSInstanceType -KeyName $Script:HashValue['KeyPair'] -Region $Script:HashValue['Region']`
-ProfileName $Script:ProfileNameVaule -UserData $Script:UserDataText -EncodeUserData -Force
}
}
# Remove Profile
Remove-AWSCredentialProfile -ProfileName $Script:ProfileNameVaule -Force
#EndRegion to Deploy Ubuntu and install NGINX and ASP.NET Core