In Part 1 you learned how to get Visual Studio 2015 setup and connecting to Amazon AWS DynamoDB NoSQL database with CRUD Operations. Part 2 will continue where we left off and push the application to the AWS cloud by creating an Amazon Linux EC2 instance and prepare the OS to support DOT.NET Core app plus a few features to make it run better.
Step 1. Push the application created in Part 1 to AWS S3 Bucket.
- Login to the AWS Console if you are not already.
- Select S3 from the main menu then create new bucket or you can use an existing one.
- Select you bucket and upload the zipped application you created in Part 1. It should look something like this this once it’s uploaded.
Step 2. Create an EC2 Instance
This is a well-documented process so I will just touch on key points to get the app running.
- From the AWS console select EC2 from the main menu and click Launch Instance.
- Choose a free tier Amazon Linux AMI.
- Select Next Configure Instance Details.
- Leave everything as default except change the IAM Role to the one you created in Part 1 of this post.
- Select View and Launch.
- Under security groups select “Edit” security groups then Add Rule. Add a new HTTP rule.
- Review and Launch” then “Launch”.
Step 3. Configure your new EC2 instance.
You will need command line access to the EC2 Instance. As before this step is well-documented. Here are detail for windows users.
Once you logged your screen should look something like this:
1. Update the systems and install ASP.NET Core 1.1. Copy and Paste the following commands in to the Linux console.
sudo yum -y update sudo yum -y install libunwind libicu curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=835016 sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet sudo ln -s /opt/dotnet/dotnet /usr/bin
2. Test by typing dotnet –version. You should see something like this:
3. Create a directory to unzip your app into:
cd /var/aspnetcoreapps
sudo mkdir /var/aspnetcoreapps
4. Now pull down the application you pushed earlier from Amazon S3. Note: This can only be accomplished if you had added S3 permissions to the IAM Role in Part 1 so if for some reason you are not able to access the zip file go back to IAM and update the role you attached to this instance.
5. sudo aws s3 cp s3://<yourbucketname>/yourappname.zip
6. sudo unzip <yourappname>.zip
7. cd <yourappname>
8. sudo dotnet <yourappname>.dll
As you see the Kestrel web server is listening on the localhost port 5000.
At this point you will not be able to connect from the internet so as per Microsoft’s documentation you need a reverse proxy server prior to connecting to the localhost.
Type Ctrl+C to stop the application from the command line if you haven’t done so.
Step 4. Create a Reverse Proxy
We will use the open source project Nginx as a reverse proxy. To Install Type:
- sudo yum install nginx -y
- On success update the nignx config to proxy connections to localhost:5000. Since we are using Amazon Linux the configuration file is a little different then the documentation provided by Microsoft.
- Using your favorite text editor edit /etc/nginx/nginx.conf
- sudo nano /etc/nginx/nginx.conf
- Arrow down until to find the section called “location” and add the following until your configuration looks like this:
- 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; }
- When your do hit CTRL+X then Y to save and <enter> to exit.
Step 5. Start the Reverse Proxy and Your Application
- sudo service nginx start
- Verify nginx is listening on port 80
netstat -nael |grep LIST You should something like:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 9812 - Now you can re-launch your dotnet app
- cd /var/aspnetcoreapp/<yourappname>
- sudo <yourappname>.dll
As before you should see dotnet kestrel web server listing on port 500. Nginx is listening on port 80 and will forward requests to port 500. nginx(80)->kestrel(500)
Final Step. Verify the Application responds from the Internet
Open a browser and type the public dns name you captured above when connecting with putty i.e.:
http://ec2-54-85-99-91.compute-1.amazonaws.com
You should see the same APS.NET default splash page you did from Part 1.
Add the /Books to the URL like:
http://ec2-54-85-99-91.compute-1.amazonaws.com/Books
Conclusion
In the Part 1 and Part 2 of “HOWTO Build an ASP.NET CORE Web App on AWS Running Linux with DynamoDB” I hope I have shown you some of the flexibility of running ASP.NET apps in an uncommon environment. In Part 3 we will automate the installation using AWS OpsWorks Configuration Management Service with custom Chef Cookbooks. Part 4 will utilize the AWS Elastic Beanstalk service as an alternative to OpsWorks.