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(cfn-include): add support for the Fn::Sub function #9275

Merged
merged 75 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
db60c27
doc: added slack link to readme
comcalvi Jun 4, 2020
5924f3b
Merge branch 'master' into master
comcalvi Jun 5, 2020
f0c85a3
Update README.md
comcalvi Jun 5, 2020
f37e60e
Merge branch 'master' into master
mergify[bot] Jun 5, 2020
83ed743
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 15, 2020
c6664af
added support for Fn::Select, Fn::FindInMap, Fn::Cidr, Fn::GetAZs, Fn…
comcalvi Jun 15, 2020
d0f64d2
added support for the 'Fn::Transform' cloudformation intrinsic function.
comcalvi Jun 16, 2020
d69e46d
added support for the Fn::Base64 Intrinsic Function
comcalvi Jun 16, 2020
df1446a
tested more complex combinations of conditional and non-conditional i…
comcalvi Jun 16, 2020
132cb4d
fixed linter issues
comcalvi Jun 16, 2020
ae7c570
implmented Adam's requests and fixed additional linter issues
comcalvi Jun 17, 2020
7ca707e
updated README to reflect the newly supported cloudformation functions
comcalvi Jun 17, 2020
63c27a7
removed quotes from the type of Transform's parameter argument, modif…
comcalvi Jun 17, 2020
4179271
fixed teseting issue related to Fn::Select
comcalvi Jun 18, 2020
117ae63
Merge branch 'master' into CfnFunctions
comcalvi Jun 18, 2020
dbb9ef8
fixing merge conflicts
comcalvi Jun 18, 2020
bdab761
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 18, 2020
d390580
Merge branch 'master' of https://github.com/aws/aws-cdk into CfnFunct…
comcalvi Jun 18, 2020
14a0d64
merge conflict resolution
comcalvi Jun 18, 2020
dfb42b9
Merge branch 'CfnFunctions' of github.com:comcalvi/aws-cdk into CfnFu…
comcalvi Jun 18, 2020
9f1b2d9
Merge branch 'master' of https://github.com/aws/aws-cdk into CfnFunct…
comcalvi Jun 18, 2020
4c9d1ec
fixed typo in condition name
comcalvi Jun 18, 2020
e15c187
Merge branch 'CfnFunctions'
comcalvi Jun 18, 2020
82c4317
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 18, 2020
42c0228
removed parameters from _toCloudFormation()
comcalvi Jun 18, 2020
6a2df1e
added support for parameters in templates
comcalvi Jun 19, 2020
43198b7
fixed merge conflict
comcalvi Jun 19, 2020
cd27ff6
updated documentation
comcalvi Jun 19, 2020
1e72465
updated readme
comcalvi Jun 19, 2020
070f902
incorporated adam's comments
comcalvi Jun 19, 2020
b10a1ed
fixed spacing
comcalvi Jun 19, 2020
68d2fbc
fixed merge conflicts
comcalvi Jun 22, 2020
225d1ce
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 25, 2020
badeae8
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 26, 2020
5bf4760
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 29, 2020
479931e
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jun 29, 2020
c3fa10e
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 2, 2020
685d57d
added outputs array
comcalvi Jun 29, 2020
e334814
added support for retrieving and modifying outputs
comcalvi Jun 30, 2020
34d5c34
fixed linter issues
comcalvi Jun 30, 2020
95badbe
updated README
comcalvi Jun 30, 2020
fa39d72
updated documentation
comcalvi Jun 30, 2020
a3940e3
removed unneeded line in tests
comcalvi Jun 30, 2020
fbf17f2
added newline
comcalvi Jun 30, 2020
c719e85
incorporated PR requests
comcalvi Jul 1, 2020
f05aae1
updated the example in the readme
comcalvi Jul 1, 2020
69223d2
added support for common-named outputs. Fixed a bug in the export nam…
comcalvi Jul 1, 2020
0c55180
added a negative test case and a new error message if an output refer…
comcalvi Jul 1, 2020
7557a00
incorporated changes to PR
comcalvi Jul 6, 2020
356fc8f
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 16, 2020
6c62755
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 17, 2020
783e1f4
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 17, 2020
0e59c33
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 19, 2020
98498d1
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 21, 2020
8d5deff
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 22, 2020
59abb94
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 23, 2020
3b902af
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 27, 2020
bdc9fd1
Merge branch 'master' of https://github.com/aws/aws-cdk
comcalvi Jul 29, 2020
0541ded
added fn::sub now turns into fn::join in cfnInclude
comcalvi Jul 23, 2020
f7f6a25
added a new way to resolve tokens
comcalvi Jul 23, 2020
344d3e9
we now support pseudo parameters
comcalvi Jul 24, 2020
2d95d9d
added full support for the string form of Fn::Sub, including literals
comcalvi Jul 24, 2020
d6406fa
added support for basic usage of both Fn::Sub forms.
comcalvi Jul 24, 2020
2ac07ef
refactored code and improved testing
comcalvi Jul 27, 2020
40520d0
substantially refactored the parsing of the string argument to Fn::Sub
comcalvi Jul 27, 2020
ae3a04b
improved tests and refactored implementation
comcalvi Jul 27, 2020
ea0372d
improved documentation
comcalvi Jul 27, 2020
6ecf479
updated README
comcalvi Jul 27, 2020
bb03256
added support for yaml templates that use Fn::Sub
comcalvi Jul 27, 2020
970fa59
incorporated review comments
comcalvi Jul 28, 2020
1d91d61
incorporated review requests, improved testing
comcalvi Jul 29, 2020
58a3496
added newline
comcalvi Jul 29, 2020
3067019
Update the type of specialCaseSubRefs like in the comment
skinny85 Jul 29, 2020
4932e78
Remove the redundant map || {} expression
skinny85 Jul 29, 2020
35850a9
Merge branch 'master' into sub
mergify[bot] Jul 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions packages/@aws-cdk/cloudformation-include/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,47 +218,3 @@ bucketReadRole.addToPolicy(new iam.PolicyStatement({
resources: [bucket.attrArn],
}));
```

## Known limitations

This module is still in its early, experimental stage,
and so does not implement all features of CloudFormation templates.
All items unchecked below are currently not supported.

### Ability to retrieve CloudFormation objects from the template:

- [x] Resources
- [x] Parameters
- [x] Conditions
- [x] Outputs

### [Resource attributes](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-product-attribute-reference.html):

- [x] Properties
- [x] Condition
- [x] DependsOn
- [x] CreationPolicy
- [x] UpdatePolicy
- [x] UpdateReplacePolicy
- [x] DeletionPolicy
- [x] Metadata

### [CloudFormation functions](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html):

- [x] Ref
- [x] Fn::GetAtt
- [x] Fn::Join
- [x] Fn::If
- [x] Fn::And
- [x] Fn::Equals
- [x] Fn::Not
- [x] Fn::Or
- [x] Fn::Base64
- [x] Fn::Cidr
- [x] Fn::FindInMap
- [x] Fn::GetAZs
- [x] Fn::ImportValue
- [x] Fn::Select
- [x] Fn::Split
- [ ] Fn::Sub
- [x] Fn::Transform
4 changes: 1 addition & 3 deletions packages/@aws-cdk/cloudformation-include/lib/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ function makeTagForCfnIntrinsic(
}

const shortForms: yaml_types.Schema.CustomTag[] = [
'Base64', 'Cidr', 'FindInMap', 'GetAZs', 'ImportValue', 'Join',
'Base64', 'Cidr', 'FindInMap', 'GetAZs', 'ImportValue', 'Join', 'Sub',
'Select', 'Split', 'Transform', 'And', 'Equals', 'If', 'Not', 'Or',
].map(name => makeTagForCfnIntrinsic(name)).concat(
// ToDo: special logic for ImportValue will be needed when support for Fn::Sub is added. See
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-importvalue.html
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
makeTagForCfnIntrinsic('Ref', false),
makeTagForCfnIntrinsic('GetAtt', true, (_doc: yaml.Document, cstNode: yaml_cst.CST.Node): any => {
// The position of the leftmost period and opening bracket tell us what syntax is being used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ describe('CDK Include', () => {
includeTestTemplate(stack, 'output-referencing-nonexistant-condition.json');
}).toThrow(/Output with name 'SomeOutput' refers to a Condition with name 'NonexistantCondition' which was not found in this template/);
});

test("throws a validation exception when Fn::Sub in string form uses a key that isn't in the template", () => {
expect(() => {
includeTestTemplate(stack, 'fn-sub-key-not-in-template-string.json');
}).toThrow(/Element referenced in Fn::Sub expression with logical ID: 'AFakeResource' was not found in the template/);
});

test('throws a validation exception when Fn::Sub has an empty ${} reference', () => {
expect(() => {
includeTestTemplate(stack, 'fn-sub-${}-only.json');
}).toThrow(/Element referenced in Fn::Sub expression with logical ID: '' was not found in the template/);
});
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
});

function includeTestTemplate(scope: core.Construct, testTemplate: string): inc.CfnInclude {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"Resources": {
"Bucket": {
"Type": "Custom::ManyStrings",
"Properties": {
"SymbolsOnly": {
"DollarSign": {
"Fn::Sub": "$"
},
"OpeningBrace": {
"Fn::Sub": "{"
},
"ClosingBrace": {
"Fn::Sub": "}"
},
"DollarOpeningBrace": {
"Fn::Sub": "${"
},
"DollarClosingBrace": {
"Fn::Sub": "$}"
},
"OpeningBraceDollar": {
"Fn::Sub": "{$"
},
"ClosingBraceDollar": {
"Fn::Sub": "}$"
}
},
"SymbolsAndResources": {
"DollarSign": {
"Fn::Sub": "DoesNotExist$DoesNotExist"
},
"OpeningBrace": {
"Fn::Sub": "DoesNotExist{DoesNotExist"
},
"ClosingBrace": {
"Fn::Sub": "DoesNotExist}DoesNotExist"
},
"DollarOpeningBrace": {
"Fn::Sub": "DoesNotExist${DoesNotExist"
},
"DollarClosingBrace": {
"Fn::Sub": "DoesNotExist$}DoesNotExist"
},
"OpeningBraceDollar": {
"Fn::Sub": "DoesNotExist{$DoesNotExist"
},
"ClosingBraceDollar": {
"Fn::Sub": "DoesNotExist}$DoesNotExist"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": { "Fn::Sub": "some-bucket${!AWS::AccountId}7896${ ! DoesNotExist}${!Immediate}234" }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Sub": "${ELB.SourceSecurityGroup.GroupName}"
}
}
},
"ELB": {
"Type": "AWS::ElasticLoadBalancing::LoadBalancer",
"Properties": {
"AvailabilityZones": [
"us-east-1a"
],
"Listeners": [{
"LoadBalancerPort": "80",
"InstancePort": "80",
"Protocol": "HTTP"
}]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "bucket"
}
},
"AnotherBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": { "Fn::Sub": "${Bucket}-${!Bucket}-${Bucket.DomainName}" }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Sub": [
"${AnotherBucket.DomainName}",
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
{
"AnotherBucket": "whatever"
}
]
}
}
},
"AnotherBucket": {
"Type": "AWS::S3::Bucket"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Sub": [
"${AnotherBucket}",
{
"AnotherBucket": { "Ref" : "AnotherBucket" }
}
]
}
}
},
"AnotherBucket": {
"Type": "AWS::S3::Bucket"
}
}
}
comcalvi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "bucket"
}
},
"AnotherBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": { "Fn::Sub": "1-${AWS::Region}-foo-${Bucket}-${!Literal}-${Bucket.DomainName}-${AWS::Region}" }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Sub": "${}"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Resources": {
"Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"AccessControl": { "Fn::Sub": "${AFakeResource}" }
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
!ImportValue
!Sub ${AWS::Region}
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ Resources:
Location: location,
AnotherParameter:
Fn::Base64: AnotherValue
AccessControl:
Fn::ImportValue:
Fn::Sub:
- "${Region}-foo-${!Immediate}-foo-${Vpc}-${Vpc.Id}-${Name}"
- Name:
Ref: Vpc
Region:
Fn::Base64: AWS::Region
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
Fn::Sub: some-bucket${!AWS::AccountId}7896${ ! AWS::Region}1-1${!Immediate}234
AnotherBucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
!Sub 1-${AWS::Region}-foo-${Bucket}-${!Literal}-${Bucket.DomainName}-${AWS::Region}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Resources:
Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName:
!Sub
- "${Region}-foo-${!Immediate}-foo-${AnotherBucket}-${AnotherBucket.DomainName}-${Name}"
- Name:
Ref: AnotherBucket
Region:
Fn::Base64: AWS::Region
AnotherBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: another-bucket
Loading