-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1019 lines (952 loc) · 35.9 KB
/
Copy pathindex.html
File metadata and controls
1019 lines (952 loc) · 35.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Using AWS CDK outside its comfort zone</title>
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/white.css">
<link rel="stylesheet" href="assets/custom.css">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown data-visibility="uncounted">
<textarea data-template>
## Using AWS CDK<br/>outside its comfort zone
Michael Käufl
German CDK Happy Hour<br/>
2022-04-27
</textarea>
</section>
<section>
<h3>Background</h3>
<div class="container">
<div class="col">
<ul>
<li>Most infrastructure is managed in a single repository</li>
<li>Project specific CodePipelines and CloudFormation stacks</li>
</ul>
</div>
<div class="col">
<img data-src="assets/pipeline.png" max-height="100px">
</div>
</div>
</section>
<section>
<h3>Goal 1: Generate CloudFormation templates with AWS CDK</h3>
</section>
<section>
<pre><code class="r-stretch language-yaml" data-trim data-line-numbers>
---
version: 0.2
phases:
install:
runtime-versions:
nodejs: 14 // 😠
python: 3.9 // 😠
commands:
- npm install -g aws-cdk@">=2.21.1"
- python3 -m pip install -r requirements.txt
build:
commands:
- cdk synth > ${DEPLOY_TEMPLATE_FILE_NAME}
- cfn-lint ${DEPLOY_TEMPLATE_FILE_NAME}
artifacts:
type: zip
files:
- ${DEPLOY_TEMPLATE_FILE_NAME}
</code></pre>
</section>
<section>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="1-11,30">
from aws_cdk import Stack, aws_codebuild as codebuild
from constructs import Construct
class PlaygroundStack(Stack):
def __init__(self, scope: Construct, id_: str, **kwargs) -> None:
super().__init__(scope, id_, **kwargs)
codebuild.Project(
self,
"PlaygroundCodeBuild",
source=codebuild.Source.git_hub(
owner="owner",
repo="repo",
branch_or_ref="main",
clone_depth=1,
fetch_submodules=False,
report_build_status=True,
webhook=False,
),
build_spec=codebuild.BuildSpec.from_source_filename("buildspec.yml"),
cache=None,
description="This is a description.",
environment=codebuild.BuildEnvironment(
build_image=codebuild.LinuxBuildImage.STANDARD_5_0,
compute_type=codebuild.ComputeType.SMALL,
environment_variables={},
privileged=True,
),
)
</code></pre>
</section>
<section>
<h4>Works, but …</h4>
<pre><code class="language-yaml" data-trim data-line-numbers="1-3,14-15,71-72,101-197">
Resources:
PlaygroundCodeBuildRoleCBACFC80:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Version: "2012-10-17"
Metadata:
aws:cdk:path: playground/PlaygroundCodeBuild/Role/Resource
PlaygroundCodeBuildRoleDefaultPolicy68E453E0:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Effect: Allow
Resource:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: PlaygroundCodeBuild4D1EEA94
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":logs:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :log-group:/aws/codebuild/
- Ref: PlaygroundCodeBuild4D1EEA94
- :*
- Action:
- codebuild:CreateReportGroup
- codebuild:CreateReport
- codebuild:UpdateReport
- codebuild:BatchPutTestCases
- codebuild:BatchPutCodeCoverages
Effect: Allow
Resource:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- ":codebuild:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- :report-group/
- Ref: PlaygroundCodeBuild4D1EEA94
- -*
Version: "2012-10-17"
PolicyName: PlaygroundCodeBuildRoleDefaultPolicy68E453E0
Roles:
- Ref: PlaygroundCodeBuildRoleCBACFC80
Metadata:
aws:cdk:path: playground/PlaygroundCodeBuild/Role/DefaultPolicy/Resource
PlaygroundCodeBuild4D1EEA94:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/standard:5.0
ImagePullCredentialsType: CODEBUILD
PrivilegedMode: true
Type: LINUX_CONTAINER
ServiceRole:
Fn::GetAtt:
- PlaygroundCodeBuildRoleCBACFC80
- Arn
Source:
BuildSpec: buildspec.yml
GitCloneDepth: 1
Location: https://github.com/owner/repo.git
ReportBuildStatus: true
Type: GITHUB
Cache:
Type: NO_CACHE
Description: This is a description.
EncryptionKey: alias/aws/s3
SourceVersion: main
Triggers:
Webhook: false
Metadata:
aws:cdk:path: playground/PlaygroundCodeBuild/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/zWMQQ7CIBBFz9I9jGldGNe9AMEDGDpgnJYyCYWYhnB3K+rqv/z38wcYeug789ok2kV6mqDcksFFaLdxjujE4e4F2bopk7dQVOTZYRLjI/ywCjIrFM3efdqWij3h3kaNam3qf3rwyMFSIg5VqD09OZzOcIVLN29EMuaQaHWgv/kGar52TacAAAA=
Metadata:
aws:cdk:path: playground/CDKMetadata/Default
Condition: CDKMetadataAvailable
Conditions:
CDKMetadataAvailable:
Fn::Or:
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- af-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-east-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-northeast-2
- Fn::Equals:
- Ref: AWS::Region
- ap-south-1
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-1
- Fn::Equals:
- Ref: AWS::Region
- ap-southeast-2
- Fn::Equals:
- Ref: AWS::Region
- ca-central-1
- Fn::Equals:
- Ref: AWS::Region
- cn-north-1
- Fn::Equals:
- Ref: AWS::Region
- cn-northwest-1
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- eu-central-1
- Fn::Equals:
- Ref: AWS::Region
- eu-north-1
- Fn::Equals:
- Ref: AWS::Region
- eu-south-1
- Fn::Equals:
- Ref: AWS::Region
- eu-west-1
- Fn::Equals:
- Ref: AWS::Region
- eu-west-2
- Fn::Equals:
- Ref: AWS::Region
- eu-west-3
- Fn::Equals:
- Ref: AWS::Region
- me-south-1
- Fn::Equals:
- Ref: AWS::Region
- sa-east-1
- Fn::Equals:
- Ref: AWS::Region
- us-east-1
- Fn::Equals:
- Ref: AWS::Region
- us-east-2
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- us-west-1
- Fn::Equals:
- Ref: AWS::Region
- us-west-2
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value<String>
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
CheckBootstrapVersion:
Assertions:
- Assert:
Fn::Not:
- Fn::Contains:
- - "1"
- "2"
- "3"
- "4"
- "5"
- Ref: BootstrapVersion
AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.
</code></pre>
</section>
<section>
<h4>Get rid of useless bloat</h4>
<pre><code class="r-stretch language-yaml" data-trim data-line-numbers>
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/…
Metadata:
aws:cdk:path: playground/CDKMetadata/Default
Condition: CDKMetadataAvailable
Conditions:
CDKMetadataAvailable:
Fn::Or:
- Fn::Or:
- Fn::Equals:
- Ref: AWS::Region
- af-south-1
…
</code></pre>
<p><code>cdk.json</code>:</p>
<pre><code class="r-stretch language-json" data-trim data-line-numbers>
{
"app": "python3 app.py",
"versionReporting": false,
"context": {}
}
</code></pre>
</section>
<section>
<h3>Goal 2: Use existing resources</h3>
</section>
<section>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="7-11">
from aws_cdk import Fn, aws_codebuild as codebuild, aws_iam as iam
codebuild.Project(
self,
"PlaygroundCodeBuild",
role=iam.Role.from_role_arn(
self,
"CodeBuildRole",
Fn.import_value("PlaygroundCodeBuildRoleARN"),
),
)
</code></pre>
<pre class="fragment" data-fragment-index="1"><code class="r-stretch language-yaml" data-trim data-line-numbers>
- Fn::Select:
- 1
- Fn::Split:
- /
- Fn::Select:
- 5
- Fn::Split:
- ":"
- Fn::ImportValue: PlaygroundCodeBuildRoleARN
</code></pre>
<p class="fragment" data-fragment-index="1">🤔🧐</p>
</section>
<section>
<p>
<blockquote cite="https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_iam/Role.html#aws_cdk.aws_iam.Role.from_role_arn">
If the imported Role ARN is a Token (… Fn.importValue()) and the
referenced role has a path, the roleName property will not resolve
to the correct value. Instead it will resolve to
the first path component.
</blockquote>
<br />
<small class="fragment">
<code>Role.from_role_name()</code> did not exist at the time.
</small>
</p>
</section>
<section>
<p>
The CloudFormation template uses only the ARN and the Name of the role,
but it's not possible to just provide those two and be done with it.
</p>
<p>
You have to either
<ul>
<li>use <code>iam.IRole</code> or</li>
<li>fall back to Level 1 constructs.</li>
</ul>
</p>
<p>
It's not possible to mix L1 and L2/3 constructs directly.
</p>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="1,4">
codebuild.Project(
self,
"PlaygroundCodeBuild",
role=iam.CfnRole(…), # does not work; expects iam.IRole
)
</code></pre>
<p>
<small>
L1 constructs: 1:1 mappings of the corresponding
CloudFormation resources
</small>
</p>
</section>
<section>
<h4>Idea: Use a custom class derived from <code>iam.IRole</code></h4>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="5-10,13">
from aws_cdk import Fn, aws_codebuild as codebuild, aws_iam as iam
from .imported_role import ImportedRole
imported_role = ImportedRole(
self,
"CodeBuildRoleImported",
arn_export_name="PlaygroundCodeBuildRoleARN",
name_export_name="PlaygroundCodeBuildRoleName",
)
codebuild.Project(
self, "PlaygroundCodeBuild", role=imported_role
)
</code></pre>
</section>
<section>
<h4>How to implement a custom <code>iam.IRole</code> in Python?</h4>
<p>The documentation and the TypeScript code are a start,<br/>but raise more questions:</p>
<ul>
<li class="fragment"><code>export class Role extends Resource implements IRole</code></li>
<li class="fragment"><code>implements</code> == multiple inheritance in Python? Not quite</li>
<li class="fragment">
I'm not the only one with this problem:
<ul>
<li><a href="https://github.com/aws/jsii/issues/576">aws/jsii#576</a>: python: Implementing an interface is non-obvious</li>
<li><a href="https://github.com/aws/jsii/issues/738">aws/jsii#738</a>: How to implement an interface in Python?</li>
</ul>
</li>
</ul>
<p>
<small>
https://github.com/aws/aws-cdk/blob/v2-main/packages/@aws-cdk/aws-iam/lib/role.ts
</small>
</p>
</section>
<section>
<h4>Where's CDK's Python code?</h4>
<p>On your filesystem:<br/><code>/usr/local/lib/python3.9/site-packages</code><br/><code>/aws_cdk/aws_iam/__init__.py</code></p>
<pre><code class="language-python" data-trim data-line-numbers>
@jsii.implements(IRole)
class Role(
_Resource_45bc6135,
metaclass=jsii.JSIIMeta,
jsii_type="aws-cdk-lib.aws_iam.Role",
):
def __init__(
self,
scope: constructs.Construct,
id: builtins.str,
*,
assumed_by: IPrincipal,
description: typing.Optional[builtins.str] = None,
external_ids: typing.Optional[typing.Sequence[builtins.str]] = None,
inline_policies: typing.Optional[typing.Mapping[builtins.str, PolicyDocument]] = None,
managed_policies: typing.Optional[typing.Sequence[IManagedPolicy]] = None,
max_session_duration: typing.Optional[_Duration_4839e8c3] = None,
path: typing.Optional[builtins.str] = None,
permissions_boundary: typing.Optional[IManagedPolicy] = None,
role_name: typing.Optional[builtins.str] = None,
) -> None:
props = RoleProps(
assumed_by=assumed_by,
description=description,
external_ids=external_ids,
inline_policies=inline_policies,
managed_policies=managed_policies,
max_session_duration=max_session_duration,
path=path,
permissions_boundary=permissions_boundary,
role_name=role_name,
)
jsii.create(self.__class__, self, [scope, id, props])
</code></pre>
</section>
<section>
<h4>The minimal <code>ImportedRole</code></h4>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="5-6,14-17,19-22">
import jsii
from aws_cdk import Fn, Resource, aws_iam as iam
from constructs import Construct
@jsii.implements(iam.IRole)
class ImportedRole(Resource, metaclass=jsii.JSIIMeta):
def __init__(
self, scope: Construct, id_: str, *, arn_export_name: str, name_export_name: str
):
super().__init__(scope, id_)
self._role_arn = Fn.import_value(arn_export_name)
self._role_name = Fn.import_value(name_export_name)
@property # type: ignore[misc]
@jsii.member(jsii_name="roleArn")
def role_arn(self) -> str:
return self._role_arn
@property # type: ignore[misc]
@jsii.member(jsii_name="roleName")
def role_name(self) -> str:
return self._role_name
</code></pre>
</section>
<section>
<pre><code data-trim data-line-numbers="2">
jsii.errors.JavaScriptError:
TypeError: this.role.addToPrincipalPolicy is not a function
at Project.addToRolePolicy (/tmp/jsii-kernel-cjDHem/node_modules/aws-cdk-lib/aws-codebuild/lib/project.js:1:1599)
at new Project (/tmp/jsii-kernel-cjDHem/node_modules/aws-cdk-lib/aws-codebuild/lib/project.js:1:7253)
at /tmp/tmpflnb1rup/lib/program.js:8420:58
at Kernel._wrapSandboxCode (/tmp/tmpflnb1rup/lib/program.js:8848:24)
at Kernel._create (/tmp/tmpflnb1rup/lib/program.js:8420:34)
at Kernel.create (/tmp/tmpflnb1rup/lib/program.js:8161:29)
at KernelHost.processRequest (/tmp/tmpflnb1rup/lib/program.js:9769:36)
at KernelHost.run (/tmp/tmpflnb1rup/lib/program.js:9732:22)
at Immediate._onImmediate (/tmp/tmpflnb1rup/lib/program.js:9733:46)
at processImmediate (node:internal/timers:466:21)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/code/app.py", line 13, in <module>
PlaygroundStack(app, "playground")
File "/usr/lib/python3.9/site-packages/jsii/_runtime.py", line 86, in __call__
inst = super().__call__(*args, **kwargs)
File "/code/playground/playground_stack.py", line 25, in __init__
codebuild.Project(
File "/usr/lib/python3.9/site-packages/jsii/_runtime.py", line 86, in __call__
inst = super().__call__(*args, **kwargs)
File "/usr/lib/python3.9/site-packages/aws_cdk/aws_codebuild/__init__.py", line 9972, in __init__
jsii.create(self.__class__, self, [scope, id, props])
File "/usr/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 302, in create
obj.__jsii_ref__ = _callback_till_result(self, response, CreateResponse)
File "/usr/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 235, in _callback_till_result
response = kernel.sync_complete(response.cbid, None, result, response_type)
File "/usr/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 401, in sync_complete
return self.provider.sync_complete(
File "/usr/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 382, in sync_complete
resp = self._process.send(_CompleteRequest(complete=request), response_type)
File "/usr/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 326, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: this.role.addToPrincipalPolicy is not a function
</code></pre>
</section>
<section>
<h4>The first “working” <code>ImportedRole</code></h4>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="24-31">
import jsii
from aws_cdk import Fn, Resource, aws_iam as iam
from constructs import Construct, DependencyGroup
@jsii.implements(iam.IRole)
class ImportedRole(Resource, metaclass=jsii.JSIIMeta):
def __init__(
self, scope: Construct, id_: str, *, arn_export_name: str, name_export_name: str
):
super().__init__(scope, id_)
self._role_arn = Fn.import_value(arn_export_name)
self._role_name = Fn.import_value(name_export_name)
@property # type: ignore[misc]
@jsii.member(jsii_name="roleArn")
def role_arn(self) -> str:
return self._role_arn
@property # type: ignore[misc]
@jsii.member(jsii_name="roleName")
def role_name(self) -> str:
return self._role_name
@jsii.member(jsii_name="addToPrincipalPolicy")
def add_to_principal_policy(
self, statement: iam.PolicyStatement
) -> iam.AddToPrincipalPolicyResult:
# Pretend that we've added the statement.
return iam.AddToPrincipalPolicyResult(
statement_added=True, policy_dependable=DependencyGroup(),
)
</code></pre>
</section>
<section>
<ul>
<li>
The <code>AWS::IAM::Policy</code> is gone, but we can add that manually.
(More later)
</li>
<li>
Some attributes and methods specified by <code>iam.IRole</code> are missing,
but there's no warning/error/… about that.
</li>
</ul>
</section>
<section>
<h3>Goal 3: Try something different</h3>
</section>
<section>
<pre><code class="r-stretch language-python" data-trim data-line-numbers>
from aws_cdk import Stack, aws_ec2 as ec2
from constructs import Construct
class PlaygroundStack(Stack):
def __init__(self, scope: Construct, id_: str, **kwargs) -> None:
super().__init__(scope, id_, **kwargs)
vpc = ec2.Vpc(self, "VPC")
ec2.SecurityGroup(self, "SecurityGroup", allow_all_outbound=True, vpc=vpc)
</code></pre>
</section>
<section>
<h4>Use an existing VPC?</h4>
<ul>
<li><code>Vpc.from_lookup()</code>? No, thank you</li>
<li class="fragment" data-fragment-index="1"><code>Vpc.from_vpc_attributes()</code> works, but why the AZs?</li>
</ul>
<pre><code class="language-python fragment" data-trim data-line-numbers data-fragment-index="1">
vpc = ec2.Vpc.from_vpc_attributes(
self,
"VPC",
vpc_id=Fn.import_value("PlaygroundVPCId"),
availability_zones=["a", "b", "c"],
)
</code></pre>
<pre><code class="language-yaml fragment" data-trim data-line-numbers="9-10" data-fragment-index="1">
SecurityGroupDD263621:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: playground/SecurityGroup
SecurityGroupEgress:
- CidrIp: 0.0.0.0/0
Description: Allow all outbound traffic by default
IpProtocol: "-1"
VpcId:
Fn::ImportValue: PlaygroundVPCId
</code></pre>
</section>
<section>
<h4>Can we do that with a custom <code>IVpc</code>?</h4>
<p>✅</p>
<pre><code class="language-python" data-trim data-line-numbers>
vpc = ImportedVPC(self, "VPC", vpc_id_export_name="PlaygroundVPCId")
</code></pre>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="6-7,12-15,17-19">
import jsii
from aws_cdk import Fn, Resource, aws_ec2 as ec2
from constructs import Construct, DependencyGroup
@jsii.implements(ec2.IVpc)
class ImportedVPC(Resource, metaclass=jsii.JSIIMeta):
def __init__(self, scope: Construct, id_: str, *, vpc_id_export_name: str):
super().__init__(scope, id_)
self._vpc_id = Fn.import_value(vpc_id_export_name)
@property # type: ignore[misc]
@jsii.member(jsii_name="vpcId")
def vpc_id(self) -> str:
return self._vpc_id
@jsii.member(jsii_name="selectSubnets")
def select_subnets(self, selection: ec2.SubnetSelection) -> ec2.SelectedSubnets:
return ec2.SelectedSubnets(
availability_zones=[],
has_public=False,
internet_connectivity_established=DependencyGroup(),
subnet_ids=[subnet.subnet_id for subnet in selection.subnets],
subnets=[],
is_pending_lookup=False,
)
</code></pre>
</section>
<section>
<h4>Add Fargate</h4>
<p>✅</p>
<pre><code class="language-python" data-trim data-line-numbers>
from aws_cdk import Stack, aws_ec2 as ec2, aws_ecs as ecs, aws_logs as logs
from constructs import Construct
from .imported_role import ImportedRole
from .imported_vpc import ImportedSubnet, ImportedVPC
class PlaygroundStack(Stack):
def __init__(self, scope: Construct, id_: str, **kwargs) -> None:
super().__init__(scope, id_, **kwargs)
vpc = ImportedVPC(self, "VPC", vpc_id_export_name="PlaygroundVPCId")
cluster = ecs.Cluster(self, "FargateCluster", vpc=vpc)
cluster.enable_fargate_capacity_providers()
task_definition = ecs.FargateTaskDefinition(
self,
"TaskDefinition",
cpu=1024,
ephemeral_storage_gib=21,
memory_limit_mib=4096,
runtime_platform=ecs.RuntimePlatform(
cpu_architecture=ecs.CpuArchitecture.X86_64,
operating_system_family=ecs.OperatingSystemFamily.LINUX,
),
execution_role=ImportedRole(
self,
"ExecutionRole",
arn_export_name="PlaygroundTaskExecutionRoleARN",
name_export_name="PlaygroundTaskExecutionRoleName",
),
task_role=ImportedRole(
self,
"TaskRole",
arn_export_name="PlaygroundTaskRoleARN",
name_export_name="",
),
)
task_definition.add_container(
"PlaygroundContainer",
image=ecs.ContainerImage.from_registry("foo"),
command=["bar"],
entry_point=["executable"],
environment={"SOMETHING": "pointless"},
memory_limit_mib=2048,
)
security_group = ec2.SecurityGroup(self, "SecurityGroup", allow_all_outbound=True, vpc=vpc)
ecs.FargateService(
self,
"FargateService",
task_definition=task_definition,
assign_public_ip=False,
platform_version=ecs.FargatePlatformVersion.VERSION1_4,
security_groups=[security_group],
vpc_subnets=ec2.SubnetSelection(
subnets=[
ImportedSubnet(
self,
f"Subnet{subnet}",
subnet_export_name=(f"PlaygroundSubnet{subnet}"),
)
for subnet in ("A", "B", "C")
]
),
cluster=cluster,
capacity_provider_strategies=[
ecs.CapacityProviderStrategy(
capacity_provider="FARGATE_SPOT", base=0, weight=1
)
],
circuit_breaker=ecs.DeploymentCircuitBreaker(rollback=False),
desired_count=1,
propagate_tags=ecs.PropagatedTagSource.TASK_DEFINITION,
)
</code></pre>
</section>
<section>
<h4>The custom <code>ISubnet</code> works like everything before</h4>
<pre><code class="r-stretch language-python" data-trim data-line-numbers>
@jsii.implements(ec2.ISubnet)
class ImportedSubnet(Resource, metaclass=jsii.JSIIMeta):
def __init__(self, scope: Construct, id_: str, *, subnet_export_name: str):
super().__init__(scope, id_)
self._subnet = Fn.import_value(subnet_export_name)
@property # type: ignore[misc]
@jsii.member(jsii_name="subnetId")
def subnet_id(self) -> str:
return self._subnet
</code></pre>
</section>
<section>
<h4>Add some Logging</h4>
<pre><code class="r-stretch language-python" data-trim data-line-numbers>
log_group = logs.LogGroup(
self, "LogGroup", retention=logs.RetentionDays.ONE_MONTH
)
log_driver = ecs.AwsLogDriver(
stream_prefix="foo",
log_group=log_group,
mode=ecs.AwsLogDriverMode.NON_BLOCKING,
)
task_definition.add_container(
# …
logging=log_driver,
# …
)
</code></pre>
</section>
<section>
<h4>Exception \o/</h4>
<pre><code class="language-python" data-trim data-line-numbers>
jsii.errors.JavaScriptError:
TypeError: Cannot read properties of undefined (reading 'addToPrincipalPolicy')
at Function.addToPrincipal (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-iam/lib/grant.js:1:2339)
at Function.addToPrincipalOrResource (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-iam/lib/grant.js:1:963)
at LogGroup.grant (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-logs/lib/log-group.js:1:1387)
at LogGroup.grantWrite (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-logs/lib/log-group.js:1:1286)
at AwsLogDriver.bind (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-ecs/lib/log-drivers/aws-log-driver.js:1:1346)
at new ContainerDefinition (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-ecs/lib/container-definition.js:1:3069)
at FargateTaskDefinition.addContainer (/tmp/jsii-kernel-pFaccC/node_modules/aws-cdk-lib/aws-ecs/lib/base/task-definition.js:1:9119)
at /tmp/tmpjqoomlgi/lib/program.js:8236:134
at Kernel._wrapSandboxCode (/tmp/tmpjqoomlgi/lib/program.js:8848:24)
at /tmp/tmpjqoomlgi/lib/program.js:8236:107
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/code/app.py", line 13, in <module>
PlaygroundStack(app, "playground")
File "/usr/lib/python3.9/site-packages/jsii/_runtime.py", line 86, in __call__
inst = super().__call__(*args, **kwargs)
File "/code/playground/playground_stack.py", line 51, in __init__
task_definition.add_container(
File "/usr/lib/python3.9/site-packages/aws_cdk/aws_ecs/__init__.py", line 27624, in add_container
return typing.cast(ContainerDefinition, jsii.invoke(self, "addContainer", [id, props]))
File "/usr/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 143, in wrapped
return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
File "/usr/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 355, in invoke
response = self.provider.invoke(
File "/usr/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 359, in invoke
return self._process.send(request, InvokeResponse)
File "/usr/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 326, in send
raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Cannot read properties of undefined (reading 'addToPrincipalPolicy')
</code></pre>
</section>
<section>
<ul>
<li>Looks like <code>ImportedRole</code> needs more attributes/methods</li>
<li>But see if we can debug this</li>
</ul>
</section>
<section>
<h4><code>breakpoint()</code></h4>
<pre><code class="language-python" data-trim data-line-numbers>
breakpoint()
task_definition.add_container(…)
</code></pre>
<pre><code class="r-stretch language-python" data-trim data-line-numbers="17,19">
> /code/playground/playground_stack.py(52)__init__()
-> task_definition.add_container(
(Pdb)
Traceback (most recent call last):
File "/code/app.py", line 13, in <module>
PlaygroundStack(app, "playground")
File "/usr/lib/python3.9/site-packages/jsii/_runtime.py", line 86, in __call__
inst = super().__call__(*args, **kwargs)
File "/code/playground/playground_stack.py", line 52, in __init__
task_definition.add_container(
File "/code/playground/playground_stack.py", line 52, in __init__
task_definition.add_container(
File "/usr/lib/python3.9/bdb.py", line 88, in trace_dispatch
return self.dispatch_line(frame)
File "/usr/lib/python3.9/bdb.py", line 113, in dispatch_line
if self.quitting: raise BdbQuit
bdb.BdbQuit
Subprocess exited with error 1
</code></pre>
</section>
<section>
<div class="container">
<div class="col">
<p> <br/> <br/> </p>
<h4>Bhat et al. suggest VS Code</h4>
<p>Doesn't help, because it stays within Python.</p>
</div>
<div class="col">
<img data-src="assets/the-cdk-book.png" max-height="100px">
</div>
</div>
<p><small>https://thecdkbook.com/</small></p>
</section>
<section>
<h4>“Solution”</h4>
<pre><code class="language-python" data-trim data-line-numbers="22,24,26-29,41-44,46-53,55-59,65-67,69-79">
from typing import Optional
import jsii
from aws_cdk import ArnFormat, Fn, Resource, Stack, aws_iam as iam
from constructs import Construct, DependencyGroup
@jsii.implements(iam.IRole)
class ImportedRole(Resource, metaclass=jsii.JSIIMeta):
def __init__(
self,
scope: Construct,
id_: str,
*,
arn_export_name: str,
name_export_name: str,
):
super().__init__(scope, id_)
self._scope = scope
self._role_arn = Fn.import_value(arn_export_name)
self._role_name = Fn.import_value(name_export_name)
self._default_policy: iam.Policy = None
self.__seen_policies: set[iam.Policy] = set()
@property # type: ignore[misc]
@jsii.member(jsii_name="policyFragment")
def policy_fragment(self) -> iam.PrincipalPolicyFragment:
return iam.ArnPrincipal(self.role_arn).policy_fragment
@property # type: ignore[misc]
@jsii.member(jsii_name="roleArn")
def role_arn(self) -> str:
return self._role_arn
@property # type: ignore[misc]
@jsii.member(jsii_name="roleName")
def role_name(self) -> str:
return self._role_name
@property # type: ignore[misc]
@jsii.member(jsii_name="grantPrincipal")
def grant_principal(self) -> iam.IPrincipal:
return self # iam.ArnPrincipal(self._role_arn)
@property # type: ignore[misc]
@jsii.member(jsii_name="principalAccount")
def principal_account(self) -> Optional[str]:
scope_stack = Stack.of(self._scope)
parsed_arn = scope_stack.split_arn(
self._role_arn, ArnFormat.SLASH_RESOURCE_NAME
)
return parsed_arn.account
@jsii.member(jsii_name="attachInlinePolicy")
def attach_inline_policy(self, policy: iam.Policy) -> None:
if policy not in self.__seen_policies:
self.__seen_policies.add(policy)
policy.attach_to_role(self)
# @jsii.member(jsii_name="addManagedPolicy")
# def add_managed_policy(self, policy: iam.IManagedPolicy) -> None:
# pass
@jsii.member(jsii_name="addToPolicy")
def add_to_policy(self, statement: iam.PolicyStatement) -> bool:
return self.add_to_principal_policy(statement).statement_added
@jsii.member(jsii_name="addToPrincipalPolicy")
def add_to_principal_policy(
self, statement: iam.PolicyStatement
) -> iam.AddToPrincipalPolicyResult:
if self._default_policy is None:
self._default_policy = iam.Policy(self, "Policy")
self.attach_inline_policy(self._default_policy)
self._default_policy.add_statements(statement)
return iam.AddToPrincipalPolicyResult(
statement_added=True, policy_dependable=self._default_policy
)
# @jsii.member(jsii_name="grant")
# def grant(self, grantee: iam.IPrincipal, *actions: str) -> iam.Grant:
# return self.role.grant(grantee, *actions)
# @jsii.member(jsii_name="grantPassRole")
# def grant_pass_role(self, grantee: iam.IPrincipal) -> iam.Grant:
# return self.role.grant_pass_role(grantee)
</code></pre>
</section>
<section>
<h4>Bonus</h4>
<div class="r-stack">
<img data-src="assets/commit.png" max-height="100px" class="fragment fade-out" data-fragment-index="0">
<img data-src="assets/commit-comment.png" max-height="100px" class="fragment" data-fragment-index="0">
</div>
</section>
<section data-visibility="uncounted">
<h4>Thank you</h4>
<p>Questions?</p>
</section>
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config: