diff --git a/samtranslator/model/capacity_provider/generators.py b/samtranslator/model/capacity_provider/generators.py index 40c84b6b3..4983d7e91 100644 --- a/samtranslator/model/capacity_provider/generators.py +++ b/samtranslator/model/capacity_provider/generators.py @@ -133,13 +133,13 @@ def _transform_instance_requirements(self) -> Dict[str, Any]: """ instance_requirements = {} - if "Architectures" in self.instance_requirements: + if self.instance_requirements.get("Architectures") is not None: instance_requirements["Architectures"] = self.instance_requirements["Architectures"] - if "AllowedTypes" in self.instance_requirements: + if self.instance_requirements.get("AllowedTypes") is not None: instance_requirements["AllowedInstanceTypes"] = self.instance_requirements["AllowedTypes"] - if "ExcludedTypes" in self.instance_requirements: + if self.instance_requirements.get("ExcludedTypes") is not None: instance_requirements["ExcludedInstanceTypes"] = self.instance_requirements["ExcludedTypes"] return instance_requirements @@ -150,11 +150,11 @@ def _transform_scaling_config(self) -> Dict[str, Any]: """ scaling_config = {} - if "MaxVCpuCount" in self.scaling_config: + if self.scaling_config.get("MaxVCpuCount") is not None: scaling_config["MaxVCpuCount"] = self.scaling_config["MaxVCpuCount"] # Handle AverageCPUUtilization structure - if "AverageCPUUtilization" in self.scaling_config: + if self.scaling_config.get("AverageCPUUtilization") is not None: scaling_config["ScalingMode"] = "Manual" scaling_policies = [] diff --git a/samtranslator/model/sam_resources.py b/samtranslator/model/sam_resources.py index 7d462b959..fc19fec71 100644 --- a/samtranslator/model/sam_resources.py +++ b/samtranslator/model/sam_resources.py @@ -1504,7 +1504,7 @@ def to_cloudformation(self, **kwargs: Any) -> List[Resource]: instance_requirements=( model.InstanceRequirements.dict(exclude_none=True) if model.InstanceRequirements else None ), - scaling_config=model.ScalingConfig.dict() if model.ScalingConfig else None, + scaling_config=model.ScalingConfig.dict(exclude_none=True) if model.ScalingConfig else None, kms_key_arn=passthrough_value(model.KMSKeyArn), depends_on=self.depends_on, resource_attributes=self.resource_attributes, diff --git a/tests/translator/input/capacity_provider_full.yaml b/tests/translator/input/capacity_provider_full.yaml index bf8b46288..c446560c9 100644 --- a/tests/translator/input/capacity_provider_full.yaml +++ b/tests/translator/input/capacity_provider_full.yaml @@ -26,3 +26,35 @@ Resources: MaxVCpuCount: 10 AverageCPUUtilization: 75.0 KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv + + CapacityProviderTwo: + Type: AWS::Serverless::CapacityProvider + Properties: + CapacityProviderName: customized-capacity-provider + VpcConfig: + SecurityGroupIds: + - sg-12345678 + - sg-87654321 + SubnetIds: + - subnet-12345678 + ScalingConfig: + AverageCPUUtilization: 75.0 + KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv + + PartialCapacityProviderThree: + Type: AWS::Serverless::CapacityProvider + Properties: + VpcConfig: + SecurityGroupIds: + - sg-12345678 + SubnetIds: + - subnet-12345678 + Tags: + Team: Tooling + PropagateTags: false + InstanceRequirements: + AllowedTypes: + - c5.xlarge + ScalingConfig: + MaxVCpuCount: 10 + KMSKeyArn: arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv diff --git a/tests/translator/output/aws-cn/capacity_provider_full.json b/tests/translator/output/aws-cn/capacity_provider_full.json index 6c976f0d6..ec960ea29 100644 --- a/tests/translator/output/aws-cn/capacity_provider_full.json +++ b/tests/translator/output/aws-cn/capacity_provider_full.json @@ -2,6 +2,75 @@ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Full CapacityProvider configuration test", "Resources": { + "CapacityProviderTwo": { + "Properties": { + "CapacityProviderName": "customized-capacity-provider", + "CapacityProviderScalingConfig": { + "ScalingMode": "Manual", + "ScalingPolicies": [ + { + "PredefinedMetricType": "LambdaCapacityProviderAverageCPUUtilization", + "TargetValue": 75.0 + } + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "CapacityProviderTwoOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678", + "sg-87654321" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "CapacityProviderTwoOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + }, "FullCapacityProvider": { "Properties": { "CapacityProviderName": "customized-capacity-provider", @@ -88,6 +157,77 @@ ] }, "Type": "AWS::IAM::Role" + }, + "PartialCapacityProviderThree": { + "Properties": { + "CapacityProviderScalingConfig": { + "MaxVCpuCount": 10, + "ScalingMode": "Auto" + }, + "InstanceRequirements": { + "AllowedInstanceTypes": [ + "c5.xlarge" + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "PartialCapacityProviderThreeOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "Team", + "Value": "Tooling" + }, + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "PartialCapacityProviderThreeOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-cn:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" } } } diff --git a/tests/translator/output/aws-us-gov/capacity_provider_full.json b/tests/translator/output/aws-us-gov/capacity_provider_full.json index a56787a80..e4c4d5141 100644 --- a/tests/translator/output/aws-us-gov/capacity_provider_full.json +++ b/tests/translator/output/aws-us-gov/capacity_provider_full.json @@ -2,6 +2,75 @@ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Full CapacityProvider configuration test", "Resources": { + "CapacityProviderTwo": { + "Properties": { + "CapacityProviderName": "customized-capacity-provider", + "CapacityProviderScalingConfig": { + "ScalingMode": "Manual", + "ScalingPolicies": [ + { + "PredefinedMetricType": "LambdaCapacityProviderAverageCPUUtilization", + "TargetValue": 75.0 + } + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "CapacityProviderTwoOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678", + "sg-87654321" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "CapacityProviderTwoOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + }, "FullCapacityProvider": { "Properties": { "CapacityProviderName": "customized-capacity-provider", @@ -88,6 +157,77 @@ ] }, "Type": "AWS::IAM::Role" + }, + "PartialCapacityProviderThree": { + "Properties": { + "CapacityProviderScalingConfig": { + "MaxVCpuCount": 10, + "ScalingMode": "Auto" + }, + "InstanceRequirements": { + "AllowedInstanceTypes": [ + "c5.xlarge" + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "PartialCapacityProviderThreeOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "Team", + "Value": "Tooling" + }, + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "PartialCapacityProviderThreeOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws-us-gov:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" } } } diff --git a/tests/translator/output/capacity_provider_full.json b/tests/translator/output/capacity_provider_full.json index 2eedfafd2..992b088b3 100644 --- a/tests/translator/output/capacity_provider_full.json +++ b/tests/translator/output/capacity_provider_full.json @@ -2,6 +2,75 @@ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Full CapacityProvider configuration test", "Resources": { + "CapacityProviderTwo": { + "Properties": { + "CapacityProviderName": "customized-capacity-provider", + "CapacityProviderScalingConfig": { + "ScalingMode": "Manual", + "ScalingPolicies": [ + { + "PredefinedMetricType": "LambdaCapacityProviderAverageCPUUtilization", + "TargetValue": 75.0 + } + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "CapacityProviderTwoOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678", + "sg-87654321" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "CapacityProviderTwoOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" + }, "FullCapacityProvider": { "Properties": { "CapacityProviderName": "customized-capacity-provider", @@ -88,6 +157,77 @@ ] }, "Type": "AWS::IAM::Role" + }, + "PartialCapacityProviderThree": { + "Properties": { + "CapacityProviderScalingConfig": { + "MaxVCpuCount": 10, + "ScalingMode": "Auto" + }, + "InstanceRequirements": { + "AllowedInstanceTypes": [ + "c5.xlarge" + ] + }, + "KMSKeyArn": "arn:aws:kms:us-east-1:123456789012:key/abcd1234-ef56-gh78-ij90-klmnopqrstuv", + "PermissionsConfig": { + "CapacityProviderOperatorRoleArn": { + "Fn::GetAtt": [ + "PartialCapacityProviderThreeOperatorRole", + "Arn" + ] + } + }, + "Tags": [ + { + "Key": "Team", + "Value": "Tooling" + }, + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ], + "VpcConfig": { + "SecurityGroupIds": [ + "sg-12345678" + ], + "SubnetIds": [ + "subnet-12345678" + ] + } + }, + "Type": "AWS::Lambda::CapacityProvider" + }, + "PartialCapacityProviderThreeOperatorRole": { + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": [ + "sts:AssumeRole" + ], + "Effect": "Allow", + "Principal": { + "Service": [ + "lambda.amazonaws.com" + ] + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + "arn:aws:iam::aws:policy/AWSLambdaManagedEC2ResourceOperator" + ], + "Tags": [ + { + "Key": "lambda:createdBy", + "Value": "SAM" + } + ] + }, + "Type": "AWS::IAM::Role" } } }