Posts

Parse the namespace based XML using Python

In this blog, I am considering how to parser and modify the xml file using python. For example, I need to parser the following xml 1 (slightly modified for this blog) and need to write the modified xml to out.xml file. Here the country.xml <?xml version="1.0"?> <actors xmlns:fictional="http://characters.example.com" xmlns="http://people.example.com"> <actor type='T1'> <name>John Cleese</name> <fictional:character>Lancelot</fictional:character> <fictional:character>Archie Leach</fictional:character> </actor> <actor type='T2'> <name>Eric Idle</name> <fictional:character>Sir Robin</fictional:character> <fictional:character>Gunther</fictional:character> <fictional:character>Commander Clement</fictional:character> </actor> </actors> In ...

Terraform SSH connection to AWS EC2

Image
The instances are created via Terraform, but Terraform can only used existing key pairs. First thing is to create the key pair as explained in the AWS documentation 1 . NOTE: However, if you are not familiar with Terraform, please go through the following blogs before this blog Basic example of creating AWS EC2 with Terraform Creating AWS S3 bucket with Terraform Connect to the EC2 instance, the most important thing is key value pair which is create in the time of the EC2 instance is created. But this is not currently possible in the Terraform. Therefore we need to create the mykey first. For example aws ec2 create-key-pair --key-name mykey --query 'KeyMaterial' --output text > ~/.aws/mykey.pem If you are connecting from the Linux or Mac, because need read permission. chmod 400 ~/.aws/mykey.pem To display aws ec2 describe-key-pairs --key-name mykey To retrieve the public key from the pem file (Optional) ssh-keygen -y -f ~/.aws/mykey.pem Before th...

Creating AWS S3 bucket with Terraform

I recommend to read the first example 1 ,because this is a extenstion to that. However, instead of AWS EC2, here the target resource is AWS S3 for the simplicity. Here the providers.tf file. provider "aws" { region = "${var.s3_region}" } terraform { required_version = ">= 0.11.13" backend "s3" { bucket = "ojitha" key = "test/backbone" region = "ap-southeast-2" encrypt = "true" } } As shown in the above, the stage is maintain in the S3 bucket instead of locally as specified in the line# 7. resource "aws_s3_bucket" "main" { bucket = "${var.s3_bucket_prefix}-${var.environment}-${var.s3_region}" acl = "private" tags = "${local.s3_tags}" region = "${var.s3_region}" lifecycle { prevent_destroy = "false" } server_side_encryption_configuration { rule { ...

Basic example of creating AWS EC2 with Terraform

Here the very basic example. This is just a note of creating single EC2 instance using Terraform. First you need to define the provider in the example.tf file provider "aws" { region = "ap-southeast-2" } # resource "aws_s3_bucket" "example" { # bucket = "ojithatest1" # acl = "private" # } resource "aws_instance" "example" { ami = "${lookup(var.amis, "ubuntu-server")}" instance_type = "t2.micro" # depends_on = ["aws_s3_bucket.example"] provisioner "local-exec" { command = "echo ${aws_instance.example.public_ip} > ip_address.txt" } } resource "aws_eip" "ip" { instance = "${aws_instance.example.id}" } If you need S3 bucket depends on that EC2 uncomment the above code. In the above code, we are just creating EC2 instance and the assciated Elastic IP address. Above code use the variables...

Pattern matching Explained

Image
This is an explanation to the previous blog 1 relating to the Scala Matching. Most of the thoughts referring to the book "Functional Programming in Scala" 2 . For example consider the following Algebraic Data Type MList: // Singly linked lists sealed trait MList[+A] case object MNil extends MList[Nothing] case class Cons[+A](head:A, tail: MList[A]) extends MList[A] object MList{ //companion object def sum (ints: MList[Int]):Int = ints match { case MNil => 0 case Cons(x,xs) => x + sum(xs) } def apply[A](as: A*): MList[A] = if (as.isEmpty) MNil else Cons(as.head, apply(as.tail: _*)) def append[A](fs:MList[A], ss:MList[A]): MList[A] = fs match { case MNil => ss case Cons(h,t) => Cons(h,append(t, ss)) } //curried way def dropWhile[A](l:MList[A])( f:A => Boolean): MList[A] = l match { case Cons(h,t) if f(h) => dropWhile(t)(f) case _ => l } } val x : MList[Int] = MList() val y :...

Scala Functions

Local Functions Local functions are the functions defined inside other functions. Therefore , local functions can access the parameters of their enclosing function. First-class Functions Scala supports first-class functions which can be user defined as well as anonymous literal value, for example (x:Int) => x + 1 parameters and the function body separated by the "=>". In case if the type can be inference, then no need to define type, this is called target typing . In the partially applied function , no need to provide all the necessary parameters, but partially. For example, def sum(a:Int, b:Int):Int = a + b //> sum: (a: Int, b: Int)Int val a = sum _ //> a : (Int, Int) => Int = ex3$$$Lambd a.apply(1,2) //> res1: Int = 3 In the second line, you don't need to give parameters. The underscore can be given to replace one or more parameters: val f: (Int, I...

Java 8: Remedies for What cannot be done in Generics

Image
Generic programming the way to reuse code for Objects of many different types. The unbounded wild card is "?" (which is actually "? extends Object"). You can read from it, but you cannot write. If your type variable is T then you can use only extends (never super ) with T then, the upper-bounded wildcard is "? extends T". you can define read from but can not add (only super support with wildcards) The term covariant preserves the ordering of types from more specific to more general. Collections are covariant when they use extends wildcard. The lower-bounded wildcard is "? super T". Collections are contravariant when they use super with a wildcard. The term contravariant preserves the ordering of types from more general to more specific. Here supplied element must be itself or above of the T . The rule of thumb is "producer => extends and consumer => super": PECS.  As shown in the above, you can copy the eleme...