Skip to content

Commit c75a133

Browse files
authored
chore: Remove Validation on DeletionPolicy and UpdateReplacePolicy (aws#3317)
1 parent e323efc commit c75a133

10 files changed

+409
-9
lines changed

.cfnlintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ ignore_templates:
131131
- tests/translator/output/**/function_with_mq_using_autogen_role.json # Property "EventSourceArn" can Fn::GetAtt to a resource of types [AWS::DynamoDB::GlobalTable, AWS::DynamoDB::Table, AWS::Kinesis::Stream, AWS::Kinesis::StreamConsumer, AWS::SQS::Queue]
132132
- tests/translator/output/**/function_with_tracing.json # Obsolete DependsOn on resource
133133
- tests/translator/output/**/api_with_propagate_tags.json # TODO: Intentional error transform tests. Will be updated.
134+
- tests/translator/output/**/function_with_intrinsics_resource_attribute.json # CFN now supports intrinsics in DeletionPolicy
134135
ignore_checks:
135136
- E2531 # Deprecated runtime; not relevant for transform tests
136137
- W2531 # EOL runtime; not relevant for transform tests

integration/combination/test_function_with_implicit_api_with_timeout.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ def test_function_with_implicit_api_with_timeout(self):
1616
rest_api_id = self.get_physical_id_by_type("AWS::ApiGateway::RestApi")
1717
resources = apigw_client.get_resources(restApiId=rest_api_id)["items"]
1818

19-
method = apigw_client.get_method(restApiId=rest_api_id, resourceId=resources[0]["id"], httpMethod="GET")
19+
resource = get_resource_by_path(resources, "/hello")
20+
method = apigw_client.get_method(restApiId=rest_api_id, resourceId=resource["id"], httpMethod="GET")
2021
method_integration = method["methodIntegration"]
2122
self.assertEqual(method_integration["timeoutInMillis"], expected_timeout)
23+
24+
25+
def get_resource_by_path(resources, path):
26+
for resource in resources:
27+
if resource["path"] == path:
28+
return resource
29+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from integration.helpers.base_test import BaseTest
2+
3+
4+
class TestFunctionWithIntrinsicsResourceAttributes(BaseTest):
5+
def test_function_with_intrinsics_resource_attributes(self):
6+
# simply verify the stack is deployed successfully is enough
7+
self.create_and_verify_stack("combination/function_with_intrinsics_resource_attribute")
8+
9+
stack_outputs = self.get_stack_outputs()
10+
id_dev_stack = stack_outputs["IsDevStack"]
11+
self.assertEqual(id_dev_stack, "true")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"LogicalResourceId": "MyLambdaFunction",
4+
"ResourceType": "AWS::Lambda::Function"
5+
},
6+
{
7+
"LogicalResourceId": "MyLambdaFunctionRole",
8+
"ResourceType": "AWS::IAM::Role"
9+
}
10+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: A template to test timeout support for implicit APIs.
3+
4+
Parameters:
5+
IsDevStack: {Type: String, Default: 'true', AllowedValues: ['true', 'false']}
6+
Conditions:
7+
IsDevStack: !Equals [!Ref IsDevStack, 'true']
8+
NotIsDevStack: !Not [Condition: IsDevStack]
9+
10+
Resources:
11+
MyLambdaFunction:
12+
DeletionPolicy: !If [NotIsDevStack, Retain, Delete]
13+
Type: AWS::Serverless::Function
14+
Properties:
15+
Handler: index.handler
16+
Runtime: nodejs16.x
17+
MemorySize: 128
18+
Timeout: 3
19+
InlineCode: |
20+
exports.handler = async () => 'Hello World!'
21+
22+
Outputs:
23+
IsDevStack:
24+
Value: !Ref IsDevStack
25+
26+
Metadata:
27+
SamTransformTest: true

samtranslator/sdk/resource.py

-8
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ def valid(self) -> bool:
4343
if self.condition and not IS_STR(self.condition, should_raise=False):
4444
raise InvalidDocumentException([InvalidTemplateException("Every Condition member must be a string.")])
4545

