Skip to content

Commit 90f1aa9

Browse files
authored
feat(ec2): add support for environment files and variables in systemd (#29629)
### Reason for this change Allow users to define `Environment` and `EnvironmentFile` in a systemd service file. ### Description of changes Added 2 new properties for `ec2.InitService.systemdConfigFile`: 1. `environmentVariables` which is a string key value pair 2. `environmentFiles` which is a list of file paths ### Description of how you validated changes I have added a unit test. Documentations referred to: - https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#EnvironmentFile= - https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#Environment= ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5dd72b8 commit 90f1aa9

File tree

10 files changed

+327
-189
lines changed

10 files changed

+327
-189
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.instance-init.js.snapshot/cdk.out

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.instance-init.js.snapshot/integ-init.assets.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.instance-init.js.snapshot/integ-init.template.json

+75-57
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
"IntegInitVpcPublicSubnet1Subnet41A6F6D4": {
1919
"Type": "AWS::EC2::Subnet",
2020
"Properties": {
21-
"VpcId": {
22-
"Ref": "IntegInitVpc0D4FCCB3"
23-
},
2421
"AvailabilityZone": {
2522
"Fn::Select": [
2623
0,
@@ -44,21 +41,24 @@
4441
"Key": "Name",
4542
"Value": "integ-init/IntegInitVpc/PublicSubnet1"
4643
}
47-
]
44+
],
45+
"VpcId": {
46+
"Ref": "IntegInitVpc0D4FCCB3"
47+
}
4848
}
4949
},
5050
"IntegInitVpcPublicSubnet1RouteTable837CD5FB": {
5151
"Type": "AWS::EC2::RouteTable",
5252
"Properties": {
53-
"VpcId": {
54-
"Ref": "IntegInitVpc0D4FCCB3"
55-
},
5653
"Tags": [
5754
{
5855
"Key": "Name",
5956
"Value": "integ-init/IntegInitVpc/PublicSubnet1"
6057
}
61-
]
58+
],
59+
"VpcId": {
60+
"Ref": "IntegInitVpc0D4FCCB3"
61+
}
6262
}
6363
},
6464
"IntegInitVpcPublicSubnet1RouteTableAssociation00D33741": {
@@ -75,12 +75,12 @@
7575
"IntegInitVpcPublicSubnet1DefaultRoute5BB90E8C": {
7676
"Type": "AWS::EC2::Route",
7777
"Properties": {
78-
"RouteTableId": {
79-
"Ref": "IntegInitVpcPublicSubnet1RouteTable837CD5FB"
80-
},
8178
"DestinationCidrBlock": "0.0.0.0/0",
8279
"GatewayId": {
8380
"Ref": "IntegInitVpcIGWF019AC85"
81+
},
82+
"RouteTableId": {
83+
"Ref": "IntegInitVpcPublicSubnet1RouteTable837CD5FB"
8484
}
8585
},
8686
"DependsOn": [
@@ -102,15 +102,15 @@
102102
"IntegInitVpcPublicSubnet1NATGateway46F32F7F": {
103103
"Type": "AWS::EC2::NatGateway",
104104
"Properties": {
105-
"SubnetId": {
106-
"Ref": "IntegInitVpcPublicSubnet1Subnet41A6F6D4"
107-
},
108105
"AllocationId": {
109106
"Fn::GetAtt": [
110107
"IntegInitVpcPublicSubnet1EIP46FCC3D6",
111108
"AllocationId"
112109
]
113110
},
111+
"SubnetId": {
112+
"Ref": "IntegInitVpcPublicSubnet1Subnet41A6F6D4"
113+
},
114114
"Tags": [
115115
{
116116
"Key": "Name",
@@ -126,9 +126,6 @@
126126
"IntegInitVpcPublicSubnet2Subnet9A384F16": {
127127
"Type": "AWS::EC2::Subnet",
128128
"Properties": {
129-
"VpcId": {
130-
"Ref": "IntegInitVpc0D4FCCB3"
131-
},
132129
"AvailabilityZone": {
133130
"Fn::Select": [
134131
1,
@@ -152,21 +149,24 @@
152149
"Key": "Name",
153150
"Value": "integ-init/IntegInitVpc/PublicSubnet2"
154151
}
155-
]
152+
],
153+
"VpcId": {
154+
"Ref": "IntegInitVpc0D4FCCB3"
155+
}
156156
}
157157
},
158158
"IntegInitVpcPublicSubnet2RouteTableF7E8F920": {
159159
"Type": "AWS::EC2::RouteTable",
160160
"Properties": {
161-
"VpcId": {
162-
"Ref": "IntegInitVpc0D4FCCB3"
163-
},
164161
"Tags": [
165162
{
166163
"Key": "Name",
167164
"Value": "integ-init/IntegInitVpc/PublicSubnet2"
168165
}
169-
]
166+
],
167+
"VpcId": {
168+
"Ref": "IntegInitVpc0D4FCCB3"
169+
}
170170
}
171171
},
172172
"IntegInitVpcPublicSubnet2RouteTableAssociationB816F9F3": {
@@ -183,12 +183,12 @@
183183
"IntegInitVpcPublicSubnet2DefaultRoute2393995F": {
184184
"Type": "AWS::EC2::Route",
185185
"Properties": {
186-
"RouteTableId": {
187-
"Ref": "IntegInitVpcPublicSubnet2RouteTableF7E8F920"
188-
},
189186
"DestinationCidrBlock": "0.0.0.0/0",
190187
"GatewayId": {
191188
"Ref": "IntegInitVpcIGWF019AC85"
189+
},
190+
"RouteTableId": {
191+
"Ref": "IntegInitVpcPublicSubnet2RouteTableF7E8F920"
192192
}
193193
},
194194
"DependsOn": [
@@ -210,15 +210,15 @@
210210
"IntegInitVpcPublicSubnet2NATGateway9CCB4A9C": {
211211
"Type": "AWS::EC2::NatGateway",
212212
"Properties": {
213-
"SubnetId": {
214-
"Ref": "IntegInitVpcPublicSubnet2Subnet9A384F16"
215-
},
216213
"AllocationId": {
217214
"Fn::GetAtt": [
218215
"IntegInitVpcPublicSubnet2EIP553B40DC",
219216
"AllocationId"
220217
]
221218
},
219+
"SubnetId": {
220+
"Ref": "IntegInitVpcPublicSubnet2Subnet9A384F16"
221+
},
222222
"Tags": [
223223
{
224224
"Key": "Name",
@@ -234,9 +234,6 @@
234234
"IntegInitVpcPrivateSubnet1Subnet259B51C1": {
235235
"Type": "AWS::EC2::Subnet",
236236
"Properties": {
237-
"VpcId": {
238-
"Ref": "IntegInitVpc0D4FCCB3"
239-
},
240237
"AvailabilityZone": {
241238
"Fn::Select": [
242239
0,
@@ -260,21 +257,24 @@
260257
"Key": "Name",
261258
"Value": "integ-init/IntegInitVpc/PrivateSubnet1"
262259
}
263-
]
260+
],
261+
"VpcId": {
262+
"Ref": "IntegInitVpc0D4FCCB3"
263+
}
264264
}
265265
},
266266
"IntegInitVpcPrivateSubnet1RouteTableCB37994B": {
267267
"Type": "AWS::EC2::RouteTable",
268268
"Properties": {
269-
"VpcId": {
270-
"Ref": "IntegInitVpc0D4FCCB3"
271-
},
272269
"Tags": [
273270
{
274271
"Key": "Name",
275272
"Value": "integ-init/IntegInitVpc/PrivateSubnet1"
276273
}
277-
]
274+
],
275+
"VpcId": {
276+
"Ref": "IntegInitVpc0D4FCCB3"
277+
}
278278
}
279279
},
280280
"IntegInitVpcPrivateSubnet1RouteTableAssociation067DEF9D": {
@@ -291,21 +291,18 @@
291291
"IntegInitVpcPrivateSubnet1DefaultRoute654ACECF": {
292292
"Type": "AWS::EC2::Route",
293293
"Properties": {
294-
"RouteTableId": {
295-
"Ref": "IntegInitVpcPrivateSubnet1RouteTableCB37994B"
296-
},
297294
"DestinationCidrBlock": "0.0.0.0/0",
298295
"NatGatewayId": {
299296
"Ref": "IntegInitVpcPublicSubnet1NATGateway46F32F7F"
297+
},
298+
"RouteTableId": {
299+
"Ref": "IntegInitVpcPrivateSubnet1RouteTableCB37994B"
300300
}
301301
}
302302
},
303303
"IntegInitVpcPrivateSubnet2Subnet1643B059": {
304304
"Type": "AWS::EC2::Subnet",
305305
"Properties": {
306-
"VpcId": {
307-
"Ref": "IntegInitVpc0D4FCCB3"
308-
},
309306
"AvailabilityZone": {
310307
"Fn::Select": [
311308
1,
@@ -329,21 +326,24 @@
329326
"Key": "Name",
330327
"Value": "integ-init/IntegInitVpc/PrivateSubnet2"
331328
}
332-
]
329+
],
330+
"VpcId": {
331+
"Ref": "IntegInitVpc0D4FCCB3"
332+
}
333333
}
334334
},
335335
"IntegInitVpcPrivateSubnet2RouteTable030EC93B": {
336336
"Type": "AWS::EC2::RouteTable",
337337
"Properties": {
338-
"VpcId": {
339-
"Ref": "IntegInitVpc0D4FCCB3"
340-
},
341338
"Tags": [
342339
{
343340
"Key": "Name",
344341
"Value": "integ-init/IntegInitVpc/PrivateSubnet2"
345342
}
346-
]
343+
],
344+
"VpcId": {
345+
"Ref": "IntegInitVpc0D4FCCB3"
346+
}
347347
}
348348
},
349349
"IntegInitVpcPrivateSubnet2RouteTableAssociation6B52BD72": {
@@ -360,12 +360,12 @@
360360
"IntegInitVpcPrivateSubnet2DefaultRoute6A10B6EA": {
361361
"Type": "AWS::EC2::Route",
362362
"Properties": {
363-
"RouteTableId": {
364-
"Ref": "IntegInitVpcPrivateSubnet2RouteTable030EC93B"
365-
},
366363
"DestinationCidrBlock": "0.0.0.0/0",
367364
"NatGatewayId": {
368365
"Ref": "IntegInitVpcPublicSubnet2NATGateway9CCB4A9C"
366+
},
367+
"RouteTableId": {
368+
"Ref": "IntegInitVpcPrivateSubnet2RouteTable030EC93B"
369369
}
370370
}
371371
},
@@ -383,11 +383,11 @@
383383
"IntegInitVpcVPCGW85EDC292": {
384384
"Type": "AWS::EC2::VPCGatewayAttachment",
385385
"Properties": {
386-
"VpcId": {
387-
"Ref": "IntegInitVpc0D4FCCB3"
388-
},
389386
"InternetGatewayId": {
390387
"Ref": "IntegInitVpcIGWF019AC85"
388+
},
389+
"VpcId": {
390+
"Ref": "IntegInitVpc0D4FCCB3"
391391
}
392392
}
393393
},
@@ -513,7 +513,7 @@
513513
]
514514
}
515515
},
516-
"Instance255F35265a0c5f577d761edb0": {
516+
"Instance255F3526574cbd507dfce8b71": {
517517
"Type": "AWS::EC2::Instance",
518518
"Properties": {
519519
"AvailabilityZone": {
@@ -553,23 +553,23 @@
553553
"Fn::Join": [
554554
"",
555555
[
556-
"#!/bin/bash\n# fingerprint: 89cb2e09a1c3d4c8\n(\n set +e\n /opt/aws/bin/cfn-init -v --region ",
556+
"#!/bin/bash\n# fingerprint: 4f2827c68bde31b4\n(\n set +e\n /opt/aws/bin/cfn-init -v --region ",
557557
{
558558
"Ref": "AWS::Region"
559559
},
560560
" --stack ",
561561
{
562562
"Ref": "AWS::StackName"
563563
},
564-
" --resource Instance255F35265a0c5f577d761edb0 -c default\n /opt/aws/bin/cfn-signal -e $? --region ",
564+
" --resource Instance255F3526574cbd507dfce8b71 -c default\n /opt/aws/bin/cfn-signal -e $? --region ",
565565
{
566566
"Ref": "AWS::Region"
567567
},
568568
" --stack ",
569569
{
570570
"Ref": "AWS::StackName"
571571
},
572-
" --resource Instance255F35265a0c5f577d761edb0\n cat /var/log/cfn-init.log >&2\n)"
572+
" --resource Instance255F3526574cbd507dfce8b71\n cat /var/log/cfn-init.log >&2\n)"
573573
]
574574
]
575575
}
@@ -641,6 +641,24 @@
641641
"group": "root"
642642
}
643643
}
644+
},
645+
"service": {
646+
"files": {
647+
"/myvars.env": {
648+
"content": "OTHER_VAR=\"im from the file :3\"",
649+
"encoding": "plain",
650+
"mode": "000644",
651+
"owner": "root",
652+
"group": "root"
653+
},
654+
"/etc/systemd/system/myapp.service": {
655+
"content": "[Unit]\nAfter=network.target\n[Service]\nExecStart=/bin/bash -c \"echo HELLO_WORLD=${MY_VAR} | FROM_FILE=${OTHER_VAR}\"\nRestart=always\nEnvironmentFile=/myvars.env\nEnvironment=\"MY_VAR=its me :)\"\n[Install]\nWantedBy=multi-user.target",
656+
"encoding": "plain",
657+
"mode": "000644",
658+
"owner": "root",
659+
"group": "root"
660+
}
661+
}
644662
}
645663
},
646664
"AWS::CloudFormation::Authentication": {

packages/@aws-cdk-testing/framework-integ/test/aws-ec2/test/integ.instance-init.js.snapshot/integ.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)