Building the Cocoon
A caterpillar, when it is fully grown, secretes a long stream of liquid from its glands, called the spinneret, located below its mouth. The liquid stiffens forming a silk like thread which is used to attach its hind end to a twig or leaf. The caterpillar spins the silky thread around its body to form a covering. This outside layer hardens to form a shell called a chrysalis.
The Transformation Process
Inside the cocoon the caterpillar changes into a pupa. In a process called histolysis, the caterpillar digests itself from the inside out, causing its body to die. During this partial death, some of the caterpillar's old tissues are salvaged to form new. This remnant of cells are called the histoblasts and are used to create a new body. Using its digestive juices, the caterpillar turns his old larval body into food which he uses to rebuild its new body.
Breaking Out of the Shell
Once the pupa has fully grown inside the cocoon, and the butterfly is ready to emerge, the insect releases a fluid which softens the shell. The butterfly pushes on the walls of the shell until it breaks open. The process of a caterpillar turning into a butterfly can take anywhere from 10 days to several months.