Skip to content

Commit d825580

Browse files
authored
chore: add e2e and docs for env file (#3159)
resolves #1366 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.
1 parent c5fb606 commit d825580

File tree

12 files changed

+47
-565
lines changed

12 files changed

+47
-565
lines changed

e2e/multi-svc-app/copilot/front-end/manifest.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ image:
1313
dockerfile: front-end/Dockerfile
1414
context: front-end
1515
args:
16-
MAGIC_WORDS_ARG: "open sesame"
16+
MAGIC_VERB_ARG: "open"
1717
# Port exposed through your container to route traffic to it.
1818
port: 80
1919

@@ -44,4 +44,6 @@ storage:
4444
variables:
4545
TEST_JSON_ENV_VAR: "{}" # https://github.com/aws/copilot-cli/issues/1292
4646
TEST_BOOL_ENV_VAR: on # https://github.com/aws/copilot-cli/issues/1120
47-
TEST_JOB_CHECK_VAR: "no"
47+
TEST_JOB_CHECK_VAR: "no"
48+
49+
env_file: magic.env

e2e/multi-svc-app/front-end/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ RUN chmod +x ./e2e-service
2929

3030
# Define a build argument which we'll override in the copilot manifest
3131
# and make available to the service through the environment
32-
ARG MAGIC_WORDS_ARG
33-
ENV MAGIC_WORDS=$MAGIC_WORDS_ARG
34-
RUN echo $MAGIC_WORDS
32+
ARG MAGIC_VERB_ARG
33+
ENV MAGIC_VERB=$MAGIC_VERB_ARG
34+
RUN echo $MAGIC_VERB
3535

3636
# Start the service
3737
ENTRYPOINT ["./e2e-service"]

e2e/multi-svc-app/front-end/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ import (
1515
"github.com/julienschmidt/httprouter"
1616
)
1717

18-
// Get the env var "MAGIC_WORDS" for testing if the build arg was overridden.
19-
var magicWords string = os.Getenv("MAGIC_WORDS")
20-
var volumeName string = "efsTestVolume"
18+
var (
19+
// Get the env var "MAGIC_VERB" for testing if the build arg was overridden.
20+
magicVerb = os.Getenv("MAGIC_VERB")
21+
// Get the env var "MAGIC_WORD" for testing if the env var defined in env file is rendered.
22+
magicWord = os.Getenv("MAGIC_WORD")
23+
volumeName = "efsTestVolume"
24+
)
2125

2226
// SimpleGet just returns true no matter what
2327
func SimpleGet(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
@@ -50,6 +54,7 @@ func ServiceDiscoveryGet(w http.ResponseWriter, req *http.Request, ps httprouter
5054
func GetMagicWords(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
5155
log.Println("Get Succeeded")
5256
w.WriteHeader(http.StatusOK)
57+
magicWords := magicVerb + " " + magicWord
5358
log.Println(magicWords)
5459
w.Write([]byte(magicWords))
5560
}

e2e/multi-svc-app/magic.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MAGIC_WORD=sesame

site/content/docs/developing/environment-variables.en.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Environment variables are variables that are available to your service, based on the environment they're running in. Your service can reference them without having to define them. Environment variables are useful for when you want to pass in data to your service that's specific to a particular environment. For example, your test database name versus your production database name.
44

5-
Accessing environment variables is usually simply based on the language you're using. Here are some examples of getting an environment variable called `DATABASE_NAME` in a few different languages.
5+
Accessing environment variables is usually simply based on the language you're using. Here are some examples of getting an environment variable called `DATABASE_NAME` in a few different languages.
66

77
__Go__
88
```go
@@ -21,28 +21,28 @@ database_name = os.getenv('DATABASE_NAME')
2121

2222
## What are the Default Environment Variables?
2323

24-
By default, the AWS Copilot CLI passes in some default environment variables for your service to use.
24+
By default, the AWS Copilot CLI passes in some default environment variables for your service to use.
2525

26-
* `COPILOT_APPLICATION_NAME` - this is the name of the application this service is running in.
26+
* `COPILOT_APPLICATION_NAME` - this is the name of the application this service is running in.
2727
* `COPILOT_ENVIRONMENT_NAME` - this is the name of the environment the service is running in (test vs prod, for example)
28-
* `COPILOT_SERVICE_NAME` - this is the name of the current service.
29-
* `COPILOT_LB_DNS` - this is the DNS name of the Load Balancer (if it exists) such as _kudos-Publi-MC2WNHAIOAVS-588300247.us-west-2.elb.amazonaws.com_. Note: if you're using a custom domain name, this value will still be the Load Balancer's DNS name.
28+
* `COPILOT_SERVICE_NAME` - this is the name of the current service.
29+
* `COPILOT_LB_DNS` - this is the DNS name of the Load Balancer (if it exists) such as _kudos-Publi-MC2WNHAIOAVS-588300247.us-west-2.elb.amazonaws.com_. Note: if you're using a custom domain name, this value will still be the Load Balancer's DNS name.
3030
* `COPILOT_SERVICE_DISCOVERY_ENDPOINT` - this is the endpoint to add after a service name to talk to another service in your environment via service discovery. The value is `{env name}.{app name}.local`. For more information about service discovery, check out our [Service Discovery guide](../developing/service-discovery.en.md).
3131

3232
## How do I add my own Environment Variables?
3333

34-
Adding your own environment variable is easy. You can add them directly to your [manifest](../manifest/overview.en.md) in the `variables` section. The following snippet will pass a environment variable called `LOG_LEVEL` to your service, with the value set to `debug`.
34+
Adding your own environment variable is easy. You can add them directly to your [manifest](../manifest/overview.en.md) in the `variables` section. The following snippet will pass a environment variable called `LOG_LEVEL` to your service, with the value set to `debug`.
3535

3636
```yaml
37-
# in copilot/{service name}/manifest.yml
37+
# in copilot/{service name}/manifest.yml
3838
variables:
3939
LOG_LEVEL: debug
4040
```
4141
4242
You can also pass in a specific value for an environment variable based on the environment. We'll follow the same example as above, by setting the log level, but overriding the value to be `info` in our production environment. Changes to your manifest take effect when you deploy them, so changing them locally is safe.
4343

4444
```yaml
45-
# in copilot/{service name}/manifest.yml
45+
# in copilot/{service name}/manifest.yml
4646
variables:
4747
LOG_LEVEL: debug
4848
@@ -56,6 +56,20 @@ Here's a quick guide showing you how to add environment variables to your app by
5656

5757
![Editing the manifest to add env vars](https://raw.githubusercontent.com/kohidave/ecs-cliv2-demos/master/env-vars-edit.svg?sanitize=true)
5858

59+
Additionally, if you want to add environment variables in bulk, you can list them in an [env file](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html#taskdef-envfiles-considerations). And then specify its path (from the root of the workspace) in the `env_file` field of your[manifest](../manifest/overview.en.md).
60+
61+
```yaml
62+
# in copilot/{service name}/manifest.yml
63+
env_file: log.env
64+
```
65+
66+
And in `log.env` we could have
67+
```
68+
#This is a comment and will be ignored
69+
LOG_LEVEL=debug
70+
LOG_INFO=all
71+
```
72+
5973
## How do I know the name of my DynamoDB table, S3 bucket, RDS database, etc?
6074
6175
When using the Copilot CLI to provision additional AWS resources such as DynamoDB tables, S3 buckets, databases, etc., any output values will be passed in as environment variables to your app. For more information, check out the [additional resources guide](../developing/additional-aws-resources.en.md).

0 commit comments

Comments
 (0)