Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scripting creating AWS based servers

coldclimate
January 27, 2014

Scripting creating AWS based servers

in the most Taco Bell way I know

coldclimate

January 27, 2014
Tweet

More Decks by coldclimate

Other Decks in Technology

Transcript

  1. Scripting your servers
    @coldclimate CANDDi.com

    View Slide

  2. Put your pens down
    @coldclimate CANDDi.com

    View Slide

  3. There once was a man...
    @coldclimate CANDDi.com

    View Slide

  4. And then there was a document...
    @coldclimate CANDDi.com

    View Slide

  5. Which became a wiki...
    @coldclimate CANDDi.com

    View Slide

  6. Which finally because a script
    @coldclimate CANDDi.com

    View Slide

  7. Which mostly worked...
    @coldclimate CANDDi.com

    View Slide

  8. And then the madness had to stop
    @coldclimate CANDDi.com

    View Slide

  9. You wouldn’t treat code like this
    (hopefully)
    @coldclimate CANDDi.com

    View Slide

  10. Man => Document => Wiki => Script =>
    Images
    @coldclimate CANDDi.com

    View Slide

  11. Encoding
    Version controlling
    (automating)
    @coldclimate CANDDi.com

    View Slide

  12. Man => Document
    Removing the bus factor
    “The man we can’t fire”
    @coldclimate CANDDi.com

    View Slide

  13. Document => Wiki
    The fucker *might* stay up to date
    @coldclimate CANDDi.com

    View Slide

  14. Wiki => Script
    Because script in documentation
    is stupid
    @coldclimate CANDDi.com

    View Slide

  15. @coldclimate CANDDi.com

    View Slide

  16. Why are you not talking about
    chef, puppet, ansible etc?
    @coldclimate CANDDi.com

    View Slide

  17. http://widgetsandshit.com/teddziuba/2010/10/taco-bell-programming.html
    @coldclimate CANDDi.com

    View Slide

  18. Build once or build twice?
    @coldclimate CANDDi.com

    View Slide

  19. Build once
    Raw OS
    Base
    Instance
    Script
    Working
    Instance
    @coldclimate CANDDi.com

    View Slide

  20. Kick-off instance
    Install packages
    Configure them
    Install your software
    Hope it all works
    @coldclimate CANDDi.com

    View Slide

  21. Build Twice
    Raw OS
    Base
    Instance
    Script “Base Box” Make AMI
    “Base Box”
    “Instance
    Box”
    Script
    Base AMI
    @coldclimate CANDDi.com

    View Slide

  22. Build once or build twice?
    @coldclimate CANDDi.com

    View Slide

  23. Kick-off instance
    Install packages
    --------------------------------------------
    Configure them
    Install your software
    @coldclimate CANDDi.com

    View Slide

  24. Kick-off instance
    Install packages
    --------------------------------------------
    Configure them
    Install your software
    Changes rarely
    Changes frequently

    View Slide

  25. Kick-off instance
    Install packages
    --------------------------------------------
    Configure them
    Install your software
    AMI
    Instance

    View Slide

  26. Stability
    @coldclimate CANDDi.com

    View Slide

  27. Speed
    @coldclimate CANDDi.com

    View Slide

  28. The game:
    always runs, no manual fixes
    @coldclimate CANDDi.com

    View Slide

  29. A script to built an AMI
    https://gist.github.com/coldclimate/5ff56c96dac116f3234e
    @coldclimate CANDDi.com

    View Slide

  30. ec2-run-instances
    --region 'eu-west-1'
    -g TEMP
    -k TEMP
    -f install.sh
    -t t1.micro
    ami-ce7b6fba
    @coldclimate CANDDi.com

    View Slide

  31. install.sh
    limited size (16kb)
    runs as root
    cock to debug
    @coldclimate CANDDi.com

    View Slide

  32. /var/log/cloud-init.log
    @coldclimate CANDDi.com

    View Slide

  33. set -ex
    @coldclimate CANDDi.com

    View Slide

  34. Add any sources
    Update the APT repository
    Install core packages
    @coldclimate CANDDi.com

    View Slide

  35. Add any sources
    @coldclimate CANDDi.com

    View Slide

  36. echo "deb http://archive.canonical.com/ precise
    partner" | sudo tee -a /etc/apt/sources.list
    @coldclimate CANDDi.com

    View Slide

  37. python-setuptools
    add-apt-repository -y ppa:ondrej/php5
    @coldclimate CANDDi.com

    View Slide

  38. apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
    echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist
    10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
    @coldclimate CANDDi.com

    View Slide

  39. Update the APT repository
    @coldclimate CANDDi.com

    View Slide

  40. apt-get update
    @coldclimate CANDDi.com

    View Slide

  41. Install core packages
    @coldclimate CANDDi.com

    View Slide

  42. apt-get -y install zip unzip xmlstarlet
    sysstat iftop ntp sysvbanner build-
    essential python-setuptools nginx
    nodejs
    @coldclimate CANDDi.com

    View Slide

  43. easy_install -U boto
    @coldclimate CANDDi.com

    View Slide

  44. wget https://github.com/nicolasff/phpredis/archive/2.2.4.zip -P /tmp/
    mkdir -p /opt/code
    unzip /tmp/2.2.4.zip -d /opt/code/
    cd /opt/code/phpredis-2.2.4
    phpize
    ./configure
    make
    make install
    @coldclimate CANDDi.com

    View Slide

  45. banner “*** DONE BABY ***”
    @coldclimate CANDDi.com

    View Slide

  46. ec2-create-image
    -n
    instance_id
    @coldclimate CANDDi.com

    View Slide

  47. Making running instance
    @coldclimate CANDDi.com

    View Slide

  48. ec2-run-instances
    --region 'eu-west-1'
    -g sg-bb18b66c
    -k LIVE-PEM-KEY
    -p Instance_Profile
    -f install.sh
    -t m1.small
    --availability-zone eu-west-1a
    --disable-api-termination
    ami-XXXXXX
    @coldclimate CANDDi.com

    View Slide

  49. Roles
    http://docs.aws.amazon.com/cli/latest/reference/iam/create-role.html
    Instance Profiles
    http://docs.aws.amazon.com/cli/latest/reference/iam/create-instance-profile.html
    Adding a Role to an Instance Profile
    http://docs.aws.amazon.com/cli/latest/reference/iam/add-role-to-instance-profile.html
    @coldclimate CANDDi.com

    View Slide

  50. Download from S3
    Setup directories
    Setup config
    Install code
    Start it up
    Register it
    @coldclimate CANDDi.com

    View Slide

  51. Download from S3
    python <import boto
    import sys
    from boto.s3.key import Key
    def get_wonka(bucket_name, file_name, destination_file):
    conn = boto.connect_s3()
    bucket = conn.get_bucket(bucket_name)
    k = Key(bucket)
    k.name = file_name
    destination = "/tmp/%s" % (destination_file)
    k.get_contents_to_filename(destination)
    get_wonka("BUCKET","SOURCEFILE","DESTINATION")
    PY
    @coldclimate CANDDi.com

    View Slide

  52. Setup directories
    Your script is running as root
    so remember to chown
    @coldclimate CANDDi.com

    View Slide

  53. Setup config
    Copy it from the download into place
    Restart the service
    @coldclimate CANDDi.com

    View Slide

  54. Install code
    https://github.com/jordansissel/fpm
    @coldclimate CANDDi.com

    View Slide

  55. Start it up
    Supervisord
    Ubuntu service
    Avoid own scripts
    @coldclimate CANDDi.com

    View Slide

  56. Register it
    @coldclimate CANDDi.com

    View Slide

  57. Monitoring (zabbix)
    @coldclimate CANDDi.com

    View Slide

  58. Load Balancer (ec2 call)
    @coldclimate CANDDi.com

    View Slide

  59. Logging (papertrail)
    @coldclimate CANDDi.com

    View Slide

  60. DNS (R53 call)
    @coldclimate CANDDi.com

    View Slide

  61. And it works!
    @coldclimate CANDDi.com

    View Slide

  62. @coldclimate CANDDi.com

    View Slide

  63. Need to change the code or config?
    @coldclimate CANDDi.com

    View Slide

  64. JFDI and roll a fresh one
    @coldclimate CANDDi.com

    View Slide

  65. You need to change the base AMI
    @coldclimate CANDDi.com

    View Slide

  66. It’s simple, it just doesn’t feel it
    @coldclimate CANDDi.com

    View Slide

  67. Update the scripts until you can
    “launch clean”
    @coldclimate CANDDi.com

    View Slide

  68. Lastly
    @coldclimate CANDDi.com

    View Slide

  69. @coldclimate CANDDi.com

    View Slide

  70. speakerdeck.
    com/coldclimate/scripting-creating-
    aws-based-servers
    @coldclimate
    canddi.com/blog
    @coldclimate CANDDi.com

    View Slide