Taswar Bhatti
The synonyms of software simplicity
aws

I needed to increase my disk size of my Windows 2016 server on AWS EC2. I though I will share this just in case it can help you or myself in the future on How to increase the disk size of a Windows EC2 machine?. First thing first you need to increase the volume space that you have, easiest way is to use the aws console.

Below I have logged in and am on the windows ec2 machine and have clicked on storage tab to find the volume I wish to increase.

Note: You can only increase there is no decrease

EC2-Stoage-Tab

EC2-Stoage-Tab

Click on the storage volume link and it will take you to the storage section on the console. Right click and choose Modify Volume.
A window will show up like below, enter the size of the volume you wish to increase it into.

EC2-Modfy-Volume

EC2-Modfy-Volume

Once successful you will see the message like below.

EC2-Modify-Volume-Success

EC2-Modify-Volume-Success

At this point you might be thinking you have completed and it will automagically increase your size. Unfortunately that is not the case, and you will be required to restart your instance and Remote desktop into it to increase it inside of windows also.

We will need to restart the instance and once restarted let us check the disk size.
As it states it is still 300G, we have changed it to 400G but nothing has changed. What the hell???

EC2-Windows-Disksize

EC2-Windows-Disksize

We will need to launch disk manager (diskmgmt.msc) and we will see that there is 100G that is not allocated yet.

EC2-Unallocated-Space

EC2-Unallocated-Space

We will need to right click on our existing volume and choose extend volume like below.

EC2-Extend-Volume

EC2-Extend-Volume

It will bring up a wizard where you click next, next and it will increase your volume like below.

EC2-Final-Result-Extended-Volume

EC2-Final-Result-Extended-Volume

Summary

I hope this helped in showing how to increase the volume size for your windows server running on EC2.

aws-dotnetcore-lambda

If you are using AWS S3 C# TransferUtilityUploadRequest and when you try to upload objects onto S3 you can potentially get Access Denied. The reason could your IAM Role is not defined to have access or your bucket name is incorrect.

What do you mean bucket name is incorrect? Basically it boils down to that buckets used with Amazon S3 Transfer Acceleration can’t have dots (.) in their names. So if you created a bucket with name e.g “my.fancy.bucket.” this will not work with TransferUtilityUploadRequest. You will need to change the name to “my-fancy-bucket” rather.

Example below I am using this code to upload some excel data.

Now if you change the code to use dash for the bucket name it will succeed in uploading, make sure you do have a bucket with that name already created.

Make sure to check the Role given to the lambda function also, use least privileges for s3 if possible to and if you are logging any information you need to give the Cloudwatch permission to your lambda also.

Hope this helps 🙂

aws-dotnetcore-lambda

If you are working on AWS Lambda Dotnet with C# and find out that you are getting something like an error like below.

Error message
Could not find the specified handler assembly with the file name ‘LambdaTest, Culture=neutral, PublicKeyToken=null’. The assembly should be located in the root of your uploaded .zip file.: LambdaException
[WARN] (invoke@invoke.c:331 errno: No such file or directory) run_dotnet(dotnet_path, &args) failed
START RequestId: b7bb069b-3f44-4cd6-8b63-43a37098cd5e Version: $LATEST
Could not find the specified handler assembly with the file name ‘LambdaTest, Culture=neutral, PublicKeyToken=null’. The assembly should be located in the root of your uploaded .zip file.: LambdaException

The main reason is that you most likely created the lambda on the console first and then tried to upload the code using dotnet cli e.g dotnet lambda deploy-function “functionName”.

In order to fix it, its quite simple you have to go back on the console and into the configuration section of the lambda function console. Find the Runtime Settings and click on Edit and change the Handler to the function name you are using. E.g MyTransformFunc::MyTransformFunc.Function::ProcessFile as you can see the ProcessFile is my method that I have in my code that needs to be executed based on the namespace I am using.

I hope this helps 🙂

Csharp-MS-Dotnet

In C#7 there is an enhancement in the main method Async Main in C# that will allow you to await in your main method. Let me show you an example of how it was in C#6 and then how it has changed in C#7.

You will remember this most likely

One will need to get the Awaiter and then GetResult in order for it to work. But now in C#7 you can write it like below.

And to add cherry to the icing, if you have a method that doesn’t return a value you can also just return Task.

Summary

The above code shows the ease that C#7 provides with its main method an great improvement from C#6 I would say, hope that helps you in learning C#.

Csharp-MS-Dotnet

In C#7 there is an improvement on using out parameter. Some of you may remember writing code like below.

The improvement that C#7 brings is you can now declare out variables in the argument list of a method call, rather than writing a separate declaration statement like below:

And it doesn’t end there we can also use the var keyword if we choose to.

Summary

What are the benefits of this out parameter, well it does make the code is easier to read. You are not declaring it where you use it. Not to mention you don’t need to initialize the variable either.

