Search code examples
phpaws-code-deployaws-codepipeline

Export AWS Parameter Store environment variables to PHP during build?


I have a simple Laravel installation that I am trying to set up to run through a AWS CodePipeline using ECS.

What have I done?

My Codepipeline is set up with a buildspec.yml file, where I have specified that it should use a value from the Parameter Store, like

env:
    parameter-store:
        TEST_STRING: "TEST"

My TEST variable does exist in the Parameter Store and I can get it using the cli.

What do I want to do?

I want my environment variable to be accessible to my Laravel installation, so I can use the Parameter Store to save passwords and usernames for e.g. databases and apis.

What actually happens?

When I try to access the environment variable through php, I'm not getting any value. If I var_dump $_ENV, then I cannot see my TEST_STRING in the given array.

I'm quite sure that the environment variable only exists in the build process, but I cannot figure out how to "transfer" the environment variable from my build process to the deployed code.

Question

What am I missing to make the environment variables from the build process available to the deployed code?


Solution

  • Go to your cluster and the find your Task Definitions for the relevant task you want to add some environment variables to.

    Click to create a new revision of the task you wish to add variables to.

    Under "Container definitions" you have a list of containers on that task. Select the relevant container and a modal will roll from the right of the screen.

    Scroll down a bit and find Environment > Environment Variables. Here you can add environment variables. You can either set a static value (by using "value") or a Parameter Store value (by using "valueFrom").

    Save your new revision and remember to update your service to use the new definition.