Search code examples
amazon-web-servicesyamlenvironment-variablesaws-copilotaws-copilot-cli

How to do environment variable substitution with AWS CoPilot


I need to do environment variable substitution from a value from a SSM secret (secrets section in manifest). But escaping doesn't work as the doco mentioned.

variables:
  TEST1: "https://\\${DATABASE_SERVER}:5000/ReportServer?"
  TEST2: https://\${DATABASE_SERVER}:5000/ReportServer?
secrets:                      # Pass secrets from AWS Systems Manager (SSM) Parameter Store.
  DATABASE_SERVER: /database/server

I am getting the following error: ✘ interpolate environment variables for fisherassist-api manifest: environment variable "DATABASE_SERVER" is not defined


Solution

  • Firstly, this feature was released in copilot CLI v1.33.0 and I was using a older version. Got support from a ever so helpful mate from copilot gitter community 🙏🏼

    Secondly, composing Run time variables from other variables is not possible with Copilot at the moment. A similar feature request is there for this.

    There are 2 workarounds to achieve this.

    Approach 1

    1. Importing the SSM parameter in environment addon as a template parameter
    2. Output the composed value
    3. Import the output in the workload manifest as environment variable

    Approach 2

    This is a bit complicated workaround is possibly modifying the ENTRYPOINT

    1. Create an entrypoint script (entrypoint.sh): This script will export TEST1 and TEST2 using the current value of DATABASE_SERVER and then run your application.
    #!/bin/sh
    export TEST1="https://${DATABASE_SERVER}:5000/ReportServer?"
    export TEST2="https://${DATABASE_SERVER}:5000/ReportServer?"
    
    # Now execute the main process of the container
    
    exec "$@"
    
    1. Modify your Dockerfile
    # Copy the entrypoint script into the container
    COPY entrypoint.sh /entrypoint.sh
    # Set the script as the entry point
    ENTRYPOINT ["/entrypoint.sh"]
    CMD ["your_application_start_command"]
    
    1. In your workload manifest you need
    secrets:
      DATABASE_SERVER: /database/server
    

    This way the DATABASE_SERVER environment variable can be passed at runtime, and entrypoint.sh will set TEST1 and TEST2 accordingly.