Deploy a Symfony application on AWS Elastic Beanstalk with Docker

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.

0d02cc7698020597fdff97397baf799b?s=128

Romaric Drigon

December 07, 2018
Tweet

Transcript

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

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

  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".
  4. Why Docker? Avoid snowflake servers Documentation as code More robust:

    easier to rebuild, upgrade, rollback Easier to deploy a new server
  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)
  6. How does it costs? It is free

  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.
  8. Getting started

  9. Step 1: Prepare your application

  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...
  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).
  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
  13. Step 2: Define architecture

  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".
  15. Container(s) architecture From AWS EB multi-container documentation

  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
  17. Step 3: make Docker images

  18. Step 4: create the manifest file

  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",
  20. Step 5: deploying

  21. Step 5.1: create the app on Elastic Beanstalk

  22. Select multi-container, and sample application

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

  24. Step 5.2: deploy your code

  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.
  26. Done! romaric@netinfluence.ch