Hi Anon,
Firstly, I'm sorry to hear about your friend's situation.
Writing a letter can be an immensely intimate and personal way of communicating with a loved one so it's really appropriate in this circumstance, especially if you hand write it.
My primary piece of advice would be to plan out what you say before you write the letter so you can organise it later on. Think of thoughts, feelings and memories you want to include and jot them down.
Address them by their favourite nickname and keep the tone light and sincere. Open with your dismay regarding the situation and then move on to reminisce about the great times you've had together. Ask them questions so they don't feel as if this is the 'final letter' and it gets them thinking about their reply.
Hopefully this helps and I wish you and your friend all the best.