Skip to content

Commit 7a68125

Browse files
authored
2 parents 8c2d7fc + 8a2cbcc commit 7a68125

File tree

216 files changed

+9643
-2200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

216 files changed

+9643
-2200
lines changed

.mergify.yml

-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pull_request_rules:
1717
method: squash
1818
strict_method: merge
1919
commit_message: title+body
20-
delete_head_branch: {}
2120
conditions:
2221
- base!=release
2322
- -title~=(WIP|wip)
@@ -41,7 +40,6 @@ pull_request_rules:
4140
method: squash
4241
strict_method: merge
4342
commit_message: title+body
44-
delete_head_branch: {}
4543
conditions:
4644
- base!=release
4745
- -title~=(WIP|wip)
@@ -67,7 +65,6 @@ pull_request_rules:
6765
method: merge
6866
strict_method: merge
6967
commit_message: title+body
70-
delete_head_branch: {}
7168
conditions:
7269
- -title~=(WIP|wip)
7370
- -label~=(blocked|do-not-merge)
@@ -115,7 +112,6 @@ pull_request_rules:
115112
# It's not dangerous: GitHub branch protection settings prevent merging stale branches.
116113
strict: false
117114
method: squash
118-
delete_head_branch: {}
119115
conditions:
120116
- -title~=(WIP|wip)
121117
- -label~=(blocked|do-not-merge)

CHANGELOG.md

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,66 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5+
## [1.63.0](https://github.com/aws/aws-cdk/compare/v1.62.0...v1.63.0) (2020-09-12)
6+
7+
8+
### ⚠ BREAKING CHANGES TO EXPERIMENTAL FEATURES
9+
10+
* **appsync:** force `apiKeyConfig` require a Expiration class instead of string
11+
- **appsync**: Parameter `apiKeyConfig` takes `Expiration` class instead of `string`
12+
* **core:** custom implementations of `IStackSynthesizer`
13+
must now implement `synthesize()` instead of
14+
`synthesizeStackArtifacts()`.
15+
* **aws-batch:** Changed type of `ComputeResources.computeResourcesTags` from `Tag` to map
16+
17+
### Features
18+
19+
* **appsync:** add authorization config to the HttpDataSource ([#10171](https://github.com/aws/aws-cdk/issues/10171)) ([b2cc277](https://github.com/aws/aws-cdk/commit/b2cc277971aed36aa03e720b8fea093ef14bd9be)), closes [#9971](https://github.com/aws/aws-cdk/issues/9971) [#9934](https://github.com/aws/aws-cdk/issues/9934)
20+
* **appsync:** add support for subscriptions for code-first schema generation ([#10078](https://github.com/aws/aws-cdk/issues/10078)) ([65db131](https://github.com/aws/aws-cdk/commit/65db1312d6b395700c203edeb50248b5e8a0c036)), closes [#9345](https://github.com/aws/aws-cdk/issues/9345)
21+
* **appsync:** implement directives for code-first approach ([#9973](https://github.com/aws/aws-cdk/issues/9973)) ([088cd48](https://github.com/aws/aws-cdk/commit/088cd4857f2f3f29873bdf5f7d1c4b25b3d49372)), closes [#9879](https://github.com/aws/aws-cdk/issues/9879)
22+
* **appsync:** support enumeration types for code-first approach ([#10023](https://github.com/aws/aws-cdk/issues/10023)) ([30a5b80](https://github.com/aws/aws-cdk/commit/30a5b806f265d88e88d2faea623e1ce024c019c3)), closes [#10023](https://github.com/aws/aws-cdk/pull/10023#issuecomment-690773916)
23+
* **appsync:** support union types for code-first approach ([#10025](https://github.com/aws/aws-cdk/issues/10025)) ([28a9834](https://github.com/aws/aws-cdk/commit/28a9834fb6cbacbd3e0ef97441fa0fb6e45120b1))
24+
* **cfn-include:** add support for Hooks ([#10143](https://github.com/aws/aws-cdk/issues/10143)) ([4de68c0](https://github.com/aws/aws-cdk/commit/4de68c0a5b0e7568a5e222e8a29703186b961ec7)), closes [#9713](https://github.com/aws/aws-cdk/issues/9713)
25+
* **cfn-include:** allow renaming the template elements logical IDs ([#10169](https://github.com/aws/aws-cdk/issues/10169)) ([cf746a0](https://github.com/aws/aws-cdk/commit/cf746a07be171b35bb5e5514287c628642436df3)), closes [#9714](https://github.com/aws/aws-cdk/issues/9714)
26+
* **chatbot:** log retention support and metrics utility methods ([#10137](https://github.com/aws/aws-cdk/issues/10137)) ([0f0d1e7](https://github.com/aws/aws-cdk/commit/0f0d1e74fb71a7b415aa9a5d02258b7c5933536b)), closes [#10135](https://github.com/aws/aws-cdk/issues/10135)
27+
* **cli:** support credential_source in aws shared config file ([#10272](https://github.com/aws/aws-cdk/issues/10272)) ([940a443](https://github.com/aws/aws-cdk/commit/940a443ba457ddaebd85b44fa63cd9b15201c18a))
28+
* **codebuild:** add git submodule options of codebuild ([#10283](https://github.com/aws/aws-cdk/issues/10283)) ([698e5ef](https://github.com/aws/aws-cdk/commit/698e5ef3568880474adf73a38c21dad919b62b7e)), closes [#10271](https://github.com/aws/aws-cdk/issues/10271)
29+
* **eks:** arm64 support ([#9875](https://github.com/aws/aws-cdk/issues/9875)) ([ffb84c6](https://github.com/aws/aws-cdk/commit/ffb84c62d03e40daa0e07c451c31bdea2dd0816c)), closes [#9915](https://github.com/aws/aws-cdk/issues/9915)
30+
* **eks:** bump aws-node-termination-handler to 0.9.5 ([#10278](https://github.com/aws/aws-cdk/issues/10278)) ([8cfc190](https://github.com/aws/aws-cdk/commit/8cfc190daaa112de448c19716df2c10eeb29695c)), closes [aws/aws-cdk#10277](https://github.com/aws/aws-cdk/issues/10277)
31+
* **eks:** managed nodegroup with custom AMI and launch template support ([#9881](https://github.com/aws/aws-cdk/issues/9881)) ([5c294fb](https://github.com/aws/aws-cdk/commit/5c294fbc1149167a70f35c7870ff1995042839b7)), closes [#9873](https://github.com/aws/aws-cdk/issues/9873)
32+
* **elasticloadbalancingv2:** more health check validations to NLB target group ([#3703](https://github.com/aws/aws-cdk/issues/3703)) ([#10205](https://github.com/aws/aws-cdk/issues/10205)) ([e3f3332](https://github.com/aws/aws-cdk/commit/e3f333212b7c9f7deb836be8ecd71c7a7a06c394))
33+
* **elasticloadbalancingv2:** multiple security groups for ALBs ([#10244](https://github.com/aws/aws-cdk/issues/10244)) ([1ebf362](https://github.com/aws/aws-cdk/commit/1ebf36206b1e6a98a9a708efbe3ba3bfb1d3f05e)), closes [#5138](https://github.com/aws/aws-cdk/issues/5138)
34+
* **lambda-nodejs:** improved project root detection ([#10182](https://github.com/aws/aws-cdk/issues/10182)) ([cce83dc](https://github.com/aws/aws-cdk/commit/cce83dcc163da8f71b8c2f95818d7d8f25db95f4)), closes [#10174](https://github.com/aws/aws-cdk/issues/10174)
35+
* **pipelines:** adding IAM permissions to ShellScriptAction ([#10149](https://github.com/aws/aws-cdk/issues/10149)) ([ec15485](https://github.com/aws/aws-cdk/commit/ec154850b6bc2f485241aa1c0ea41400785d8a41)), closes [#9600](https://github.com/aws/aws-cdk/issues/9600)
36+
* **rds:** database clusters from snapshots ([#10130](https://github.com/aws/aws-cdk/issues/10130)) ([915eb4b](https://github.com/aws/aws-cdk/commit/915eb4be3946652a00b7496b9e8610169852f27b)), closes [#4379](https://github.com/aws/aws-cdk/issues/4379)
37+
* **rds:** deprecate OracleSE and OracleSE1 engine versions ([#10241](https://github.com/aws/aws-cdk/issues/10241)) ([562f891](https://github.com/aws/aws-cdk/commit/562f8913dae7b77a1516a60cc1ff277ac42fb9e0)), closes [#9249](https://github.com/aws/aws-cdk/issues/9249)
38+
* **rds:** metrics for clusters ([#10162](https://github.com/aws/aws-cdk/issues/10162)) ([49f6034](https://github.com/aws/aws-cdk/commit/49f6034ee2b514f870819b7b48880246750b6fd2)), closes [#5212](https://github.com/aws/aws-cdk/issues/5212)
39+
* **route53-patterns:** support IPv6 in HttpsRedirect ([#10203](https://github.com/aws/aws-cdk/issues/10203)) ([a1f6e1b](https://github.com/aws/aws-cdk/commit/a1f6e1be8a21625b1723b51277457f598a3be23e))
40+
* **secrets-manager:** exclude characters for password rotation applications ([#10110](https://github.com/aws/aws-cdk/issues/10110)) ([1260d52](https://github.com/aws/aws-cdk/commit/1260d5215d474d6edc2460ffe9658552d17ab239)), closes [#4144](https://github.com/aws/aws-cdk/issues/4144)
41+
42+
43+
### Bug Fixes
44+
45+
* **appsync:** strongly type `expires` prop in apiKeyConfig ([#9122](https://github.com/aws/aws-cdk/issues/9122)) ([287f808](https://github.com/aws/aws-cdk/commit/287f808cf34870295e4032995fc083ac204b64c3)), closes [#8698](https://github.com/aws/aws-cdk/issues/8698)
46+
* **aws-batch:** `computeResources` tags are not configured properly ([#10209](https://github.com/aws/aws-cdk/issues/10209)) ([40222ef](https://github.com/aws/aws-cdk/commit/40222ef398fd1fb63b3b886624d5bb40562142c6)), closes [#7350](https://github.com/aws/aws-cdk/issues/7350)
47+
* **cfn-include:** correctly parse YAML strings in short-form GetAtt ([#10197](https://github.com/aws/aws-cdk/issues/10197)) ([a388d70](https://github.com/aws/aws-cdk/commit/a388d70f38a84195bbe5e580220b5cd21ebde624)), closes [#10177](https://github.com/aws/aws-cdk/issues/10177)
48+
* **cfn-include:** correctly substitute falsy parameter values ([#10195](https://github.com/aws/aws-cdk/issues/10195)) ([8791f88](https://github.com/aws/aws-cdk/commit/8791f8877165c87f6ef4967376aa53ffeb696013)), closes [#10107](https://github.com/aws/aws-cdk/issues/10107)
49+
* **cli:** metadata not recorded for templates >50k ([#10184](https://github.com/aws/aws-cdk/issues/10184)) ([dfd2baf](https://github.com/aws/aws-cdk/commit/dfd2baf8b9d4cc930a1c5dc88c178ffe7d1121b0))
50+
* **cli:** simplify lib template ([#10175](https://github.com/aws/aws-cdk/issues/10175)) ([fc3ec9b](https://github.com/aws/aws-cdk/commit/fc3ec9b20edb2442bab456acf55c5cff7efb11b9))
51+
* **cli:** unable to set termination protection for pipeline stacks ([#9938](https://github.com/aws/aws-cdk/issues/9938)) ([a00a4ee](https://github.com/aws/aws-cdk/commit/a00a4ee162f287b5db45e73051ecdf0e32009def))
52+
* **cloudfront:** comment for origin access identity is too long ([#10266](https://github.com/aws/aws-cdk/issues/10266)) ([495aeb9](https://github.com/aws/aws-cdk/commit/495aeb96fd530ba0f73eeeab5a5b75b4a7390c99)), closes [#10211](https://github.com/aws/aws-cdk/issues/10211)
53+
* **codepipeline:** cross-region support stack requires bootstrapping ([#10217](https://github.com/aws/aws-cdk/issues/10217)) ([b5ff4d6](https://github.com/aws/aws-cdk/commit/b5ff4d6673629ebabceb72fc83464001267c328e)), closes [#10215](https://github.com/aws/aws-cdk/issues/10215)
54+
* **core:** DefaultSynthesizer breaks this.node.setContext() on Stack ([#10246](https://github.com/aws/aws-cdk/issues/10246)) ([61865aa](https://github.com/aws/aws-cdk/commit/61865aaef682be6727d7768213260c7a95d799f8))
55+
* **core:** Stacks render CloudFormation elements in nested Stages ([#10156](https://github.com/aws/aws-cdk/issues/10156)) ([5f36f6b](https://github.com/aws/aws-cdk/commit/5f36f6b5b2126de763b2e156fa2a9b6604f40f00)), closes [#9792](https://github.com/aws/aws-cdk/issues/9792) [#9669](https://github.com/aws/aws-cdk/issues/9669)
56+
* **custom-resources:** deleting custom resource fails when using two or more ([#10012](https://github.com/aws/aws-cdk/issues/10012)) ([8d23f24](https://github.com/aws/aws-cdk/commit/8d23f248c7496d56c352a3db4b9d4bcccbdfe1c4))
57+
* **ec2:** cfn-init user data hash not updated if file asset changes ([#10216](https://github.com/aws/aws-cdk/issues/10216)) ([0d7ca63](https://github.com/aws/aws-cdk/commit/0d7ca63edb11a80440732d6327d0fe3ed685a993)), closes [#10206](https://github.com/aws/aws-cdk/issues/10206)
58+
* **eks:** restricted public access breaks cluster functionality ([#10103](https://github.com/aws/aws-cdk/issues/10103)) ([a1b5bf6](https://github.com/aws/aws-cdk/commit/a1b5bf6f5a77e236169f95159cf62a2ecfb25bc4))
59+
* **kms:** do not change the principal to root for imported resources in dependent Stacks ([#10299](https://github.com/aws/aws-cdk/issues/10299)) ([54dfe83](https://github.com/aws/aws-cdk/commit/54dfe8374afeaff400e85f43185462b9fd9c8ac2)), closes [#10166](https://github.com/aws/aws-cdk/issues/10166)
60+
* **lambda-nodejs:** permission denied, mkdir '/.parcel-cache' ([#10181](https://github.com/aws/aws-cdk/issues/10181)) ([20f5535](https://github.com/aws/aws-cdk/commit/20f5535bee8d0e1e7aa5b0c3bad780666d64bc87))
61+
* **pipelines:** changing synth action doesn't restart pipeline ([#10176](https://github.com/aws/aws-cdk/issues/10176)) ([14c8a98](https://github.com/aws/aws-cdk/commit/14c8a9878d092aac857655c2e5c8684015c84b29)), closes [#9458](https://github.com/aws/aws-cdk/issues/9458)
62+
* **pipelines:** check for an empty Stage object ([#10153](https://github.com/aws/aws-cdk/issues/10153)) ([cec20c8](https://github.com/aws/aws-cdk/commit/cec20c8f68676c2bf3c8c3246334c82f50261358)), closes [#9559](https://github.com/aws/aws-cdk/issues/9559)
63+
* **rds:** Make most DatabaseClusterAttributes properties optional ([#10291](https://github.com/aws/aws-cdk/issues/10291)) ([0653e6b](https://github.com/aws/aws-cdk/commit/0653e6bead37ed92e47295010645009e3b97e246)), closes [#3587](https://github.com/aws/aws-cdk/issues/3587)
64+
565
## [1.62.0](https://github.com/aws/aws-cdk/compare/v1.61.1...v1.62.0) (2020-09-03)
666

767

@@ -10,6 +70,7 @@ All notable changes to this project will be documented in this file. See [standa
1070
* **eks:** when importing EKS clusters using `eks.Cluster.fromClusterAttributes`, the `clusterArn` attribute is not supported anymore, and will always be derived from `clusterName`.
1171
* **eks**: Only a single `eks.Cluster` is allowed per CloudFormation stack.
1272
* **eks**: The `securityGroups` attribute of `ClusterAttributes` is now `securityGroupIds`.
73+
* **cli**: `--qualifier` must be alphanumeric and not longer than 10 characters when bootstrapping using `newStyleStackSynthesis`.
1374

1475
### Features
1576

allowed-breaking-changes.txt

+7
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,10 @@ changed-type:@aws-cdk/aws-codedeploy.ServerDeploymentGroup.autoScalingGroups
2828
change-return-type:@aws-cdk/aws-ecs.ContainerDefinition.renderContainerDefinition
2929
change-return-type:@aws-cdk/aws-ecs.FirelensLogRouter.renderContainerDefinition
3030
change-return-type:@aws-cdk/aws-ecs.LinuxParameters.renderLinuxParameters
31+
32+
# These were accidentally not marked @experimental
33+
removed:@aws-cdk/core.BootstraplessSynthesizer.synthesizeStackArtifacts
34+
removed:@aws-cdk/core.DefaultStackSynthesizer.synthesizeStackArtifacts
35+
removed:@aws-cdk/core.LegacyStackSynthesizer.synthesizeStackArtifacts
36+
removed:@aws-cdk/core.NestedStackSynthesizer.synthesizeStackArtifacts
37+
removed:@aws-cdk/core.IStackSynthesizer.synthesizeStackArtifacts

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"tools/*"
1111
],
1212
"rejectCycles": "true",
13-
"version": "1.62.0"
13+
"version": "1.63.0"
1414
}

package.json

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@
6969
"@aws-cdk/core/minimatch/**",
7070
"@aws-cdk/cx-api/semver",
7171
"@aws-cdk/cx-api/semver/**",
72+
"aws-cdk-lib/case",
73+
"aws-cdk-lib/case/**",
74+
"aws-cdk-lib/fs-extra",
75+
"aws-cdk-lib/fs-extra/**",
76+
"aws-cdk-lib/jsonschema",
77+
"aws-cdk-lib/jsonschema/**",
78+
"aws-cdk-lib/minimatch",
79+
"aws-cdk-lib/minimatch/**",
80+
"aws-cdk-lib/semver",
81+
"aws-cdk-lib/semver/**",
82+
"aws-cdk-lib/yaml",
83+
"aws-cdk-lib/yaml/**",
7284
"monocdk-experiment/case",
7385
"monocdk-experiment/case/**",
7486
"monocdk-experiment/fs-extra",

packages/@aws-cdk/assert/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,65 @@ expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
114114
}));
115115
```
116116
117+
### Capturing values from a match
118+
119+
Special `Capture` matchers exist to capture values encountered during a match. These can be
120+
used for two typical purposes:
121+
122+
* Apply additional assertions to the values found during a matching operation.
123+
* Use the value found during a matching operation in a new matching operation.
124+
125+
`Capture` matchers take an inner matcher as an argument, and will only capture the value
126+
if the inner matcher succeeds in matching the given value.
127+
128+
Here's an example which asserts that a policy for `RoleA` contains two statements
129+
with *different* ARNs (without caring what those ARNs might be), and that
130+
a policy for `RoleB` *also* has a statement for one of those ARNs (again, without
131+
caring what the ARN might be):
132+
133+
```ts
134+
const arn1 = Capture.aString();
135+
const arn2 = Capture.aString();
136+
137+
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
138+
Roles: ['RoleA'],
139+
PolicyDocument: {
140+
Statement: [
141+
objectLike({
142+
Resource: [arn1.capture()],
143+
}),
144+
objectLike({
145+
Resource: [arn2.capture()],
146+
}),
147+
],
148+
},
149+
}));
150+
151+
// Don't care about the values as long as they are not the same
152+
expect(arn1.capturedValue).not.toEqual(arn2.capturedValue);
153+
154+
expect(stack).to(haveResourceLike('AWS::IAM::Policy', {
155+
Roles: ['RoleB'],
156+
PolicyDocument: {
157+
Statement: [
158+
objectLike({
159+
// This ARN must be the same as ARN1 above.
160+
Resource: [arn1.capturedValue]
161+
}),
162+
],
163+
},
164+
}));
165+
```
166+
167+
NOTE: `Capture` look somewhat like *bindings* in other pattern matching
168+
libraries you might be used to, but they are far simpler and very
169+
deterministic. In particular, they don't do unification: if the same Capture
170+
is either used multiple times in the same structure expression or matches
171+
multiple times, no restarting of the match is done to make them all match the
172+
same value: the last value encountered by the `Capture` (as determined by the
173+
behavior of the matchers around it) is stored into it and will be the one
174+
available after the match has completed.
175+
117176
### Check number of resources
118177
119178
If you want to assert that `n` number of resources of a particular type exist, with or without specific properties:

packages/@aws-cdk/assert/lib/assertions/have-resource-matchers.ts

+98
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,104 @@ export function notMatching(matcher: any): PropertyMatcher {
281281
});
282282
}
283283

284+
export type TypeValidator<T> = (x: any) => x is T;
285+
286+
/**
287+
* Captures a value onto an object if it matches a given inner matcher
288+
*
289+
* @example
290+
*
291+
* const someValue = Capture.aString();
292+
* expect(stack).toHaveResource({
293+
* // ...
294+
* Value: someValue.capture(stringMatching('*a*')),
295+
* });
296+
* console.log(someValue.capturedValue);
297+
*/
298+
export class Capture<T=any> {
299+
/**
300+
* A Capture object that captures any type
301+
*/
302+
public static anyType(): Capture<any> {
303+
return new Capture();
304+
}
305+
306+
/**
307+
* A Capture object that captures a string type
308+
*/
309+
public static aString(): Capture<string> {
310+
return new Capture((x: any): x is string => {
311+
if (typeof x !== 'string') {
312+
throw new Error(`Expected to capture a string, got '${x}'`);
313+
}
314+
return true;
315+
});
316+
}
317+
318+
/**
319+
* A Capture object that captures a custom type
320+
*/
321+
public static a<T>(validator: TypeValidator<T>): Capture<T> {
322+
return new Capture(validator);
323+
}
324+
325+
private _value?: T;
326+
private _didCapture = false;
327+
private _wasInvoked = false;
328+
329+
protected constructor(private readonly typeValidator?: TypeValidator<T>) {
330+
}
331+
332+
/**
333+
* Capture the value if the inner matcher successfully matches it
334+
*
335+
* If no matcher is given, `anything()` is assumed.
336+
*
337+
* And exception will be thrown if the inner matcher returns `true` and
338+
* the value turns out to be of a different type than the `Capture` object
339+
* is expecting.
340+
*/
341+
public capture(matcher?: any): PropertyMatcher {
342+
if (matcher === undefined) {
343+
matcher = anything();
344+
}
345+
346+
return annotateMatcher({ $capture: matcher }, (value: any, failure: InspectionFailure) => {
347+
this._wasInvoked = true;
348+
const result = matcherFrom(matcher)(value, failure);
349+
if (result) {
350+
if (this.typeValidator && !this.typeValidator(value)) {
351+
throw new Error(`Value not of the expected type: ${value}`);
352+
}
353+
this._didCapture = true;
354+
this._value = value;
355+
}
356+
return result;
357+
});
358+
}
359+
360+
/**
361+
* Whether a value was successfully captured
362+
*/
363+
public get didCapture() {
364+
return this._didCapture;
365+
}
366+
367+
/**
368+
* Return the value that was captured
369+
*
370+
* Throws an exception if now value was captured
371+
*/
372+
public get capturedValue(): T {
373+
// When this module is ported to jsii, the type parameter will obviously
374+
// have to be dropped and this will have to turn into an `any`.
375+
if (!this.didCapture) {
376+
throw new Error(`Did not capture a value: ${this._wasInvoked ? 'inner matcher failed' : 'never invoked'}`);
377+
}
378+
return this._value!;
379+
}
380+
}
381+
284382
/**
285383
* Match on the innards of a JSON string, instead of the complete string
286384
*/

0 commit comments

Comments
 (0)