Wednesday, March 30, 2016

How To Setup AWS CodeDeploy using Github

Learn how to setup AWS CodeDeploy using Github for Continuous Deployment & Continuous Integration...


Unlike Microsoft Azure cloud service which offers a very easy and staright forward way to setup the Continuous Deployment and Continuous Delivery with any version control like Github, Bitbucket, or a local repository, I found out AWS CodeDeploy requires a quite complex procedure to achieve the same and hence is limited to only S3 bucket and Github for continuous deployment of code to any EC2 instance. So kindly follow this step-by-step tutorial on to set this up with minimal steps. The AWS doc too seems complex and i searched a lot when finally found some video demonstration for the same which really helped me out.

 AWS CodeDeploy

Introduction To AWS CodeDeploy
AWS CodeDeploy is one of the developer tools by Amazon Web Services which allows for Continuous Integration and Continuous Deployment. AWS CodeDeploy coordinates the application deployments to Amazon EC2 instances, on-premises or both.
AWS CodeDeploy currently deploys application revisions from Github and Amazon S3 buckets.

Pre-requisites for the Tutorial
i.   GitHub Account with Code Repository setup containing the appspec.yml file and scripts folder in the root directory of the application.
Sample Code Structure and appspec.yml file can be found on the following-            
https://github.com/shoobomb/AWS-CodeDeploy-Example
ii.               AWS account setup.





Tutorial
Here goes the tutorial to configure and setup Continuous Deployment with Github Using CodeDeploy –   
  1.    Sign in to your AWS account by going to the following link:

  2.    Next, we create two IAM roles by clicking the services tab and going to the IAM (Identity & Access Management)

i.               First we are going to create IAM role for CodeDeploy to talk to our EC2 instance where our code is deployed. For this we click on create new role by clicking on “Roles” tab, with some name “CodeDeploy”.
ii.               Next, select EC2 and click on Next.
iii.         Then, we are going to add a custom policy to this Role. Add the following policy document to this role and click on create Role.

// Policy Role for Code Deploy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "autoscaling:PutLifecycleHook",
        "autoscaling:DeleteLifecycleHook",
        "autoscaling:RecordLifecycleActionHeartbeat",
        "autoscaling:CompleteLifecycleAction",
        "autoscaling:DescribeAutoscalingGroups",
        "autoscaling:PutInstanceInStandby",
        "autoscaling:PutInstanceInService",
        "ec2:Describe*"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

iv.           Next, we are also going to edit the trust relationship for this IAM role by adding the following relationship policy.

// Policy Trust for Code Deploy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "codedeploy.us-west-2.amazonaws.com",
          "codedeploy.us-east-1.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

v.      Now we are going to create another IAM role for our EC2 instance. For this we click on create new role by clicking on “Roles” tab, with some name “EC2CodeDeploy”.
vi.              Then, select EC2 and click on Next.
vii.         Then, we are going to add a custom policy to this Role. Add the following policy document to this role.

// Instance Role for EC2 Instance
{
    "Version": "2012-10-17",
    "Statement": [  
      {    
          "Action": [      
              "s3:Get*",      
              "s3:List*"    
          ],    
          "Effect": "Allow",    
          "Resource": "*"  
      }
    ]
}

viii.         Finally click on ‘Create Role’.

  3.    Now we are going to create and launch a new EC2 instance by going under the services and click on EC2. Then click Launch a New Instance.

  4.    For this tutorial, we’ll select “Amazon Linux AMI” because it comes pre-installed with AWS CLI.

  5.    Next, we are going select the instance type according to our requirements.

  6.    Then click on the button “Configure Instance Details”.

 7. Now under IAM we are going to select the EC2 role we create i.e. EC2CodeDeploy.

 8.  Next the storage is fine as default and then we can tag the instance by adding some value to the Key name like “AWS CodeDeploy”.

 9.  Next configure the Security Group to allow all inbound-outbound traffic, leave it as default and proceed further. Finally click on Review and Launch button. 

 10.   Now select the key pair for establishing the SSH or create a new one if not already created and wait for the instance to launch.

 11. Once the instance has been launched successfully, we are going to connect to via SSH. For this go to your instance under EC2 and click on connect.

 12. After you are successfully connected to your EC2 instance, we need to configure something and install Code Deploy agent in our EC2 instance.

 13. Run the following commands on to your EC2 instance by following the instruction sheet provided.

Instruction Sheet
i.                   When server is booted run the following commands as root.
yum -y update
yum install -y aws-cli
cd /home/ec2-user

ii.                 Here you will setup your AWS access, secret, and region.
aws configure
aws s3 cp s3://aws-codedeploy-us-east-1/latest/install . --region us-east-1
chmod +x ./install

iii.              This is simply a quick hack to get the agent running faster.
sed -i "s/sleep(.*)/sleep(10)/" install

        ./install auto

iv.              Verify it is running.
service codedeploy-agent status

 14. Next, we are going to open the CodeDeploy in the AWS console by clicking the services tab and finding the option for CodeDeploy under Developer tools.

 15.  We will click on “Get Started” and then select the “Custom Deployment” option and click next.

 16.  And then under the Create New Application window, we would need to add the entries to the fields provided.
Application Name: AWSCodeDeploy
Deployment Group: AWSCodeDeploy
Add the Tags: Key- name, Value – AWS CodeDeploy
Deployment Configuration: CodeDeployDefault.OneAtATime (fine for now or select according to your requirements)
Service Role ARN: CodeDeploy (It’s the first role we created earlier)
Finally click on Create Application.

 17.   Next click on the CodeDeploy application we just created and then click on Actions button then “Deploy New Revision”.

 18. Inside the “Create New Deployment” window, add the entries to the following fields.
Application Name: AWSCodeDeploy
Deployment Group: AWSCodeDeploy
Revision Type: Select the option for Github
Repository Name: Enter the name of the repository in the following format- username/repository_name
shoobomb/AWS-CodeDeploy-Example
Commit ID: In the commit id, enter the 40 characters commit id you can find on your Github repository page.
Deployment Config: CodeDeployDefault.OneAtATime

Note- The Github repository which contains the code to deploy should also contain an ‘appspec.yml’ file which defines the application specification for how the application would be deployed on CodeDeploy. You can have a look at the directory structure of the application code and the appspec.yml file in the following Github repository which also contains a sample code to be deployed.

More details on how about creating your own ‘appspec.yml’ file or plan a revision for your own project, you can have a look at the AWS Documentation for the same.

 19. Finally you can click on ‘Deploy’ and wait for this revision to get successfully deployed on the EC2 instance. You can check the ‘Status’ field to check whether the revision was successfully deployed or not. 

  20.  If you see the Status field value as Succeeded, it means your revision has been successfully deployed on your EC2 instance. To check for the same, you can go to the EC2 tab and look out for running instances, then check for the Public DNS or the public IP to check the changes made to the new revision deployed.

  21. That’s it about setting up continuous deployment using CodeDeploy, you can explore more features by going to the CodeDeploy on AWS console.



Thursday, March 17, 2016

Step by Step Tutorial To Setup MySQL Database Server on Microsoft Azure

Setting Up the MySQL Database Server on Microsoft Azure Virtual Machine...
Following are the steps -
1. Login to the Azure Management Portal by going to the following link –
Enter the your user credentials –
Username: your username
Password: your password

2. Go to the Virtual Machines window by clicking on the ‘Virtual Machines’ tab on the left side of the Azure Management Portal.

3.  Click on the ‘New’ button at the bottom to create a new virtual machine.

4. Inside the ‘New’ Grid window click on ‘Compute’, then ‘Virtual Machine’ and finally ‘Quick Create’. This would open up a small form to enter the details of the Virtual Machine.

5. In The ‘DNS Name’ text-field, enter the name of the virtual machine of your choice which has the extension of .cloudapp.net (anyname.cloudapp.net).

6. In the ‘Image’ section, select the copy of the virtual machine image you want to run (Windows Server 2012 R2 Datacenter).

7. Now select the ‘Size’ you want to allocate to the Virtual Machine in terms of CPU cores and the memory (RAM).

8. Next enter the user credentials (Username: anyusername, Password: anypassword).

9. At last select the ‘Region/Affinity Group’ from the list of regions to opt for a particular region where the virtual machine would reside (East US).

10. Finally click on the ‘Create Virtual Machine’ button. Now you can see a notification saying “Creating Virtual Machine…”

11. Once the Virtual Machine is configured and ready, you’ll see a notification saying “Successfully created virtual machine vm_name”.

12.  Now you can check the ‘Status’ of the Virtual Machine changed to ‘Running’ in the Virtual Machine window.

13. When the Virtual Machine is in the Running state, you can click on the Virtual Machine and navigate to various tabs like Dashboard, Monitor, Endpoints & Configure to check and edit the details of the VM.

14. Now for connecting to the Virtual Machine, go to the Dashboard. In the dashboard window, click on the “Connect” button on the Bottom.

15. Clicking on the ‘Connect’ button, a .rdp (remote desktop protocol) file will be downloaded. Save the file on the personal computer for establishing the connection to the Virtual Machine from your windows machine.

16. Open the .rdp file downloaded from Azure. It would ask for user credentials. Enter the user credentials mentioned earlier in the process of configuring the Virtual Machine. Also a security certificate prompt will open, click on OK to trust the source and wait for the Remote Desktop Connection to commence. In a few seconds you will be inside your Virtual Machine (Windows Server 2012 R2 Datacenter).

17. Now inside your Virtual Machine, download the MySQL Community Server from the following link –

18. Open the installation file for MySQL and start the installation process. Click on Typical mode for installation. And wait for it to complete the installation.

19. Click on Finish to complete the MySQL installation. Next the MySQL Configuration wizard will open.

20. Click on next. Then check the option for ‘Standard’ Installation, click on Next.

21. Now check the option for both ‘Install as a Windows Service’ and ‘Include Bin Directory in Windows PATH’.

22. Now enter the password for the MySQL server, then click on Next.

23. Now click on Execute to commit the configurations and wait for it to complete.

24. Click on Finish to complete. Now the database server is ready.

25. To check the connection of the database server from a remote system/computer execute the following command from MySQL Command Line Tool installed on the remote system –
mysql –u username –p –h anyname.cloudapp.net
password: your password

26. After this you are into the MySQL database server hosted on the Microsoft Azure Virtual Machine.