Skip to content
Logo Theodo

Deploy your Symfony application on AWS Elastic Beanstalk using CloudFormation

Maxime Thoonsen4 min read

I decided to follow an advice shared on twitter via the The Practical Dev: the best way to learn AWS is to start using it.

The problem

I was looking for a way to quickly create a Minimum Viable Stack on AWS with the following properties:

  1. Be setup in less than 10min
  2. Be able to run a Symfony application
  3. Using PostgreSQL on RDS
  4. Deployment is easy and fast
  5. Non AWS experts can create the MVS

But I couldn’t find any out-of-the-box tools so I looked for a solution. Here I describe my journey which ended up with a ready-to-go CloudFormation configuration.

Elastic Beanstalk

I started working with Elastic Beanstalk, the PAAS of AWS, which seemed to be exactly what I needed. Thanks to this article on Elastic Beanstalk configuration files and this one on how to deploy a Symfony application, I was able to run my application after some debugging cycles. My problem after that was that I couldn’t reuse my configuration to recreate the whole environment (Elastic Beanstalk instances + RDS instance) for a new project so I chose to experiment using CloudFormation.

CloudFormation

CloudFormation is an AWS service that creates a complete stack (VPC, load balancers, web servers, ..) from a template file.If you want to learn how to use CloudFormation, I recommend you to start by learning the basic templates. I started from a sample Elastic Beanstalk template and changed it so it can run a Symfony App. Here are the steps you need to perform in order to use my template:

Step 1: Because we will pass to the Elastic Beanstalk server the information to connect to the RDS postgresql database through environment variables, you need to update your parameters.yml with the following values:

database_host: "%env(DB_HOST)%"
database_port: 5432
database_name: "%env(DB_NAME)%"
database_user: "%env(DB_USER)%"
database_password: "%env(DB_PASSWORD)%"

Step 2: Ensure you have specified the driver to “pdo_pgsql” in the “app/config/config.yml” file.
Step 3: Upload a zip file with all your code to a s3 bucket. You create the zip file with this command:

zip -r code.zip . --exclude=*vendors*`

Step 4: Download my CloudFormation template.
Step 5: Go to your AWS console and open the CloudFormation and click on “create new stack”.
Step 6: Enter the required information:

cloudformation_stack
Step 7: Click on “next”, “next” and check “I acknowledge that AWS CloudFormation might create IAM resources.”. Then you can click on create. If everything is fine then you should see something like this:

creation_in_progress
Step 8 : You can check the Elastic Beanstalk page to see if everything went okay and find the url of your project at the top of the page.

all_right_status

Security remark: the password of your database will be available on the AWS console in the config section. A better solution would be to use an s3 file that will be copied during the initialisation of the Elastic Beanstalk container using .ebextensions files or using KMS and DynamoDB.

Configure the Elastic Beanstalk environment

Your site is online but you will want to update it. To do that you need to follow those steps:

  1. Ensure you don’t have a .elasticbeanstalk directory
  2. Run “eb init”, choose your region and the application you just created. You can find the name in the Elastic Beanstalk page.

Now you are ready, you can deploy new version of your code simply with “eb deploy”.

Conclusion

CloudFormation is a powerful tool with some drawbacks:

To help you write CloudFormation templates, you can try Troposphere. An alternative to using CloudFormation is to use Terraform. You can find an objective benchmark between the two tools here.

Bonus: tips to debug your elastic beanstalk application

Here are some tips you may need to debug you app:

Liked this article?