Skip to content

Instantly share code, notes, and snippets.

@i-e-b
Created March 11, 2020 11:39
Show Gist options
  • Save i-e-b/979f18580f442cc0129171c3c45e3822 to your computer and use it in GitHub Desktop.
Save i-e-b/979f18580f442cc0129171c3c45e3822 to your computer and use it in GitHub Desktop.
Setup dotnet core 3.1 website on AWS EC2 instance, using S3 bucket for storage

Put your software in an S3 bucket

Publish your web app, check it works locally. Zip the files into a single archive.

Go to AWS console, and S3 buckets. Create a bucket for your compiled app artefacts.

Upload your zip file.

Create an instance

Go to AWS console, and EC2 instances. Start a new instance with:

  • An AMI Linux image
  • SSL open
  • HTTP port open
  • A public IP address Download the access key .pem file. Keep it safe. Make sure the IAM role includes READ and LIST.

Attaching to SSH from Windows

Connecting ( https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html ) On Windows PC, download and install PuTTY ( http://www.chiark.greenend.org.uk/~sgtatham/putty/ )

Use "PuTTYgen" to convert the .pem file to a .ppk file (as a copy) to use with PuTTY Open PuTTY desktop client; Go to the EC2 instance dashboard in browser to get VM details.

Copy EC2 "Public DNS (IPv4)" value into Session:Host Name Go to PuTTY Connection/SSH/Auth and click "Browse..." button under "Private key file for authentication" Select the .ppk file you converted earlier. Go back to Session, save your profile and press "Open" button at the bottom of the window.

On first connection, you will be asked to trust the server's fingerprint. Choose "Yes" You should get a prompt login as Type "ec2-user" and press enter. You should see 'Authenticating with public key "imported-openssh-key"' and end up in a Bash session.

Note: Copy-and-paste in PuTTY is unusual in the Windows world. You copy text by selecting it with the mouse (ANY time you select anything, it's copied) You paste text by right-clicking. So to enter the samples below, copy the from your text editor as normal, then right-click in the PuTTY console window.

Install Dotnet Core on the EC2 Image

Check yum is installed with which yum. This should return something like /usr/bin/yum

Enter these CASE SENSITIVE commands (beware PuTTY's weird copy-and-paste support -- ask someone if you've never used it before)

sudo yum -y update
sudo yum -y install libunwind libicu
curl -sSL -o dotnet.tar.gz https://download.visualstudio.microsoft.com/download/pr/2d72ee67-ac4d-42c6-97d9-a26a28201fc8/977ad14b99b6ed03dcefd6655789e43a/aspnetcore-runtime-3.1.2-linux-x64.tar.gz
sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
sudo ln -s /opt/dotnet/dotnet /usr/bin

If any of those fail, show someone who knows Linux well, or ask Professor Google. If it was all successful, test by typing

dotnet --list-runtimes

You should see something like:

Microsoft.AspNetCore.App 3.1.2 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.2 [/opt/dotnet/shared/Microsoft.NETCore.App]

Pull down your app hosted on S3

Create a directory to store your app:

sudo mkdir -p /var/aspnetcoreapps
cd /var/aspnetcoreapps

Pull down the S3 package. If you get errors like "fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden", go back and add the S3 permissions to your IAM role.

sudo aws s3 cp s3://{yourbucketname}/yourappname.zip .
sudo unzip {yourappname}.zip
cd {yourappname}

Test that your app can run in this environment (substitute .exe for .dll if required):

sudo dotnet {yourappname}.dll

If that doesn't work, you need to troubleshoot the issues. If you get "Now listening on: http://localhost:5000" or similar, it worked. Press ^C to exit.

Proxy with Nginx

Nginx will expose our app to the big bad internet with a reverse proxy. Install with

sudo amazon-linux-extras install nginx1.12

Edit the config. (substitute nano for vim if you want extra credit) Note: AMI Linux stores the config in an odd place.

sudo nano /etc/nginx/nginx.conf

Find the section http/server/location Insert contents so it looks like:

location / {
  proxy_http_version 1.1;
  proxy_set_header Connection keep-alive;
  proxy_cache_bypass $http_upgrade;
  proxy_set_header Host $host;
  proxy_set_header Upgrade $http_upgrade;
  proxy_pass http://localhost:5000;
}

Save and exit the editor. Start Nginx service:

sudo service nginx start

Check that worked with netstat -nael | grep 0.0.0.0:80 That should return an output line containing "LISTEN" Run your app again -- sudo dotnet {yourappname}.dll

Check it worked

Try to connect over the internet, using your public DNS name. If that works, you can disconnect your SSH session. Otherwise troubleshoot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment