Skip to content

Commit 04217a5

Browse files
author
Elad Ben-Israel
authored
feat: idiomize cloudformation intrinsics functions (#1428)
Refactor CloudFormation intrinsic functions so that they will have a more idiomatic shape. They have been converted from classes (e.g. `new FnJoin(...)`) to static methods (e.g. `Fn.join(...)`). Condition functions are mapped to `Fn.conditionXxx` and return an `FnCondition` object. Furthermore, return type of these static methods are now tokens represented as the resolved type (i.e. `string` or `string[]`) instead of `CloudFormationToken` objects. This allows using these functions and pseudo parameters naturally (instead of requiring the use of `.toString()` or `.toList()`). Fixes #202 BREAKING CHANGE: CloudFormation intrinsic functions are now represented as static methods under the `Fn` class (e.g. `Fn.join(...)` instead of `new FnJoin(...).toString()`).
1 parent 095da14 commit 04217a5

File tree

37 files changed

+541
-307
lines changed

37 files changed

+541
-307
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ Fn.if_(Fn.equals(param.ref, 'True'), 'Encrypted', Pseudo.NO_VALUE)
17001700
After:
17011701
17021702
```javascript
1703-
new FnIf(new FnEquals(param.ref, 'True'), 'Encrypted', new AwsNoValue())
1703+
new FnIf(Fn.equals(param.ref, 'True'), 'Encrypted', new AwsNoValue())
17041704
```
17051705
17061706
- CloudFormation template options (`templateFormatVersion`, `description` and `transform`) are now grouped under `Stack.templateOptions` instead of directly under `Stack`.

docs/src/cloudformation.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ Intrinsic Functions
171171
172172
.. code-block:: js
173173
174-
import cdk = require('@aws-cdk/cdk');
175-
new cdk.FnJoin(",", [...])
174+
import { Fn } from'@aws-cdk/cdk';
175+
Fn.join(",", [...])
176176
177177
.. _pseudo_parameters:
178178

examples/cdk-examples-typescript/advanced-usage/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class CloudFormationExample extends cdk.Stack {
157157
// outputs are constructs the synthesize into the template's "Outputs" section
158158
new cdk.Output(this, 'Output', {
159159
description: 'This is an output of the template',
160-
value: new cdk.FnConcat(new cdk.AwsAccountId(), '/', param.ref)
160+
value: `${new cdk.AwsAccountId()}/${param.ref}`
161161
});
162162

163163
// stack.templateOptions can be used to specify template-level options
@@ -176,13 +176,13 @@ class CloudFormationExample extends cdk.Stack {
176176
new cdk.AwsStackName(),
177177
],
178178

179-
// all CloudFormation's intrinsic functions are supported via the `cdk.FnXxx` classes
179+
// all CloudFormation's intrinsic functions are supported via the `cdk.Fn.xxx` static methods.
180180
IntrinsicFunctions: [
181-
new cdk.FnAnd(
182-
new cdk.FnFindInMap('MyMap', 'K1', 'K2'),
183-
new cdk.FnSub('hello ${world}', {
184-
world: new cdk.FnBase64(param.ref) // resolves to { Ref: <param-id> }
185-
}))
181+
cdk.Fn.join('', [
182+
cdk.Fn.findInMap('MyMap', 'K1', 'K2'),
183+
cdk.Fn.sub('hello ${world}', {
184+
world: cdk.Fn.base64(param.ref) // resolves to { Ref: <param-id> }
185+
}) ])
186186
],
187187
};
188188
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"pkglint": "tools/pkglint/bin/pkglint -f ."
88
},
99
"devDependencies": {
10-
"@types/node": "^8.10.38",
10+
"@types/node": "8.10.38",
1111
"@types/nodeunit": "^0.0.30",
1212
"conventional-changelog-cli": "^2.0.5",
1313
"lerna": "^3.3.0",

packages/@aws-cdk/assets-docker/lib/image-asset.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ export class DockerImageAsset extends cdk.Construct {
6161
this.addMetadata(cxapi.ASSET_METADATA, asset);
6262

6363
// parse repository name and tag from the parameter (<REPO_NAME>:<TAG>)
64-
const components = new cdk.FnSplit(':', imageNameParameter.value);
65-
const repositoryName = new cdk.FnSelect(0, components).toString();
66-
const imageTag = new cdk.FnSelect(1, components).toString();
64+
const components = cdk.Fn.split(':', imageNameParameter.valueAsString);
65+
const repositoryName = cdk.Fn.select(0, components).toString();
66+
const imageTag = cdk.Fn.select(1, components).toString();
6767

6868
// Require that repository adoption happens first, so we route the
6969
// input ARN into the Custom Resource and then get the URI which we use to

packages/@aws-cdk/assets/lib/asset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ export class Asset extends cdk.Construct {
110110
});
111111

112112
this.s3BucketName = bucketParam.value.toString();
113-
this.s3Prefix = new cdk.FnSelect(0, new cdk.FnSplit(cxapi.ASSET_PREFIX_SEPARATOR, keyParam.value)).toString();
114-
const s3Filename = new cdk.FnSelect(1, new cdk.FnSplit(cxapi.ASSET_PREFIX_SEPARATOR, keyParam.value)).toString();
113+
this.s3Prefix = cdk.Fn.select(0, cdk.Fn.split(cxapi.ASSET_PREFIX_SEPARATOR, keyParam.valueAsString)).toString();
114+
const s3Filename = cdk.Fn.select(1, cdk.Fn.split(cxapi.ASSET_PREFIX_SEPARATOR, keyParam.valueAsString)).toString();
115115
this.s3ObjectKey = `${this.s3Prefix}${s3Filename}`;
116116

117117
this.bucket = s3.BucketRef.import(this, 'AssetBucket', {

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
222222

223223
// use delayed evaluation
224224
const machineImage = props.machineImage.getImage(this);
225-
const userDataToken = new cdk.Token(() => new cdk.FnBase64((machineImage.os.createUserData(this.userDataLines))));
225+
const userDataToken = new cdk.Token(() => cdk.Fn.base64((machineImage.os.createUserData(this.userDataLines))));
226226
const securityGroupsToken = new cdk.Token(() => this.securityGroups.map(sg => sg.securityGroupId));
227227

228228
const launchConfig = new CfnLaunchConfiguration(this, 'LaunchConfig', {

packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,7 @@ export class CloudFrontWebDistribution extends cdk.Construct implements route53.
574574

575575
if (originConfig.s3OriginSource && originConfig.s3OriginSource.originAccessIdentity) {
576576
originProperty.s3OriginConfig = {
577-
originAccessIdentity: new cdk.FnConcat(
578-
"origin-access-identity/cloudfront/", originConfig.s3OriginSource.originAccessIdentity.ref
579-
),
577+
originAccessIdentity: `origin-access-identity/cloudfront/${originConfig.s3OriginSource.originAccessIdentity.ref}`
580578
};
581579
} else if (originConfig.s3OriginSource) {
582580
originProperty.s3OriginConfig = {};

packages/@aws-cdk/aws-cloudtrail/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class CloudTrail extends cdk.Construct {
138138
.addServicePrincipal(cloudTrailPrincipal));
139139

140140
s3bucket.addToResourcePolicy(new iam.PolicyStatement()
141-
.addResource(s3bucket.arnForObjects(new cdk.FnConcat('AWSLogs/', new cdk.AwsAccountId(), "/*")))
141+
.addResource(s3bucket.arnForObjects(`AWSLogs/${new cdk.AwsAccountId()}/*`))
142142
.addActions("s3:PutObject")
143143
.addServicePrincipal(cloudTrailPrincipal)
144144
.setCondition("StringEquals", {'s3:x-amz-acl': "bucket-owner-full-control"}));

packages/@aws-cdk/aws-codebuild/lib/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,10 @@ export class Project extends ProjectRef {
495495

496496
let cache: CfnProject.ProjectCacheProperty | undefined;
497497
if (props.cacheBucket) {
498-
const cacheDir = props.cacheDir != null ? props.cacheDir : new cdk.AwsNoValue();
498+
const cacheDir = props.cacheDir != null ? props.cacheDir : new cdk.AwsNoValue().toString();
499499
cache = {
500500
type: 'S3',
501-
location: new cdk.FnJoin('/', [props.cacheBucket.bucketName, cacheDir]),
501+
location: cdk.Fn.join('/', [props.cacheBucket.bucketName, cacheDir]),
502502
};
503503

504504
props.cacheBucket.grantReadWrite(this.role);

0 commit comments

Comments
 (0)