Csharp-MS-Dotnet

In C# 7 there the new feature called Local functions in C#. Basically Local functions allow one to write a function within the body of another method or function. It comes in handly in the case lets say you are creating a helper function that are mostly just used in one class or where the declaration makes the code clear of the intention. Local functions also come in handly to write recursive functions rather than using a Lambda for it.

Here are some places where you can use local functions.

  • Methods, especially iterator methods and async methods
  • Constructors
  • Property accessors
  • Event accessors
  • Anonymous methods
  • Lambda expressions
  • Finalizers
  • Other local functions

Below is an example where I Anonymize a string of emails, you can see that I am actually calling the local function below the return statement. Therefore where you can declare a local function below a return statement and also the Pattern variable is available inside the local function, since it is within its scope.

If you wish to read more on local functions take a look at the Microsoft documentation of local functions.

Csharp-MS-Dotnet

In C# 7 there are Expression Bodied Accessors which allows you to write getter and setters somewhat more functional way, somewhat lambda’ish. Let me show you an example. You may remember the old way of writing getter and setters like below.

Pre Expression Bodied Accessors Getter and Setter

Expression Bodied Accessors in C# 7

When using C# 7 you can use Expression Bodied Accessors like below which makes the code wayyyyy cleaner in my opinion. As you can see below, I have also added the null exception into the setter.

Summary

I hope I have explained how Expression Bodied Accessors can make your code way cleaner and easier to understand, not to mention that you can include exception into the setter since Exceptions are also Expression now. If you wish to learn more you can also visit Microsoft Learn Site on Express Bodied Accessors. Btw you can also use the Expression Accessors for Constructors and Destructors.

Csharp-MS-Dotnet

In C#7 there are Digit Separators and Binary Literals which were added. One may wonder what these are? Basically you can replace long number values with underscore (_) such that code readability is improved.

Let me give you couple of examples of it to make it clear.

Neat but it doesn’t end there you can also use it for Binary literals and Hex also, like the examples below, where they all are 2020 the year we all despise 🙂

Summary

I hope the C#7 Digital Separators and Binary Literals will give you a hand in writing more clear code 🙂

Csharp-MS-Dotnet

So one may ask “What is the advantage of using discards in C#?”, and if you don’t know what discards are you can check out the MS C#7 Documentation about it.

Basically discards are variables in simple terms, and the declaration of the variable as a discard is by assigning it the underscore (_) as its name. Let’s look at an example of it using tuples. And if you want to know more about tuples look at my older post on tuples.

Example discard in C#

As you can see above I have declare lastname as a discard, the advantage is that the lastname variable may not even be allocated any storage. Thus in a sense discards can reduce memory allocations, that is one of the main advantage of discards. Not to mention that it the intent of the code above clearer, readability wise and maintainability wise. There are other places you can use discards also.

Out parameters methods

The code below shows using discard in a TryParse method like below, where we use the out method for getting the value, but if we don’t really care about the value discards comes in handy here.

Pattern Matching

One can also use discards in pattern matching in C#. In the below example I am using pattern matching to process a user where a user can be a student or partime student, and they both are process the same way for example.

Deconstruct method

In C# you can also use discards for deconstruct methods, where you use a method name Deconstruct with out parameters, let’s look at an example of it. It is another handy way to get data out of class, structures and get them into tuples.

Summary

I hope this helps in explaining where you can see potentials of using discards in C#, it is a way of intentionally not wanting a variable, the plus side of it, allows one to read the code more clearly.

Csharp-MS-Dotnet

Some may wonder what is the difference between ValueTuple and Tuple that was released in C# 7, and you might say I have been using Tuples for a while now. Whats the difference? Some of you will remember with code like below that showcase the use of ValueTuple since .NET Framework 4.7

In order to use this, you will use the data members, such as Item1, Item2, etc., which are fields rather than properties. So in your code it will look something like below. As you can see it pretty ugly and which one is firstname and which one is lastname? You will have to guess was it Item1 or Item2?

Main differences

The main difference between Tuple and ValueTuple are:

  • System.ValueTuple are structures (value types) rather than classes (reference types). This is meaningful when talking about allocations and Garbage Collection pressure.
  • System.ValueTuple are mutable rather than read-only. That is, the value of tuple components can change.
  • System.ValueTuple exposes data members, such as Item1, Item2, etc., are fields rather than properties.

Lets try to use the same example and use System.Tuple rather and see how our code changes.

In the above code you can see it’s shorter and clearer of the intention of the code and when we try to consume this code like below we can use the labels rather.

Summary

Hope you learned something new today and start using the new syntactic sugar that C# 7 provides, and if you are doing machine learning you will also feel the benefit of C# Tuples. More on machine learning in later blog post 🙂

UA-4524639-2