Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 11 additions & 1 deletion samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from samtranslator.translator import logical_id_generator
from samtranslator.translator.arn_generator import ArnGenerator
from samtranslator.model.intrinsics import (
is_intrinsic,
is_intrinsic_if,
is_intrinsic_no_value,
ref,
Expand Down Expand Up @@ -1224,6 +1225,15 @@ def _get_retention_policy_value(self):
:return: value for the DeletionPolicy attribute.
"""

if is_intrinsic(self.RetentionPolicy):
# RetentionPolicy attribute of AWS::Serverless::LayerVersion does set the DeletionPolicy
# attribute. And DeletionPolicy attribute does not support intrinsic values.
raise InvalidResourceException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we communicate that the customer could use the top level Attribute of DeletionPolicy? For customers needing or wanting the Intrinsic, this might be the only way forward for them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, intrinsics are not supported for top level attributes; https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html

I tested with a template but I wasn't able to deploy it.

self.logical_id,
"'RetentionPolicy' does not accept intrinsic functions, "
"please use one of the following options: {}".format([self.RETAIN, self.DELETE]),
)

if self.RetentionPolicy is None:
return None
elif self.RetentionPolicy.lower() == self.RETAIN.lower():
Expand All @@ -1233,7 +1243,7 @@ def _get_retention_policy_value(self):
elif self.RetentionPolicy.lower() not in self.retention_policy_options:
raise InvalidResourceException(
self.logical_id,
"'{}' must be one of the following options: {}.".format("RetentionPolicy", [self.RETAIN, self.DELETE]),
"'RetentionPolicy' must be one of the following options: {}.".format([self.RETAIN, self.DELETE]),
)


Expand Down
19 changes: 18 additions & 1 deletion tests/translator/input/basic_layer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Conditions:
- beta
- beta

Parameters:
DeletePolicy:
Default: Retain
Type: String

Resources:
MinimalLayer:
Type: 'AWS::Serverless::LayerVersion'
Expand Down Expand Up @@ -35,4 +40,16 @@ Resources:
Type: 'AWS::Serverless::LayerVersion'
Condition: TestCondition
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
ContentUri: s3://sam-demo-bucket/layer.zip

LayerWithCaseInsensitiveRetentionPolicy:
Type: 'AWS::Serverless::LayerVersion'
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
RetentionPolicy: DeleTe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️


LayerWithRetentionPolicyParam:
Type: 'AWS::Serverless::LayerVersion'
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
RetentionPolicy: !Ref DeletePolicy
11 changes: 10 additions & 1 deletion tests/translator/input/error_layer_invalid_properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Parameters:
Default:
- Retain
Type: List
DeletePolicyIf:
Default: False
Type: Boolean

Resources:
LayerWithRuntimesString:
Expand Down Expand Up @@ -43,4 +46,10 @@ Resources:
Type: 'AWS::Serverless::LayerVersion'
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
RetentionPolicy: !Ref DeleteList
RetentionPolicy: !Ref DeleteList

LayerWithRetentionPolicyAsIntrinsic:
Type: 'AWS::Serverless::LayerVersion'
Properties:
ContentUri: s3://sam-demo-bucket/layer.zip
RetentionPolicy: !If [DeletePolicyIf, Retain, Delete]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for Ref? I think that intrinsic is supported right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap it is supported, and we already have tests in basic_layer.yaml file above.

30 changes: 29 additions & 1 deletion tests/translator/output/aws-cn/basic_layer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"beta"
]
}
},
},
"Parameters": {
"DeletePolicy": {
"Default": "Retain",
"Type": "String"
}
},
"Resources": {
"LayerWithCondition7c655e10ea": {
"DeletionPolicy": "Retain",
Expand Down Expand Up @@ -59,6 +65,28 @@
},
"LayerName": "LayerWithContentUriObject"
}
},
"LayerWithCaseInsensitiveRetentionPolicyead52a491d": {
"DeletionPolicy": "Delete",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithCaseInsensitiveRetentionPolicy"
}
},
"LayerWithRetentionPolicyParamcc64815342": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithRetentionPolicyParam"
}
}
}
}
32 changes: 30 additions & 2 deletions tests/translator/output/aws-us-gov/basic_layer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"beta"
]
}
},
},
"Parameters": {
"DeletePolicy": {
"Default": "Retain",
"Type": "String"
}
},
"Resources": {
"LayerWithCondition7c655e10ea": {
"DeletionPolicy": "Retain",
Expand Down Expand Up @@ -47,7 +53,7 @@
"python2.7"
]
}
},
},
"LayerWithContentUriObjectbdbf1b82ac": {
"DeletionPolicy": "Delete",
"Type": "AWS::Lambda::LayerVersion",
Expand All @@ -59,6 +65,28 @@
},
"LayerName": "LayerWithContentUriObject"
}
},
"LayerWithCaseInsensitiveRetentionPolicyead52a491d": {
"DeletionPolicy": "Delete",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithCaseInsensitiveRetentionPolicy"
}
},
"LayerWithRetentionPolicyParamcc64815342": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithRetentionPolicyParam"
}
}
}
}
30 changes: 29 additions & 1 deletion tests/translator/output/basic_layer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"beta"
]
}
},
},
"Parameters": {
"DeletePolicy": {
"Default": "Retain",
"Type": "String"
}
},
"Resources": {
"LayerWithCondition7c655e10ea": {
"DeletionPolicy": "Retain",
Expand Down Expand Up @@ -59,6 +65,28 @@
},
"LayerName": "LayerWithContentUriObject"
}
},
"LayerWithCaseInsensitiveRetentionPolicyead52a491d": {
"DeletionPolicy": "Delete",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithCaseInsensitiveRetentionPolicy"
}
},
"LayerWithRetentionPolicyParamcc64815342": {
"DeletionPolicy": "Retain",
"Type": "AWS::Lambda::LayerVersion",
"Properties": {
"Content": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "layer.zip"
},
"LayerName": "LayerWithRetentionPolicyParam"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"errorMessage": "Resource with id [LayerWithLicenseInfoList] is invalid. Type of property 'LicenseInfo' is invalid. Resource with id [LayerWithNoContentUri] is invalid. Missing required property 'ContentUri'. Resource with id [LayerWithRetentionPolicy] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRetentionPolicyListParam] is invalid. Could not resolve parameter for 'RetentionPolicy' or parameter is not a String. Resource with id [LayerWithRetentionPolicyParam] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRuntimesString] is invalid. Type of property 'CompatibleRuntimes' is invalid."
}
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 6. Resource with id [LayerWithLicenseInfoList] is invalid. Type of property 'LicenseInfo' is invalid. Resource with id [LayerWithNoContentUri] is invalid. Missing required property 'ContentUri'. Resource with id [LayerWithRetentionPolicy] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRetentionPolicyListParam] is invalid. Could not resolve parameter for 'RetentionPolicy' or parameter is not a String. Resource with id [LayerWithRetentionPolicyParam] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRuntimesString] is invalid. Type of property 'CompatibleRuntimes' is invalid."
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 7. Resource with id [LayerWithLicenseInfoList] is invalid. Type of property 'LicenseInfo' is invalid. Resource with id [LayerWithNoContentUri] is invalid. Missing required property 'ContentUri'. Resource with id [LayerWithRetentionPolicy] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRetentionPolicyAsIntrinsic] is invalid. 'RetentionPolicy' does not accept intrinsic functions, please use one of the following options: ['Retain', 'Delete'] Resource with id [LayerWithRetentionPolicyListParam] is invalid. Could not resolve parameter for 'RetentionPolicy' or parameter is not a String. Resource with id [LayerWithRetentionPolicyParam] is invalid. 'RetentionPolicy' must be one of the following options: ['Retain', 'Delete']. Resource with id [LayerWithRuntimesString] is invalid. Type of property 'CompatibleRuntimes' is invalid."
}