1
votes

So I am trying to deploy my laravel application (v7) to was elastic beanstalk. I have seen tutorials directing uploading a zip file that contains a .env file and update config.database to use the global RDS_* environment variables.

This does not work for me because I want to use codepipline and codebuild to build my application with git hooks. I have tried to set that up but my codebuild does not build successfully because in my pubsec.yaml file I added the usual laravel setup commands like installing dependencies and migrating the application's database.

Migrating the database is where I am encountering an issue. Somehow it seems codebuild does not get the RDS_* variables for my app database. I have been stuck here for a while.

This has made me question how codebuild handles environment variables. How does it create the .env file it uses to deploy? I even added a Linux command to copy my .env.example into an new .env file but having the same issues.

Any help would be greatly appreciated. Thanks

The error on logs:

  

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from 
information_schema.tables where table_schema = forge and table_name = migrations 
and table_type = 'BASE TABLE') ```


1
Can you clarify what do you mean? CodeDeploy can't deploy to ElasticBeanstalk. Depoyment to EB is handled by CodePipeline itself using Deploy action provider of AWS Elastic Beanstalk. - Marcin
The codedeploy build step of my pipeline is failing because it seems the application being built by codeploy in its container does not have access to the RDS_* database .env variables so my database migration is failing. - Omene Joseph Ogheneruno
Do you mean CodeBuild? Sorry I don't understand what's the role of CodeDeploy in your CodePipeline. - Marcin
Yes codebuild... I have edited my question to reflect that. Thanks for catching it - Omene Joseph Ogheneruno
What's your buildspec.yml file then? CodeBuild does only what you specify in the buildspec.yml file. - Marcin

1 Answers

1
votes

Codebuild runs on a different environment from elastic beanstalk so environment variables created in elastic beanstalk cannot be accessed in the container AWS codebuild is running.

What code build actually does is build your application and transfer it to an s3 bucket so that during deployment your app can be accessed and moved to your VPC which in my case is an ec2 instance managed by elastic beanstalk.

After deployment (ie. app moved to vpc), EB environment variables can be accessed by the application.

So if you want to run commands that require access to EB environment variables, using commands in code build is the wrong place to put them. You should make use of EB extensions. You can read about them here.

For my Laravel application, I added an init.config file in the .ebextentions directory on the root of my application and then added my migration command as a container command. This worked for my use case.