featuredimage

HOWTO Build an ASP.NET CORE Web App on AWS Running Linux with DynamoDB – Part 2

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.

  1. Login to the AWS Console if you are not already.
  2. Select S3 from the main menu then create new bucket or you can use an existing one.end1
  3. Select you bucket and upload the zipped application you created in Part 1. It should look something like this this once it’s uploaded.end2

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.

  1. From the AWS console select EC2 from the main menu and click Launch Instance.
  2. Choose a free tier Amazon Linux AMI.
  3. Select Next Configure Instance Details.
  4. Leave everything as default except change the IAM Role to the one you created in Part 1 of this post.
  5. Select View and Launch.
  6. Under security groups select “Edit” security groups then Add Rule. Add a new HTTP rule.end3
  7. 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:

end4

  1. Update the systems and install ASP.NET Core 1.1. Copy and Paste the following commands in to the Linux console.
  2. Test by typing dotnet –version. You should see something like this:end8
  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>.dllend5

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:

    1. sudo yum install nginx -y
    2. 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. 
    3. Using your favorite text editor edit /etc/nginx/nginx.conf
    4. sudo nano /etc/nginx/nginx.conf
    5. Arrow down until to find the section called “location” and add the following until your configuration looks like this:
    6. When your do hit CTRL+X then Y to save and <enter> to exit.

Step 5. Start the Reverse Proxy and Your Application

  1. sudo service nginx start
  2. 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
  3. Now you can re-launch your dotnet app
  4. cd /var/aspnetcoreapp/<yourappname>
  5. 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

end9

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.

 

 

Leave a Reply