Aws Lambda Functions in Node with Claudia

AWS-Lambda

Aws_Lambda_Function_Git_Repo

Serverless is the next frontier it seems and there are a plethora of ways of transitioning your app from managed on site to completely serverless. There are many advantages to serverless computing. Some of my favorites are

  • AutoScaling
  • Only Pay For what you use
  • Abstraction away from managed resources(you dont configure servers)
  • Low latency
  • Ease of deployment

The big players in the serverless space are Amazon with Lambda, Microsoft with Azure Functions, and Google with Cloud functions. All of them have their strengths with AWS being the most mature. Today i started looking into Claudia.js which allows you to deploy your lambda function from the terminal. I created a very simple api and was able to deploy in minutes. The caveat to this is that you have an IAM role and credential file on your machine.  You start by installing claudia

npm install claudia -g
Init a new node project in my case i created a pizza ordering api . You then run
claudia create --region us-east-1 --api-module api
Thats pretty much it. Honestly, getting everything setup beforehand was the most challenging bit of all this. Once you create claudia comes back with your url for the aws api gateway and you can start making requests.
Advertisements

Containers! Run a sql database on a droplet

dockerdi

Containers are all the rage at the moment and frankly i get it. You never have to worry about a mismatch between development and prod. You can containerize just about any type of application including asp.net using windows containers. Today we will cover running an mssql database on a digital ocean droplet. Digital Ocean is a very cost effective and easy to run cloud provider. They offer many different templates for one click apps including a droplet that includes Docker . Docker is the main player in the container space and has set the standard for creating containers. Containers are sort of an abstracted version of a virtual machine.

Blog.-Are-containers-..VM-Image-1

So, what we are going to do is run the containerized image of sql server on a digital ocean droplet.

Step 1) Create an account on digital ocean and create a droplet. The caveat here is that the droplet must have at least 4gb of ram because thats what sql needs. This eliminates using the most cost effective option of $5/month. The one we need is $20/month.

Step 2) Click the create button on the top right of the screen and select One-click apps

oneclick.PNG

Step 3) Choose your droplet size, name,  and create!

Once that process is done the fun begins!

You can SSH into your new machine. I personally use the console. You’ll get an email with your root password and will be prompted to create a new password on log in.

First thing we want to do is pull the latest docker image for sql server from the microsoft docker hub  run

docker pull mcr.microsoft.com/mssql/server

This will pull the latest docker image into your machine. You then want to run the docker run command.

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest

This will start your container and expose sql server on port 1433. Remember to set your password to something compliant with sql server. Thats it! If you run

docker ps -a

You should see your running container!

docker-ps-command

After that you can do all sorts of stuff. Mainly using SSMS to log into your sql instance and add a database. The address will be the IP of your droplet XXX.XXX.XX.25,1433

The username is SA and the password is the strong password you created.

Have Fun!

dbcon

 

Download a zip folder from netcore web api using Axios

 

This will be a quick post that covers how to return a zip folder from a web api.

This is what the method should look like in your controller.

apizipFolder

I am using the axios npm package to handle my http request in a vue app currently. If i am not mistaken axios can be used in Angular,React, and Vue applications.

You would add an import axios declaration to your file and your method to download the zip would look like this.

axiosget

That’s it! Attach that method to a button on click or however you like and you’re done!

Unit testing web apis using Javascript

2gwaf4

Unit testing can be an issue of contention for some developers. I am of the camp that we should definitely be unit testing as much as possible. Some devs believe that every method should have a unit test and some devs are all about TDD. In my opinion one of the pain points of unit testing is not exactly writing the tests themselves but more wiring up the dependencies. What if you’re testing controllers or classes that have big constructors? Well then you have to mock up all of those dependencies. Admittedly that work on the front end does pay dividends but it can be a pain. What if I told you there was another way to test your api’s that does not involve mocking dependencies? Recently while collaborating on a project i was introduced to unit testing using mocha.js

mocha

I had used karma and jasmine in the past for front end testing an angular app so i was familiar with writing tests in javascript. It had never crossed my mind that i could write calls and furthermore tests for my api using javascript! No dependencies to wire up! Recently i had released an api to AWS that can be found here. I decided to write some tests against that. Here are the steps that i took….I already had nodejs installed on my machine.

  1. Created a folder for my files
  2. Added a package.json file packagejson
  3. Then i opened the console to that directory and ran the “npm install” command that installs all of the node modules.
  4. I then created my unit tests file tests.specs.js testsspec
  5. The last step was running the tests for that again in the terminal i ran the command “npm run server’ and

testspass

all my tests have passed!

Git Repo HERE! happy coding.

 

.Net Core Web Api With Swagger

download

**Update 08/26 I deployed a version of this to AWS using the VSTS build process find it HERE

 

I wanted to illustrate how fast developing an api, and adding swagger to it is using dotnet core.

First thing i do is create a new webapi project using the .netcore CLI.

dotnet new web api

Once that’s done I run.

powershellcommand

This adds Swagger to my project. What is Swagger? Swagger automagically documents your api for you. Seeing as this is a simple REST api it doesnt have a front end of any kind nor do i want it to. Enter swagger. To add Swagger to my project I go to the Startup.cs file and enter a few lines of code.

