Skip to content

Commit

Permalink
feat(env-var): support extra environment variables for backend contai…
Browse files Browse the repository at this point in the history
…ners
  • Loading branch information
briancaffey committed Sep 29, 2021
1 parent 7224007 commit 23bf980
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
9 changes: 8 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ export CERTIFICATE_ARN=abc123
export ZONE_NAME=example.com
export API_DOMAIN_NAME=api.example.com
export BACKEND_API_URL=https://api.example.com
export FRONTEND_URL=https://app.example.com
export FRONTEND_URL=https://app.example.com

# email settings for sending email with gmail
export EMAIL_HOST=smtp.gmail.com
export EMAIL_HOST_USER=email@gmail.com
export EMAIL_HOST_PASSWORD=email-password
export EMAIL_PORT=587
export ADMINS=email@gmail.com
13 changes: 13 additions & 0 deletions src/django-ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export interface DjangoEcsProps {
*/
readonly frontendUrl?: string;

/**
* Extra Environment Variables to set in the backend container
*/
readonly environmentVariables?: { [key: string]: string };

}

/**
Expand Down Expand Up @@ -192,6 +197,14 @@ export class DjangoEcs extends cdk.Construct {
ZONE_NAME: props.zoneName ?? '',
};

if (props.environmentVariables ?? false) {
// add environment variables to environment
// merge the two objects (change const to let for `environment` declaration)
// environment = { ...environment, ...props.environmentVariables };
// or with Object.assign
Object.assign(environment, props.environmentVariables);
}

taskDefinition.addContainer('backendContainer', {
image: this.image,
environment,
Expand Down
17 changes: 12 additions & 5 deletions src/django-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
*
**/

import * as cdk from '@aws-cdk/core';
import { DjangoEcs } from './index';
import { StaticSite } from './index';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as route53 from '@aws-cdk/aws-route53';
import * as cdk from '@aws-cdk/core';
import { DjangoEcs, StaticSite } from './index';

/**
* Django and Vue application stack props
Expand Down Expand Up @@ -58,8 +57,8 @@ export class DjangoVue extends cdk.Construct {
domainName: props.domainName,
hostedZone: route53.HostedZone.fromLookup(scope, 'HostedZone', {
domainName: props.zoneName,
})
})
}),
});
}

const apiBackend = new DjangoEcs(scope, 'DjangoEcsSample', {
Expand All @@ -71,6 +70,14 @@ export class DjangoVue extends cdk.Construct {
useEcsExec: true,
frontendUrl: process.env.FRONTEND_URL,
certificateArn: certificate.certificateArn,
environmentVariables: {
EMAIL_HOST: process.env.EMAIL_HOST || 'smtp.gmail.com',
EMAIL_PORT: process.env.EMAIL_PORT || '1025',
EMAIL_HOST_USER: process.env.EMAIL_HOST_USER || 'app@domain.com',
EMAIL_HOST_PASSWORD: process.env.EMAIL_HOST_PASSWORD || 'password',
// comma separated list of admin email addresses
ADMINS: process.env.ADMINS || 'superuser@email.com,admin@email.com,',
},
});

new StaticSite(scope, 'StaticSiteSample', {
Expand Down
1 change: 1 addition & 0 deletions src/integ/integ.django-ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const construct = new DjangoEcs(stack, 'DjangoEcsSample', {
zoneName: process.env.ZONE_NAME,
useEcsExec: true,
frontendUrl: process.env.FRONTEND_URL,
environmentVariables: { FOO_ENV_VAR: 'foo-env-var' },

// certificateArn: process.env.CERTIFICATE_ARN,
});
Expand Down
2 changes: 1 addition & 1 deletion test/django-step-by-step

0 comments on commit 23bf980

Please sign in to comment.