46-
if self.deletion_policy and not IS_STR(self.deletion_policy, should_raise=False):
47-
raise InvalidDocumentException([InvalidTemplateException("Every DeletionPolicy member must be a string.")])
48-
49-
if self.update_replace_policy and not IS_STR(self.update_replace_policy, should_raise=False):
50-
raise InvalidDocumentException(
51-
[InvalidTemplateException("Every UpdateReplacePolicy member must be a string.")]
52-
)
53-
5446
# TODO: should we raise exception if `self.type` is not a string?
5547
return isinstance(self.type, str) and SamResourceType.has_value(self.type)
5648

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Parameters:
2+
IsDevStack: {Type: String, Default: 'true', AllowedValues: ['true', 'false']}
3+
Conditions:
4+
IsDevStack: !Equals [!Ref IsDevStack, 'true']
5+
NotIsDevStack: !Not [Condition: IsDevStack]
6+
7+
Resources:
8+
FunctionWithArchitecturesIntrinsic:
9+
Type: AWS::Serverless::Function
10+
UpdateReplacePolicy: !Equals [NotIsDevStack, Retain]
11+
DeletionPolicy: !Equals [NotIsDevStack, Retain]
12+
Properties:
13+
CodeUri: s3://sam-demo-bucket/hello.zip
14+
Description: Created by SAM
15+
Handler: index.handler
16+
MemorySize: 1024
17+
Runtime: nodejs12.x
18+
Timeout: 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"Conditions": {
3+
"IsDevStack": {
4+
"Fn::Equals": [
5+
{
6+
"Ref": "IsDevStack"
7+
},
8+
"true"
9+
]
10+
},
11+
"NotIsDevStack": {
12+
"Fn::Not": [
13+
{
14+
"Condition": "IsDevStack"
15+
}
16+
]
17+
}
18+
},
19+
"Parameters": {
20+
"IsDevStack": {
21+
"AllowedValues": [
22+
"true",
23+
"false"
24+
],
25+
"Default": "true",
26+
"Type": "String"
27+
}
28+
},
29+
"Resources": {
30+
"FunctionWithArchitecturesIntrinsic": {
31+
"DeletionPolicy": {
32+
"Fn::Equals": [
33+
"NotIsDevStack",
34+
"Retain"
35+
]
36+
},
37+
"Properties": {
38+
"Code": {
39+
"S3Bucket": "sam-demo-bucket",
40+
"S3Key": "hello.zip"
41+
},
42+
"Description": "Created by SAM",
43+
"Handler": "index.handler",
44+
"MemorySize": 1024,
45+
"Role": {
46+
"Fn::GetAtt": [
47+
"FunctionWithArchitecturesIntrinsicRole",
48+
"Arn"
49+
]
50+
},
51+
"Runtime": "nodejs12.x",
52+
"Tags": [
53+
{
54+
"Key": "lambda:createdBy",
55+
"Value": "SAM"
56+
}
57+
],
58+
"Timeout": 3
59+
},
60+
"Type": "AWS::Lambda::Function",
61+
"UpdateReplacePolicy": {
62+
"Fn::Equals": [
63+
"NotIsDevStack",
64+
"Retain"
65+
]
66+
}
67+
},
68+
"FunctionWithArchitecturesIntrinsicRole": {
69+
"DeletionPolicy": {
70+
"Fn::Equals": [
71+
"NotIsDevStack",
72+
"Retain"
73+
]
74+
},
75+
"Properties": {
76+
"AssumeRolePolicyDocument": {
77+
"Statement": [
78+
{
79+
"Action": [
80+
"sts:AssumeRole"
81+
],
82+
"Effect": "Allow",
83+
"Principal": {
84+
"Service": [
85+
"lambda.amazonaws.com"
86+
]
87+
}
88+
}
89+
],
90+
"Version": "2012-10-17"
91+
},
92+
"ManagedPolicyArns": [
93+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
94+
],
95+
"Tags": [
96+
{
97+
"Key": "lambda:createdBy",
98+
"Value": "SAM"
99+
}
100+
]
101+
},
102+
"Type": "AWS::IAM::Role",
103+
"UpdateReplacePolicy": {
104+
"Fn::Equals": [
105+
"NotIsDevStack",
106+
"Retain"
107+
]
108+
}
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"Conditions": {
3+
"IsDevStack": {
4+
"Fn::Equals": [
5+
{
6+
"Ref": "IsDevStack"
7+
},
8+
"true"
9+
]
10+
},
11+
"NotIsDevStack": {
12+
"Fn::Not": [
13+
{
14+
"Condition": "IsDevStack"
15+
}
16+
]
17+
}
18+
},
19+
"Parameters": {
20+
"IsDevStack": {
21+
"AllowedValues": [
22+
"true",
23+
"false"
24+
],
25+
"Default": "true",
26+
"Type": "String"
27+
}
28+
},
29+
"Resources": {
30+
"FunctionWithArchitecturesIntrinsic": {
31+
"DeletionPolicy": {
32+
"Fn::Equals": [
33+
"NotIsDevStack",
34+
"Retain"
35+
]
36+
},
37+
"Properties": {
38+
"Code": {
39+
"S3Bucket": "sam-demo-bucket",
40+
"S3Key": "hello.zip"
41+
},
42+
"Description": "Created by SAM",
43+
"Handler": "index.handler",
44+
"MemorySize": 1024,
45+
"Role": {
46+
"Fn::GetAtt": [
47+
"FunctionWithArchitecturesIntrinsicRole",
48+
"Arn"
49+
]
50+
},
51+
"Runtime": "nodejs12.x",
52+
"Tags": [
53+
{
54+
"Key": "lambda:createdBy",
55+
"Value": "SAM"
56+
}
57+
],
58+
"Timeout": 3
59+
},
60+
"Type": "AWS::Lambda::Function",
61+
"UpdateReplacePolicy": {
62+
"Fn::Equals": [
63+
"NotIsDevStack",
64+
"Retain"
65+
]
66+
}
67+
},
68+
"FunctionWithArchitecturesIntrinsicRole": {
69+
"DeletionPolicy": {
70+
"Fn::Equals": [
71+
"NotIsDevStack",
72+
"Retain"
73+
]
74+
},
75+
"Properties": {
76+
"AssumeRolePolicyDocument": {
77+
"Statement": [
78+
{
79+
"Action": [
80+
"sts:AssumeRole"
81+
],
82+
"Effect": "Allow",
83+
"Principal": {
84+
"Service": [
85+
"lambda.amazonaws.com"
86+
]
87+
}
88+
}
89+
],
90+
"Version": "2012-10-17"
91+
},
92+
"ManagedPolicyArns": [
93+
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
94+
],
95+
"Tags": [
96+
{
97+
"Key": "lambda:createdBy",
98+
"Value": "SAM"
99+
}
100+
]
101+
},
102+
"Type": "AWS::IAM::Role",
103+
"UpdateReplacePolicy": {
104+
"Fn::Equals": [
105+
"NotIsDevStack",
106+
"Retain"
107+
]
108+
}
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)