Monday, 26 October 2015

Register/Deregister EC2 Servers under ELB using AWS .NET Toolkit

I have been working with AWS toolkit for some time in last week. I prepared some tools to automate our infra team tasks and for our internal development and testing purpose.

Here I will show you how to register/de-register EC2 instances with Elastic Load Balancer programmatically using AWS .NET toolkit.

First get your awsSecretAccessKey and awsAccessKey,


1
2
 string awsSecretAccessKey = ConfigurationManager.AppSettings["awsSecretAccessKey"];
 string awsAccessKey = ConfigurationManager.AppSettings["awsAccessKey"];

Note: The user account you use here for Secret Key and Access Key must have full access to EC2 service of AWS

Create EC2 Client,


1
 var client = AWSClientFactory.CreateAmazonElasticLoadBalancingClient(awsAccessKey, awsSecretAccessKey, RegionEndpoint.USWest2);

Make sure you specify correct RegionEndPoint where your EC2 instances exist. Here I have used USWest2 region end point.

Thursday, 20 August 2015

Automate Cloudfront Cache Invalidation Using AWS .NET Toolkit

Amazon CloudFront is a content delivery web service to give developers and businesses an easy way to distribute content to end users with low latency and high data transfer speeds.

Amazon Cludfront has a number of edge locations worldwide, when your application target audience is scattered worldwide then Amazon Cloudfront is a good choice for caching your files and contents to give high throughput and low latency. Let say, if someone is accessing your application from east zone of USA then your contents will be delivered by one of the cloudfront edge servers located in USEast.

Your files which got cached on cloudfront edge locations will be automatically invalidated as per the default Cache Timeout set in request header. But sometimes you may forcefully want to clear files from Cloudfront cache prior to the actual Cache Timeout.

Wednesday, 1 July 2015

Sitecore : Datetime Field with Timezone

Working with Date Time and Timezones is always headache. Recently we faced issue with calendar events generated by our sitecore application in different regions. The start date and end date in generated ICS file was failing to show exact date time specific to end user because of the difference in timezones.

Why that happened?

Curretly there is no mechanism in sitecore to store date and time in different timezones. It simply considers the server timezone - on which the sitecore instance runs - and stores date time accordingly.  So, to make datetime field independent of any specific timezone (more specifically - server timezone) , we needed a custom field that stores UTC (Universal Coordinated Time) , a globally unique datetime, as raw value and presents timezone specific date and time to the content authors.

Note: We can easily do conversion of UTC to any timezone and visa versa, with .NET globalization API


How this prototype works?

Here is how the new field looks to content authors.


This is what sitecore stores in backend.


You can notice the raw value is the UTC equivalent of the IST time content authors can see. And this is the secret of this field :)

You might be wondering from where the the timezone India Standard Time is coming??
 -  In our application, we have separated all settings from actual content items, and those settings are globally applicable to entire site (something like AppSettings in .net application)

-  So here the timezone IST is coming from site level settings, which looks like this



-  Based on above selection, only the display date and time will change, but the raw value will remain same.

Friday, 19 June 2015

Transfer Sitecore User Accounts with Passwords from One Instance to Another

Sometimes you need to transfer sitecore users from one instance to another instance having different databases.

For this, you can serialize your users from source instance and then deserialize them on destination instance.

Steps to perform this task

1.  Serialize Users on Source Instance :
 To serialize users, open Content Editor - Security - User Manager





Thursday, 7 May 2015

MongoDB M101N Final Exam Question 10

Question 10:

We perform the following query on the enron dataset:

var exp = db.messages.explain('executionStats')

exp.find( { 'headers.Date' : { '$gt' : new Date(2001,3,1) } }, { 'headers.From' : 1, '_id' : 0 } ).sort( { 'headers.From' : 1 } )


and get the following explain output.  

MongoDB M101N Final Exam Question 9

Question 9:

Imagine an electronic medical record database designed to hold the medical records of every individual in the United States. Because each person has more than 16MB of medical history and records, it's not feasible to have a single document for every patient. Instead, there is a patient collection that contains basic information on each person and maps the person to a patient_id, and a record collection that contains one document for each test or procedure. One patient may have dozens or even hundreds of documents in the record collection.
We need to decide on a shard key to shard the record collection. What's the best shard key for the record collection, provided that we are willing to run inefficient scatter-gather operations to do infrequent research and run studies on various diseases and cohorts? That is, think mostly about the operational aspects of such a system. And by operational, we mean, think about what the most common operations that this systems needs to perform day in and day out.

MongoDB M101N Final Exam Question 8

Question 8:

Supposed we executed the following C# code. How many animals will be inserted into the "animals" collection?

using MongoDB.Bson;
using MongoDB.Driver;
using System.Threading.Tasks;

namespace M101DotNet
{
    class InsertTest
    {
        static void Main(string[] args)
        {
            MainAsync(args).GetAwaiter().GetResult();
        }

        static async Task MainAsync(string[] args)
        {
            var client = new MongoClient();
            var db = client.GetDatabase("test");

            var animals = db.GetCollection<BsonDocument>("animals");

            var animal = new BsonDocument
                            {
                            {"animal", "monkey"}
                            };

            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "cat");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
            animal.Remove("animal");
            animal.Add("animal", "lion");
            animal["_id"] = ObjectId.GenerateNewId();
            await animals.InsertOneAsync(animal);
        }
    }
}