Migrating to TypeScript from ES2015 for AWS Lambda

Posted by on Oct 12, 2016 in AWS, Cloud, JavaScript

I was recently building an skill for the Amazon Alexa platform (think Amazon Echo, Fire tablets, Echo Tap etc). Like anyone writing JavaScript today I naturally levitated toward some of the syntactic goodness that ES2015 offers – arrow functions, let variable scoping, string literal templating, default parameters etc all of which make JavaScript a much, much nicer language (and much, much closer to C# incidentally which is where the language seems to be heading long-term looking at current and new functionality. Alas, I digress!). I had the skill up and running locally (bridged via an Express.js node front end for testing) and everything was working well. Then it came the time to upload my skill to the amazon developer portal and migrate my function to AWS Lambda. This is where the fun started – it turns out Lambda uses a fairly dated version of Node.js that doesn’t support much of the ES2015 specification. This left me faced with two options – strip out all the ES2015 code and rewrite using vanilla JavaScript or make use of our friend, Typescript. To be honest I should have gone down the TypeScript route from the offset; in hindsight I’m not sure why anyone would write straight JavaScript nowadays when the TypeScript transpiler does such a great job of both supporting new functionality and also outputting compliant ES5 code. The task of migrating from JavaScript was as easy as 1-2-3: Rename all my .js files to .ts Install tsc from the command line (npm install –g tsc) and create my tsconfig.json with the list of files in my project Run typescript in watch mode (tsc –w) to get it to watch for changes to save (my lazy hands) having to recompile manually Once this was done I was free to do nothing more and I already had fully “Lambda compliant” JavaScript that I could use. However, the TypeScript compiler can do so much more for me, so I spent a little time going through my code adding in types to all my parameters, created an object interface so TS knew the type of data I was passing around on properties on the object (this also greatly improved Intellisense in VS Code I might add) and pulled down some typings for the third party libraries I was using. Migration was such a smooth and painless experience and I’m now in a much better place than I was prior to the conversion. I have compile time type checking, I get to use the much more elegant and succinct ES2015 syntax and I have a much better development environment through the “knowledge” TypeScript now has about my...

Read More »

Auto starting and stopping an EC2 instance at a given time

Posted by on Jan 13, 2014 in AWS, Cloud

This blog post covers the process of automatically starting and stopping an EC2 instance at a given point in time. In my case I needed to spin up an instance, do some work and then shut it down afterwards. This is perfectly suited to AWS and cloud computing and typifies the ethos of elastic scaling and capacity-on-demand. In order to start/stop an instance you will need to make use of the AWS Auto Scaling capabilities which are both straight-forward but very, very powerful. The first thing that you’ll need to do is set up the Auto scaling tools. Amazon have an excellent step-by-step guide posted here If done correctly you should be able to open a new command prompt (or powershell terminal) and type as-cmd 1 as-cmd You should get a listing of all auto-scaling commands: Command Name Description ------------ ----------- as-attach-instances Attaches Instances to Auto Scaling group as-create-auto-scaling-group Create a new Auto Scaling group. as-create-launch-config Creates a new launch configuration. as-create-or-update-tags Create or update tags. as-delete-auto-scaling-group Deletes the specified Auto Scaling group. as-delete-launch-config Deletes the specified launch configuration. as-delete-notification-configuration Deletes the specified notification configuration. as-delete-policy Deletes the specified policy. as-delete-scheduled-action Deletes the specified scheduled action. as-delete-tags Delete the specified tags as-describe-account-limits Describes limits for the account. as-describe-adjustment-types Describes all policy adjustment types. as-describe-auto-scaling-groups Describes the specified Auto Scaling groups. as-describe-auto-scaling-instances Describes the specified Auto Scaling instances. as-describe-auto-scaling-notification-types Describes all Auto Scaling notification types. as-describe-launch-configs Describes the specified launch configurations. as-describe-metric-collection-types Describes all metric colle... metric granularity types. as-describe-notification-configurations Describes all notification...given Auto Scaling groups. as-describe-policies Describes the specified policies. as-describe-process-types Describes all Auto Scaling process types. as-describe-scaling-activities Describes a set of activit...ties belonging to a group. as-describe-scheduled-actions Describes the specified scheduled actions. as-describe-tags Describes tags as-describe-termination-policy-types Describes all Auto Scaling termination policy types. as-disable-metrics-collection Disables collection of Auto Scaling group metrics. as-enable-metrics-collection Enables collection of Auto Scaling group metrics. as-execute-policy Executes the specified policy. as-put-notification-configuration Creates or replaces notifi...or the Auto Scaling group. as-put-scaling-policy Creates or updates an Auto Scaling policy. as-put-scheduled-update-group-action Creates or updates a scheduled update group action. as-resume-processes Resumes all suspended Auto... given Auto Scaling group. as-set-desired-capacity Sets the desired capacity of the Auto Scaling group. as-set-instance-health Sets the health of the instance. as-suspend-processes Suspends all Auto Scaling ... given Auto Scaling group. as-terminate-instance-in-auto-scaling-group Terminates a given instance. as-update-auto-scaling-group Updates the specified Auto Scaling group. help version Prints the version of the CLI tool and the API. For help on a specific command, type ' --help' Getting started Now you can start implementing auto scaling. Auto scaling on a schedule requires a number of components which form the what, where and when to scale: 1. A launch configuration (the ‘what’) 2. A scaling group (the ‘where’) 3. A schedule policy (the ‘when’) 1. [WHAT] Create the launch configuration From your command prompt enter the following command: as-create-launch-config "screenshotter-launch-config" --image-id "ami-12345678" --instance-type "m1.medium" 1 as-create-launch-config "screenshotter-launch-config"...

Read More »

IAM: Best practices

Posted by on Dec 2, 2013 in AWS, Cloud

The more I play with (and love) AWS as a platform, the more the significance and power of IAM becomes. This post outlines IAM and how it should be used effectively within Amazon’s cloud environment. What is IAM? IAM, or Identity and Access Management is the primary means of securing users, groups and permissions. IAM is complimentary to services such as security groups and Access Control Lists (which govern Instance and Subnet security respectively). Best Practices Amazon strongly recommends (and I completely agree) that the root/master account should not be used for anything other than administering the Amazon account and creating administrative accounts. Thereafter you should be logged in as a named user (if the master account is a company account) with the minimum required privileges to do your job. With great power comes great responsibility, and its quite easy with AWS to inadvertently terminate the wrong instance (thus bringing a production server offline) or incorrectly route traffic to the wrong subnet for example. Therefore you should take the time to scope out what level of access each user requires, what functions they need to be able to perform to do their job and then match this up with the IAM policy generation tools. Amazon have done a fantastic job of giving granular access to the services and its contained functions – to the point where you can permit a user to reboot a server but not terminate it, or retrieve content from S3 but not upload (or vice-versa).Thus, as part of your getting started with IAM you should navigate to the IAM section of AWS and set up user(s) pertaining to the role they need to perform Another best practice is that if a user does not need API access, do not generate the key(s) necessary to enable it. Sure, they’re inherently obscure but why risk it? Oddly enough Amazon defaults the ‘Generate and access key for each user’ checkbox to ticked, so unless you explicitly disable this they will be created. Conversely, if a user ONLY needs API access and doesn’t need to access the console, then do not generate an IAM password that would allow them to log in to the AWS console. By default new accounts do not have a login password, and thus you control who has access to the AWS console. As with all security in IT, the least required privileges, the better! IAM roles are a relatively new addition to the IAM offering. Roles allow you to assign effective permissions to a particular role as you would a user or group, but assign this to an EC2 instance. Why would you do this? Well, prior to roles, developers would have to embed API keys & secrets in their code or user-data (or use some other mechanism for getting credentials onto an instance) in order to permit it to access another AWS service...

Read More »

Assigning a fixed IP address to an EC2 instance

Posted by on Sep 22, 2013 in AWS

Amazon, being the kind-hearted souls that they are allow you five static/fixed IP addresses completely free. Gratis. No charge. Nada. This is handy if, like me, you often play with EC2 isntances but only power them up when you need them, but still want to reference them at the same address each time. Normally, AWS assigns a new public DNS and private IP/DNS to each instance meaning it can be hard to reference a particular machine externally (think config files or even RDP stored credentials). Luckily, Amazon have made it trivially easy to get a fixed IP address To allocate a new IP address to an already running EC2 instance, perform the following steps: Log in to your AWS account and go to the EC2 services panel Under network & security select Elastic IPs Click Allocate New Address in the command bar Select EC2 from the drop down in the resultant popup box Right-click on the new IP address and click associate (alternatively you can use the Associate Address button in the command bar Select your EC2 instance in the resultant popup and click Yes, Associate You can now refer to that instance using a fixed IP address and not have to worry about its public DNS or private IP address changing. It’s worth noting that these addresses are free only when they are assigned to an instance. When they’re not, you will get charged a small amount, but their usefulness far outweighs the costs IMO. When you’re done with the IP, disassciate the IP address from the instance, then click the Release address button in the command...

Read More »