startup

Thats it! Now, when i run my api and navigate to index.html i get this

swaggerindex

Pretty simple! My routes are documented and i can even test them on the browser!

testonbrowser

Swagger uses curl and makes the call for you.

I deployed this to azure using the new Kudu release process that is tied into my instance of visual studio online. Thats a topic for another time. Happy Coding!

Feel free to check out my Api that is on azure.

https://netcoreapiswagger.azurewebsites.net/index.html

An entire world exists outside of eF?

This is news to me. It really shouldn’t be this surprising. We do after all have document Db’s becoming all the rage nowadays. Today i learned about a neat micro ORM named Dapper.  Dapper is what is used in production to run StackOverflow. It is a million times faster than Entity Framework and only slightly slower than a hand coded query.

performanceorm

This is all really interesting but is it for me? The answer… It depends. In my opinion if you are happy with Entity Framework and the repository pattern then i would say no. If you are using ADO.Net in a legacy application then dapper may be something to look into. In any case its a neat product to learn about and i hope to create a project using it here soon. Recently i followed this tutorial which allowed me to use a database first approach in efcore. If anyone is using it and enjoying it i would like to hear more in the comments. Cheers.

dotPeek Does it Again!

dotpeek_1280x800

 

If I’ve said it once I’ve said it a thousand times. I want a jet brain! The problem I faced recently was that a long running automated process stopped working out of nowhere. It is a project that communicates to a third party API. We had abstracted the majority of what the process does into an external library and then converted that into a nuget package. We found that running the project would not let us look into where we were experiencing our exception. Meaning it was in the dll of the library that was a nuget package. Visual Studio 2017 allows you to debug nuget packages and Dlls using a symbol server. You can debug from TeamCity, VSTS, or a local symbol server. Enter dotPeek. I found this solution at StackOverflow btw. The detailed instructions are here thanks to user anthonybell 

I simply started the dotPeek symbol server and was able to walk directly into the exception inside visual studio. It turned out to be some stupid TLS thing fyi. Once again jet brains saved my day!

Quick File Rename dotnet core

I wanted to rename all the files in a directory and remove the .component in each file in a directory. I could do this manually…I am after all a caveman. StackOverflow had a better solution for me.

replace component

 

So I opened my console window

dotnetnew

asked the .netcore cli to create a new console app which took all of 2 seconds

newconsoledone

 

Then i added i ran my tiny program and it took less time than writing this post!

Try it out!

 

 

I want a Jet Brain!

jet_brain

I learned something really neat today. In my day to day i dont really need to decompose EXE’s or DLL’s for any reason. Well today I had to because i found that an application running on one of our servers did not match the code that we had in source control. In source control the code did not work but we know that the application works because it has ran faithfully and silently for years now.

How in the world would i be able to see what the difference in our code is from the code in production? Enter JetBrains dotPeek ! This is a free decompiler that allowed me to look into the executable file thats installed in the server! All i did was install dotpeek –> File –> Open –>myapplication.exe and voila!

decompiler_overview

This literally shows me the classes and methods that are being used and i was able to find where source control was wrong and fix our project!

Thanks JetBrains!

How to make MVC and Angular Apps 2 ways.

MVC_Bootstrap_AngularJS

 

Let me start by saying that first it can and is being done. I was at a Microsoft conference and on the Microsoft campus when a speaker told me that it couldn’t be done earlier this year….Yeah…he was wrong. The first and easiest way to accomplish this feat is to use the .NetCoreCli . Here, all you have to do is say ‘dotnet new angular’ and then BAM you’ve got yourself a brand new shiny dotnetcore MVC app with Angular baked in. Oh it so easy it hurts! What if I have an existing MVC5 app that i want to add Angular to because I enjoy pain and pain accessories? Well then i found a cool little guide for you HERE. That is not to say these are the best way of doing things. These are the ways that i found fit my particular needs. Prior to discovering this way i had found that you can also use the Angular cli to create an angular and dotnet project as well in the same directory here are the steps.

  1. Create a new project using the Angular Cli ‘ng new mydotnetproject’
  2. cd dotnetproject
  3. npm install –save-dev webpack@2.3.2 aspnet-prerendering@2.0.3 aspnet-webpack@1.0.28 webpack-hot-middleware@2.17.1
  4. ng eject
  5. npm install
  6. (In same directory) dotnet new mvc –language C#
  7. dotnet add package Microsoft.AspNetCore.SpaServices
  8. add a boot.ts file in your app folder
  9. Enable MiddleWare in your Startup.cs file
  10. edit your webpack.config.js file
  11. dotnet run
  12. Boom! ya done son!

Its a brave new world out there! The advantage here is that this might/could make your front end easier to manage and you still have the advantage of using entity framework and Linq in your projects. To me its a win win and a happy marriage. Sound off in the comments. Hasta la Pasta!

I almost forgot! Here’s a working repo! Follow the steps in the readme and you should be good to go.

https://github.com/Domt301/ng2Mvc5Demo