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

    easier to rebuild, upgrade, rollback Easier to deploy a new server
  3. Why AWS Elastic Beanstalk? We were already using some AWS

    services. Huge ecosystem Competitive prices 3 different solutions: ECS (advanced) Elastic Beanstalk Fargate (serverless)
  4. 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.
  5. 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...
  6. 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).
  7. 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
  8. 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".
  9. 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
  10. 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",
  11. 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.