Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ec2): add support for environment files and variables in systemd #29629

Merged
merged 11 commits into from
May 1, 2024
31 changes: 30 additions & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,10 @@ export class InitService extends InitElement {
throw new Error(`SystemD executables must use an absolute path, got '${options.command}'`);
}

if (options.environmentFiles?.some(file => !file.startsWith('/'))) {
throw new Error('SystemD environment files must use absolute paths');
}

const lines = [
'[Unit]',
...(options.description ? [`Description=${options.description}`] : []),
Expand All @@ -845,6 +849,17 @@ export class InitService extends InitElement {
...(options.user ? [`User=${options.user}`] : []),
...(options.group ? [`Group=${options.user}`] : []),
...(options.keepRunning ?? true ? ['Restart=always'] : []),
...(
options.environmentFiles
? options.environmentFiles.map(file => `EnvironmentFile=${file}`)
: []
),
...(
options.environmentVariables
? Object.entries(options.environmentVariables)
.map(([key, value]) => `Environment="${key}=${value}"`)
: []
),
'[Install]',
'WantedBy=multi-user.target',
];
Expand Down Expand Up @@ -1108,4 +1123,18 @@ export interface SystemdConfigFileOptions {
* @default true
*/
readonly afterNetwork?: boolean;
}

/**
* Include environment variables when process is running
*
* @default - No environment variables set
*/
readonly environmentVariables?: Record<string, string>;

/**
razin99 marked this conversation as resolved.
Show resolved Hide resolved
* Include file to load environment variables from when process is running
*
* @default - No environment files
*/
readonly environmentFiles?: string[];
}
29 changes: 29 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/cfn-init-element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,35 @@ describe('InitService', () => {
expect(capture).toContain('Group=ec2-user');
expect(capture).toContain('Description=my service');
});

test('can create systemd file with environment variables', () => {
// WHEN
const file = ec2.InitService.systemdConfigFile('myserver', {
command: '/start/my/service',
cwd: '/my/dir',
user: 'ec2-user',
group: 'ec2-user',
description: 'my service',
environmentFiles: ['/files/one', '/files/two'],
environmentVariables: {
HELLO: 'WORLD',
},
});

// THEN
const bindOptions = defaultOptions(InitPlatform.LINUX);
const rendered = file._bind(bindOptions).config;
expect(rendered).toEqual({
'/etc/systemd/system/myserver.service': expect.objectContaining({
content: expect.any(String),
}),
});

const capture = rendered['/etc/systemd/system/myserver.service'].content;
expect(capture).toContain('EnvironmentFile=/files/one');
expect(capture).toContain('EnvironmentFile=/files/two');
expect(capture).toContain('Environment="HELLO=WORLD"');
});
});

describe('InitSource', () => {
Expand Down
Loading