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

Deploy a Symfony application on AWS Elastic Beanstalk with Docker

Romaric Drigon
December 07, 2018

Deploy a Symfony application on AWS Elastic Beanstalk with Docker

Getting started with AWS Elastic Beanstalk can be challenging. We will review reasons to use it, and how to easily set up a web application, developed with the Symfony framework, on a high-availability, scalable, hosting using Docker multicontainer platform.

Romaric Drigon

December 07, 2018
Tweet

More Decks by Romaric Drigon

Other Decks in Technology

Transcript

  1. Deploy a Symfony
    application on AWS
    Beanstalk with Docker
    SymfonyCon Lisbon 2018

    View full-size slide

  2. Hello, I'm Romaric.
    I work at netinfluence @ Lausanne,
    Switzerland.

    View full-size slide

  3. What's our goal?
    We want a setup one-man* can manage
    We want high availability
    As a bonus, we would like to have auto-scalability
    *It also means documented, to account for "bus factor".

    View full-size slide

  4. Why Docker?
    Avoid snowflake
    servers
    Documentation as
    code
    More robust: easier
    to rebuild,
    upgrade, rollback
    Easier to deploy a
    new server

    View full-size slide

  5. Why AWS
    Elastic
    Beanstalk?
    We were already
    using some AWS
    services.
    Huge ecosystem
    Competitive prices
    3 different solutions:
    ECS (advanced)
    Elastic Beanstalk
    Fargate (serverless)

    View full-size slide

  6. How does it
    costs?
    It is free

    View full-size slide

  7. How does it
    costs?
    It is free
    You pay for used
    ressources, typically
    EC2 instances and ELB
    load balancer
    ($20/month).
    Plus other services you may
    use outside of EB.

    View full-size slide

  8. Getting started

    View full-size slide

  9. Step 1: Prepare your application

    View full-size slide

  10. Move all state (data) out of web servers
    Use an external file storage for uploaded files
    S3...
    Split the database from web servers
    AWS RDS for MySQL/PostgreSQL, set up your own DB EC2
    instance...
    Store sessions in a shared storage
    DB (easy!), memcached or Redis (AWS ElastiCache)...
    Optional: send logs to an external service
    Sentry...

    View full-size slide

  11. Pro tip: use AWS Route53 namesevers
    You don't have to register your domain name with AWS, but you should
    use Route53 as nameserver(s) (set up a hosted zone, a few $/month),
    so you can use an alias A record instead of copying the load balancer
    IP.
    It will also make getting SSL certificates easier, with AWS ACM (free).

    View full-size slide

  12. Use
    environment
    variables for
    configuration
    Supported in Symfony
    2 with Incenteev
    Parameter handler,
    Symfony3 (getenv),
    Symfony4
    Also supported in
    php.ini
    For other scenarii, use
    envsubst

    View full-size slide

  13. Step 2: Define architecture

    View full-size slide

  14. Elastic Beanstalk concepts
    Platform: Docker (not PHP)
    2 flavors: "single container" platform or "multicontainer" platform.
    Application: our project (with history of code versions...), we will have
    only one.
    Environment: an "instance" of our application, with some configuration
    and resources (EC2 instances...). It could be "staging" and
    "production".

    View full-size slide

  15. Container(s) architecture
    From AWS EB multi-container documentation

    View full-size slide

  16. Listing containers
    We need:
    1..n container for nginx
    1..n for PHP-FPM
    1 running Cron jobs
    1 for ephemeral tasks (like running DB migrations...)
    Some can auto-scale, some should not (ie., Cron).
    Solution: use 2 Beanstalk environments, myapp-web and myapp-
    worker

    View full-size slide

  17. Step 3: make Docker images

    View full-size slide

  18. Step 4: create the manifest file

    View full-size slide

  19. Manifest file: Dockerrun.aws.json
    Everything is declared in a Dockerrun.aws.json file, in our project
    directory.
    We have 2 different files, one for myapp-web and one for myapp-
    worker.
    Full code on Gist here
    {
    "volumes": [
    {
    "name": "sf-app",
    "host": {
    "sourcePath": "/var/app/current"
    }
    }
    ],
    "AWSEBDockerrunVersion": 2,
    "containerDefinitions": [
    {
    "name": "php-web",

    View full-size slide

  20. Step 5: deploying

    View full-size slide

  21. Step 5.1: create the app on Elastic
    Beanstalk

    View full-size slide

  22. Select multi-container, and sample
    application

    View full-size slide

  23. Select "high availability", and then you can
    configure rolling updates

    View full-size slide

  24. Step 5.2: deploy your code

    View full-size slide

  25. Deploying...
    Install EB CLI.
    From our application folder, with the application installed* and
    everything committed to Git**, we can now run eb deploy [--
    init]:
    *So you should composer install and generate assets before
    **If you are using Git and some files are not committed, by default EB will
    deploy committed changes only.

    View full-size slide

  26. Done!
    romaric@netinfluence.ch

    View full-size slide