Skip to content

Commit 2959621

Browse files
authored
fix(lmi): exclude None values from CapacityProvider ScalingConfig and InstanceRequirements (#3856)
1 parent 72b5611 commit 2959621

File tree

6 files changed

+458
-6
lines changed

6 files changed

+458
-6
lines changed

samtranslator/model/capacity_provider/generators.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,13 @@ def _transform_instance_requirements(self) -> Dict[str, Any]:
133133
"""
134134
instance_requirements = {}
135135

136-
if "Architectures" in self.instance_requirements:
136+
if self.instance_requirements.get("Architectures") is not None:
137137
instance_requirements["Architectures"] = self.instance_requirements["Architectures"]
138138

139-
if "AllowedTypes" in self.instance_requirements:
139+
if self.instance_requirements.get("AllowedTypes") is not None:
140140
instance_requirements["AllowedInstanceTypes"] = self.instance_requirements["AllowedTypes"]
141141

142-
if "ExcludedTypes" in self.instance_requirements:
142+
if self.instance_requirements.get("ExcludedTypes") is not None:
143143
instance_requirements["ExcludedInstanceTypes"] = self.instance_requirements["ExcludedTypes"]
144144

145145
return instance_requirements
@@ -150,11 +150,11 @@ def _transform_scaling_config(self) -> Dict[str, Any]:
150150
"""
151151
scaling_config = {}
152152

153-
if "MaxVCpuCount" in self.scaling_config:
153+
if self.scaling_config.get("MaxVCpuCount") is not None:
154154
scaling_config["MaxVCpuCount"] = self.scaling_config["MaxVCpuCount"]
155155

156156
# Handle AverageCPUUtilization structure
157-
if "AverageCPUUtilization" in self.scaling_config:
157+
if self.scaling_config.get("AverageCPUUtilization") is not None:
158158
scaling_config["ScalingMode"] = "Manual"
159159
scaling_policies = []
160160

samtranslator/model/sam_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ def to_cloudformation(self, **kwargs: Any) -> List[Resource]:
15041504
instance_requirements=(
15051505
model.InstanceRequirements.dict(exclude_none=True) if model.InstanceRequirements else None
15061506
),
1507-
scaling_config=model.ScalingConfig.dict() if model.ScalingConfig else None,
1507+
scaling_config=model.ScalingConfig.dict(exclude_none=True) if model.ScalingConfig else None,
15081508
kms_key_arn=passthrough_value(model.KMSKeyArn),
15091509
depends_on=self.depends_on,
15101510
resource_attributes=self.resource_attributes,

tests/translator/input/capacity_provider_full.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,35 @@ Resources:
2626
MaxVCpuCount: 10
2727
AverageCPUUtilization: 75.0
2828
KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv
29+
30+
CapacityProviderTwo:
31+
Type: AWS::Serverless::CapacityProvider
32+
Properties:
33+
CapacityProviderName: customized-capacity-provider
34+
VpcConfig:
35+
SecurityGroupIds:
36+
- sg-12345678
37+
- sg-87654321
38+
SubnetIds:
39+
- subnet-12345678
40+
ScalingConfig:
41+
AverageCPUUtilization: 75.0
42+
KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv
43+
44+
PartialCapacityProviderThree:
45+
Type: AWS::Serverless::CapacityProvider
46+
Properties:
47+
VpcConfig:
48+
SecurityGroupIds:
49+
- sg-12345678
50+
SubnetIds:
51+
- subnet-12345678
52+
Tags:
53+
Team: Tooling
54+
PropagateTags: false
55+
InstanceRequirements:
56+
AllowedTypes:
57+
- c5.xlarge
58+
ScalingConfig:
59+
MaxVCpuCount: 10
60+
KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv

tests/translator/output/aws-cn/capacity_provider_full.json

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,75 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Description": "Full CapacityProvider configuration test",
44
"Resources": {
5+
"CapacityProviderTwo": {
6+
"Properties": {
7+
"CapacityProviderName": "customized-capacity-provider",
8+
"CapacityProviderScalingConfig": {
9+
"ScalingMode": "Manual",
10+
"ScalingPolicies": [
11+
{
12+
"PredefinedMetricType": "LambdaCapacityProviderAverageCPUUtilization",
13+
"TargetValue": 75.0
14+
}
15+
]
16+
},
17+
"KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv",
18+
"PermissionsConfig": {
19+
"CapacityProviderOperatorRoleArn": {
20+
"Fn::GetAtt": [
21+
"CapacityProviderTwoOperatorRole",
22+
"Arn"
23+
]
24+
}
25+
},
26+
"Tags": [
27+
{
28+
"Key": "lambda:createdBy",
29+
"Value": "SAM"
30+
}
31+
],
32+
"VpcConfig": {
33+
"SecurityGroupIds": [
34+
"sg-12345678",
35+
"sg-87654321"
36+
],
37+
"SubnetIds": [
38+
"subnet-12345678"
39+
]
40+
}
41+
},
42+
"Type": "AWS::Lambda::CapacityProvider"
43+
},
44+
"CapacityProviderTwoOperatorRole": {
45+
"Properties": {
46+
"AssumeRolePolicyDocument": {
47+
"Statement": [
48+
{
49+
"Action": [
50+
"sts:AssumeRole"
51+
],
52+
"Effect": "Allow",
53+
"Principal": {
54+
"Service": [
55+
"lambda.amazonaws.com"
56+
]
57+
}
58+
}
59+
],
60+
"Version": "2012-10-17"
61+
},
62+
"ManagedPolicyArns": [
63+
"arn:aws-cn:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator"
64+
],
65+
"Tags": [
66+
{
67+
"Key": "lambda:createdBy",
68+
"Value": "SAM"
69+
}
70+
]
71+
},
72+
"Type": "AWS::IAM::Role"
73+
},
574
"FullCapacityProvider": {
675
"Properties": {
776
"CapacityProviderName": "customized-capacity-provider",
@@ -88,6 +157,77 @@
88157
]
89158
},
90159
"Type": "AWS::IAM::Role"
160+
},
161+
"PartialCapacityProviderThree": {
162+
"Properties": {
163+
"CapacityProviderScalingConfig": {
164+
"MaxVCpuCount": 10,
165+
"ScalingMode": "Auto"
166+
},
167+
"InstanceRequirements": {
168+
"AllowedInstanceTypes": [
169+
"c5.xlarge"
170+
]
171+
},
172+
"KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv",
173+
"PermissionsConfig": {
174+
"CapacityProviderOperatorRoleArn": {
175+
"Fn::GetAtt": [
176+
"PartialCapacityProviderThreeOperatorRole",
177+
"Arn"
178+
]
179+
}
180+
},
181+
"Tags": [
182+
{
183+
"Key": "Team",
184+
"Value": "Tooling"
185+
},
186+
{
187+
"Key": "lambda:createdBy",
188+
"Value": "SAM"
189+
}
190+
],
191+
"VpcConfig": {
192+
"SecurityGroupIds": [
193+
"sg-12345678"
194+
],
195+
"SubnetIds": [
196+
"subnet-12345678"
197+
]
198+
}
199+
},
200+
"Type": "AWS::Lambda::CapacityProvider"
201+
},
202+
"PartialCapacityProviderThreeOperatorRole": {
203+
"Properties": {
204+
"AssumeRolePolicyDocument": {
205+
"Statement": [
206+
{
207+
"Action": [
208+
"sts:AssumeRole"
209+
],
210+
"Effect": "Allow",
211+
"Principal": {
212+
"Service": [
213+
"lambda.amazonaws.com"
214+
]
215+
}
216+
}
217+
],
218+
"Version": "2012-10-17"
219+
},
220+
"ManagedPolicyArns": [
221+
"arn:aws-cn:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator"
222+
],
223+
"Tags": [
224+
{
225+
"Key": "lambda:createdBy",
226+
"Value": "SAM"
227+
}
228+
]
229+
},
230+
"Type": "AWS::IAM::Role"
91231
}
92232
}
93233
}

