forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZipPackageRequiredProperties.py
79 lines (66 loc) · 2.78 KB
/
ZipPackageRequiredProperties.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from cfnlint._typing import Path, RuleMatches
from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.template import Template
class ZipPackageRequiredProperties(CloudFormationLintRule):
id = "W2533"
shortdesc = (
"Check required properties for Lambda if the deployment package is a .zip file"
)
description = (
"When the package type is Zip, "
"you must also specify the `handler` and `runtime` properties."
)
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html"
tags = ["resources", "lambda"]
def match(self, cfn: Template) -> RuleMatches:
matches = []
required_properties = [
"Handler",
"Runtime",
] # required if package is a .zip file
resources = cfn.get_resources(["AWS::Lambda::Function"])
for resource_name, resource in resources.items():
properties = resource.get("Properties")
if not isinstance(properties, dict):
continue
for scenario in cfn.get_object_without_conditions(
properties, ["PackageType", "Code", "Handler", "Runtime"]
):
props = scenario.get("Object")
path: Path = ["Resources", resource_name, "Properties"]
# check is zip deployment
is_zip_deployment = True
code = props.get("Code")
if props.get("PackageType") == "Zip":
path.append("PackageType")
elif isinstance(code, dict) and (
code.get("ZipFile") or code.get("S3Key")
):
path.append("Code")
else:
is_zip_deployment = False
if not is_zip_deployment:
continue
# check required properties for zip deployment
missing_properties = []
for p in required_properties:
if props.get(p) is None:
missing_properties.append(p)
if len(missing_properties) > 0:
message = "Properties {0} missing for zip file deployment at {1}"
matches.append(
RuleMatch(
path,
message.format(
missing_properties,
"/".join(
map(str, ["Resources", resource_name, "Properties"])
),
),
)
)
return matches