Monday, November 19, 2018

Unable to GetPassword on AWS EC2 Launched from Windows Server 2016 Custom AMI

We found out that our custom AMI doesn't allow us to enable GetPassword from AWS console on any EC2 launched from it. After reading and some trial and error, we found out that InitializeInstance.ps1 has to be enabled for the next boot.

https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch.html#ec2launch-inittasks

To be complete, LaunchConfig.json has to have adminPasswordtype  set to Random (default). Then, run the following PowerShell command:

C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 -Schedule

Thursday, November 1, 2018

Entity Framework (EF) Slow Insert and SqlBulkCopy to the Rescue

There was a need to insert bulk data into the database and we are using Entity Framework as our ORM strategy. However, bulk insert took forever. Disabling Auto Detect Changes helps a little. So I decided to look into SqlBulkCopy and ended up writing one for Entity Framework. It is generic enough and can be found in https://gist.github.com/nikyodo85/b82ffd56bb2f0d45a9860dadcdfdc01d.

It works well so far. Some of the drawbacks are:

  • It won't be able to auto insert relationship
  • No validation check
But very fast. To use it will be very similar to SqlBulkCopy:


Dim efSqlBulkCopy As New EFSqlBulkCopy(Of MyEntityClass)(myDbContext)
efSqlBulkCopy.WriteToServer(listOfMyEntityClass)

Supressing Output in PowerShell

I was working with DiskPart, PowerShell, and Amazon SSM. Whenever I run DiskPart from PowerShell, the output was reflected in the console and it was recorded as Amazon SSM run command output, thus my run command was not completely clean. Such as the following:

Microsoft DiskPart version 6.3.9600
Copyright (C) 1999-2013 Microsoft Corporation.
On computer: MyComputer
DISKPART>
Disk 1 is now the selected disk.
DISKPART>
DiskPart successfully converted the selected disk to dynamic format.

Reading Q&A and documentation online, seems like no straightforward way to suppress output from DiskPart. I then tested something, how about if I assign the output to a variable and ignore it. It works and the command is still executed. For example, listing volume will be like the following in PowerShell:

$DiskPartOutput = 'list volume' | diskpart