tests/translator/output/aws-us-gov/capacity_provider_full.json

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,75 @@
22
"AWSTemplateFormatVersion": "2010-09-09",
33
"Description": "Full CapacityProvider configuration test",
44
"Resources": {
5+
"CapacityProviderTwo": {
6+
"Properties": {
7+
"CapacityProviderName": "customized-capacity-provider",
8+
"CapacityProviderScalingConfig": {
9+
"ScalingMode": "Manual",
10+
"ScalingPolicies": [
11+
{
12+
"PredefinedMetricType": "LambdaCapacityProviderAverageCPUUtilization",
13+
"TargetValue": 75.0
14+
}
15+
]
16+
},
17+
"KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv",
18+
"PermissionsConfig": {
19+
"CapacityProviderOperatorRoleArn": {
20+
"Fn::GetAtt": [
21+
"CapacityProviderTwoOperatorRole",
22+
"Arn"
23+
]
24+
}
25+
},
26+
"Tags": [
27+
{
28+
"Key": "lambda:createdBy",
29+
"Value": "SAM"
30+
}
31+
],
32+
"VpcConfig": {
33+
"SecurityGroupIds": [
34+
"sg-12345678",
35+
"sg-87654321"
36+
],
37+
"SubnetIds": [
38+
"subnet-12345678"
39+
]
40+
}
41+
},
42+
"Type": "AWS::Lambda::CapacityProvider"
43+
},
44+
"CapacityProviderTwoOperatorRole": {
45+
"Properties": {
46+
"AssumeRolePolicyDocument": {
47+
"Statement": [
48+
{
49+
"Action": [
50+
"sts:AssumeRole"
51+
],
52+
"Effect": "Allow",
53+
"Principal": {
54+
"Service": [
55+
"lambda.amazonaws.com"
56+
]
57+
}
58+
}
59+
],
60+
"Version": "2012-10-17"
61+
},
62+
"ManagedPolicyArns": [
63+
"arn:aws-us-gov:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator"
64+
],
65+
"Tags": [
66+
{
67+
"Key": "lambda:createdBy",
68+
"Value": "SAM"
69+
}
70+
]
71+
},
72+
"Type": "AWS::IAM::Role"
73+
},
574
"FullCapacityProvider": {
675
"Properties": {
776
"CapacityProviderName": "customized-capacity-provider",
@@ -88,6 +157,77 @@
88157
]
89158
},
90159
"Type": "AWS::IAM::Role"
160+
},
161+
"PartialCapacityProviderThree": {
162+
"Properties": {
163+
"CapacityProviderScalingConfig": {
164+
"MaxVCpuCount": 10,
165+
"ScalingMode": "Auto"
166+
},
167+
"InstanceRequirements": {
168+
"AllowedInstanceTypes": [
169+
"c5.xlarge"
170+
]
171+
},
172+
"KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv",
173+
"PermissionsConfig": {
174+
"CapacityProviderOperatorRoleArn": {
175+
"Fn::GetAtt": [
176+
"PartialCapacityProviderThreeOperatorRole",
177+
"Arn"
178+
]
179+
}
180+
},
181+
"Tags": [
182+
{
183+
"Key": "Team",
184+
"Value": "Tooling"
185+
},
186+
{
187+
"Key": "lambda:createdBy",
188+
"Value": "SAM"
189+
}
190+
],
191+
"VpcConfig": {
192+
"SecurityGroupIds": [
193+
"sg-12345678"
194+
],
195+
"SubnetIds": [
196+
"subnet-12345678"
197+
]
198+
}
199+
},
200+
"Type": "AWS::Lambda::CapacityProvider"
201+
},
202+
"PartialCapacityProviderThreeOperatorRole": {
203+
"Properties": {
204+
"AssumeRolePolicyDocument": {
205+
"Statement": [
206+
{
207+
"Action": [
208+
"sts:AssumeRole"
209+
],
210+
"Effect": "Allow",
211+
"Principal": {
212+
"Service": [
213+
"lambda.amazonaws.com"
214+
]
215+
}
216+
}
217+
],
218+
"Version": "2012-10-17"
219+
},
220+
"ManagedPolicyArns": [
221+
"arn:aws-us-gov:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator"
222+
],
223+
"Tags": [
224+
{
225+
"Key": "lambda:createdBy",
226+
"Value": "SAM"
227+
}
228+
]
229+
},
230+
"Type": "AWS::IAM::Role"
91231
}
92232
}
93233
}

0 commit comments

Comments
 (0)