AWS ElasticBeanstalk & Encrypting Sections

I recently struggled with properly deploying a web application on ElasticBeanstalk, and became frustrated at the fact that I wasn't able to encrypt the configuration file after deployment because someone at AWS decided that injecting their own parameters into appSettings at deploy time was a 'stellar' idea.  After being told by AWS Support that this wasn't possible other than egregious solutions that I simply didn't want to bother with, I finally found a way around it with .ebextensions and beanstalk hooks:

  • Create a folder under your web solution named '.ebextensions' (include the leading period)

  • In the contents of this file, simply place the following:

files:
"C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\00_encrypt-strings.ps1":
content: |
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis.exe -pe "connectionStrings" -app "/"
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis.exe -pe "customSettings" -app "/"

"C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\99_cleanup.ps1":
content: |
If (Test-Path "C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\00_encrypt-strings.ps1") {
Remove-Item "C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\00_encrypt-strings.ps1"
}
If (Test-Path "C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\99_cleanup.ps1") {
Remove-Item "C:\\Program Files\\Amazon\\ElasticBeanstalk\\hooks\\appdeploy\\post\\99_cleanup.ps1"
}

That's pretty much it!  This makes use of hidden 'hooks' within the beanstalk framework that allows you to inject postscript commands into the beanstalk workflow.  Whatever you do, don't bother with the different ebextension commands or container_commands, as that will just end up pissing you off.

Happy coding.

Explaining Hello World

using System;

namespace HelloWorld {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello World");
            Console.ReadLine();
        }
    }
}

So let's walk through the semantics of what the code above tells the compiler, and more importantly the Operating System, to do:

using System:  This tells the compiler that the code below will reference the named assembly or library.  This allows you to use code previously written either by yourself, or someone else.  There is an entire framework of assemblies with solid code for you to build on - at the time of this writing, you can find the documentation here.

namespace HelloWorld:  Namespaces are ways to organize your code, and control name conflicts.  As your own code library grows, you will want to organize it in logical ways so it's easy to find, such as '<Your Name>.Net', '<Your Name>.Core', which refers to the type of software that lives in each area.  This will make it easier to find your re-usable code later.

class Program:  Classes organize attributes and behavior.  You can think of classes generally as actors or nouns, although that's not always the case.  However, in many cases you will be organizing your code based upon that assumption, such as Person, Location, Car, etc. so that actions and attributes of each type of object are easily found.  You can then create instances of these classes to represent an actual thing, such as "John - a Person, who has brown hair and blue eyes".  We will get further into this later. 

static void Main:  This is a method of the Program class that acts as the entry point for our application that the Operating System will use.  This can take a series of arguments, but generally speaking, when you double-click on an application to run it, this is what gets called first by the Operating System.

Console.WriteLine:  Tells the Operating System to write out the content to the console window.

Console.ReadLine:  Tells the Operating System to wait for someone at the keyboard (a user) to enter in content.  With ReadLine, the application will wait until someone presses the Enter key.

Lastly, you will notice plenty of open { and close } braces.  These instruct the compiler as to the scope of the namespace, class, or method.  Parenthesis (e.g. Main(string[] args) on a method define what is passed from outside of the method into the scope of the method.  Generally speaking:

  • Classes MUST be contained within the scope of a Namespace
  • Methods MUST be contained within the scope of a Class
  • Parameters MUST be contained within the scope of the Method parenthesis

We will also deal later with Class Fields, Class Properties, and Method Fields, but let's not get too far ahead.  Although HelloWorld is a simple example, understanding the basic rules of how to construct Namespaces and Classes will help you to create extremely complex applications without losing your mind.  These concepts are not so important now, but certainly will be later.

Hello World

Probably the classic first application almost everyone has written (and still write when they are learning a new environment or programming language), is Hello World.  The point is to simply validate that you have the environment setup properly, and that you understand the basic language syntax and compiler well enough to get something that will RUN.  Remember, that we are using the C# (C-Sharp) programming language for our examples, however you can easily translate these into other programming languages, as the same rules apply across all programming languages, merely the syntax of how those rules are communicated to the compiler are different.  Simply follow these steps to create your first application:

  1. If your Visual Studio development environment isn't already running, please start it.
  2. Click on File > New > Project, or simply click on the 'New Project' toolbar button.
  3. On the left-hand side of the new project dialog, make sure you choose Templates > Visual C# > Windows, and in the primary dialog area, choose Console Application.  Give the project a name of 'HelloWorld' and click OK.

This should create your project, and in your development environment you should see the basic code necessary to have your application run.  Change the code in your Program.cs file to match the following:

using System;

namespace HelloWorld {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello World");
            Console.ReadLine();
        }
    }
}

Now, click on Debug > Start Debugging, or on the > Start button on the main toolbar.  At this point a black console window should appear with the text 'Hello World' in it - by pressing the Enter key, the window will close.

Congratulations, you have completed your first Windows application!  It's quite simple, and if you are anything like my clones, you will have some fun changing the text to say anything you want, like 'Where are my pants?'.

So what's important here?  Generally, just remember that Methods (e.g. Main) live inside a Class (e.g. Program), Classes live inside Namespaces (e.g. HelloWorld), and braces define what's inside and outside (e.g. { inside } ).  I will put in another post much greater detail regarding what exactly each line does, and why it's important, but that's only for those who want to know.  :)

Until next time!

Download the HelloWorld project & solution used here

Getting started

There are lots of different ways you can teach someone to code - and you don't need fancy tools in order to do it.  I have done development on many platforms, starting with basic on an Apple IIe, then on to Unix, Linux, Windows, Mac, and some random platforms in between.  I an going to teach my clones to do dev on a Windows platform, as there are plenty of tools available, it's plenty open, and we can go pretty deep into messing up the system through code.  If you don't have a Windows machine, there are plenty of tools out there for other systems, but you won't find them here.  :)

On Windows, I like development with Visual Studio (www.visualstudioonline.com), and there is a free community version for download that is perfect for getting started.  At the time of this writing I hadn't played with the 'Code' version, which is available on multiple platforms to create Cloud-based solutions.  So, for the sake of getting started, we will be using the Community Edition, and will focus on the C# programming language.  It's friendly, pulled in many of the lessons learned through the Java platform, and can be picked up on relatively quickly once you have established a few simple syntax rules.

If you are a purist, you can teach classic Unix-style development on Windows through an application called Cygwin (www.cygwin.com).  This isn't just a text editor, but more-so an environment that includes many tools such as ssh to allow you to interact with multiple platforms, and a VI editor the same way you would find classically on Unix.  You will however need a compiler depending on what language you want to teach, and the bonus (or not) here is that there is no 'intellisense' - you have to learn not just the language syntax, but also the libraries.  For those who choose to venture down this path, you will simply need to download the C# compiler, and all of the work we are doing will still work for you... at least until we migrate to leveraging the integrated tools into deploying cloud and mobile solutions.  :)

Now that we've established the environment, let's get started.

In the beginning...

By a stroke of luck, I've owned this domain for almost two decades now, and my always supportive wife has convinced me to start sharing a little bit of what I know before it's too late - either due to old age, or simply losing the vast majority of my marbles and not being able to find them.  As part of this journey, I've embarked on a few things:  started a business, started doing more freelance work to keep things interesting, and transferring what little I have learned to my clones so that they can be better than I was.

On this site you are going to see a few things - some 'heads up' business related material, some material on freelance work, and also material on what I call 'Raising a Technologist'.  Part of my passion in life is technology, as it's always come easy for me, so this category will focus on what I am teaching my clones.  Use it or not, it's what I am teaching them, when we have time between moments in life.

Enjoy, be kind, and be well.