Debian Codegraph @

ansible_collections/amazon/aws/plugins/modules/lambda_layer_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: Ansible Project
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: lambda_layer_info
10 version_added: 5.5.0
11 short_description: List lambda layer or lambda layer versions
12 description:
13 - This module is used to list the versions of an Lambda layer or all available lambda layers.
14 - The lambda layer versions that have been deleted aren't listed.
15 author: "Aubin Bikouo (@abikouo)"
16 options:
17 name:
18 description:
19 - The name or Amazon Resource Name (ARN) of the Lambda layer.
20 type: str
21 aliases:
22 - layer_name
23 version_number:
24 description:
25 - The Lambda layer version number to retrieve.
26 - Requires O(name) to be provided.
27 type: int
28 aliases:
29 - layer_version
30 version_added: 6.0.0
31 compatible_runtime:
32 description:
33 - A runtime identifier.
34 - Specify this option without O(name) to list only latest layers versions of layers that indicate
35 that they're compatible with that runtime.
36 - Specify this option with O(name) to list only layer versions that indicate that
37 they're compatible with that runtime.
38 type: str
39 compatible_architecture:
40 description:
41 - A compatible instruction set architectures.
42 - Specify this option without O(name) to include only to list only latest layers versions of layers that
43 are compatible with that instruction set architecture.
44 - Specify this option with O(name) to include only layer versions that are compatible with that architecture.
45 type: str
46 extends_documentation_fragment:
47 - amazon.aws.common.modules
48 - amazon.aws.region.modules
49 - amazon.aws.boto3
50 """
51
52 EXAMPLES = r"""
53 ---
54 # Display information about the versions for the layer named blank-java-lib
55 - name: Retrieve layer versions
56 amazon.aws.lambda_layer_info:
57 name: blank-java-lib
58
59 # Display information about the versions for the layer named blank-java-lib compatible with architecture x86_64
60 - name: Retrieve layer versions
61 amazon.aws.lambda_layer_info:
62 name: blank-java-lib
63 compatible_architecture: x86_64
64
65 # list latest versions of available layers
66 - name: list latest versions for all layers
67 amazon.aws.lambda_layer_info:
68
69 # list latest versions of available layers compatible with runtime python3.7
70 - name: list latest versions for all layers
71 amazon.aws.lambda_layer_info:
72 compatible_runtime: python3.7
73
74 # Retrieve specific lambda layer information
75 - name: Get lambda layer version information
76 amazon.aws.lambda_layer_info:
77 name: my-layer
78 version_number: 1
79 """
80
81 RETURN = r"""
82 layers_versions:
83 description:
84 - The layers versions that exists.
85 returned: success
86 type: list
87 elements: dict
88 contains:
89 layer_arn:
90 description: The ARN of the layer.
91 returned: when O(name) is provided
92 type: str
93 sample: "arn:aws:lambda:eu-west-2:123456789012:layer:pylayer"
94 layer_version_arn:
95 description: The ARN of the layer version.
96 returned: if the layer version exists or has been created
97 type: str
98 sample: "arn:aws:lambda:eu-west-2:123456789012:layer:pylayer:2"
99 description:
100 description: The description of the version.
101 returned: always
102 type: str
103 created_date:
104 description: The date that the layer version was created, in ISO-8601 format (YYYY-MM-DDThh:mm:ss.sTZD).
105 returned: if the layer version exists or has been created
106 type: str
107 sample: "2022-09-28T14:27:35.866+0000"
108 version:
109 description: The version number.
110 returned: if the layer version exists or has been created
111 type: int
112 sample: 1
113 compatible_runtimes:
114 description: A list of compatible runtimes.
115 returned: if it was defined for the layer version.
116 type: list
117 sample: ["python3.7"]
118 license_info:
119 description: The layer's software license.
120 returned: if it was defined for the layer version.
121 type: str
122 sample: "GPL-3.0-only"
123 compatible_architectures:
124 description: A list of compatible instruction set architectures.
125 returned: if it was defined for the layer version.
126 type: list
127 content:
128 description: Details about the layer version.
129 returned: if O(version_number) was provided
130 type: complex
131 version_added: 6.0.0
132 contains:
133 location:
134 description: A link to the layer archive in Amazon S3 that is valid for 10 minutes.
135 type: str
136 sample: 'https://awslambda-us-east-2-layers.s3.us-east-2.amazonaws.com/snapshots/123456789012/mylayer-4aaa2fbb-96a?versionId=27iWyA73c...'
137 code_sha256:
138 description: The SHA-256 hash of the layer archive.
139 type: str
140 sample: 'tv9jJO+rPbXUUXuRKi7CwHzKtLDkDRJLB3cC3Z/ouXo='
141 code_size:
142 description: The size of the layer archive in bytes.
143 type: int
144 sample: 169
145 signing_profile_version_arn:
146 description: The Amazon Resource Name (ARN) for a signing profile version.
147 type: str
148 signing_job_arn:
149 description: The Amazon Resource Name (ARN) of a signing job.
150 type: str
151 """
152
153 try:
154 import botocore
155 except ImportError:
156 pass # Handled by AnsibleAWSModule
157
158 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
159
160 from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule
161 from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry
162
163
164 @AWSRetry.jittered_backoff()
165 def _list_layer_versions(client, **params):
166 paginator = client.get_paginator("list_layer_versions")
167 return paginator.paginate(**params).build_full_result()
168
169
170 @AWSRetry.jittered_backoff()
171 def _list_layers(client, **params):
172 paginator = client.get_paginator("list_layers")
173 return paginator.paginate(**params).build_full_result()
174
175
176 class LambdaLayerInfoFailure(Exception):
177 def __init__(self, exc, msg):
178 self.exc = exc
179 self.msg = msg
180 super().__init__(self)
181
182
183 def list_layer_versions(lambda_client, name, compatible_runtime=None, compatible_architecture=None):
184 params = {"LayerName": name}
185 if compatible_runtime:
186 params["CompatibleRuntime"] = compatible_runtime
187 if compatible_architecture:
188 params["CompatibleArchitecture"] = compatible_architecture
189 try:
190 layer_versions = _list_layer_versions(lambda_client, **params)["LayerVersions"]
191 return [camel_dict_to_snake_dict(layer) for layer in layer_versions]
192 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
193 raise LambdaLayerInfoFailure(exc=e, msg=f"Unable to list layer versions for name {name}")
194
195
196 def list_layers(lambda_client, compatible_runtime=None, compatible_architecture=None):
197 params = {}
198 if compatible_runtime:
199 params["CompatibleRuntime"] = compatible_runtime
200 if compatible_architecture:
201 params["CompatibleArchitecture"] = compatible_architecture
202 try:
203 layers = _list_layers(lambda_client, **params)["Layers"]
204 layer_versions = []
205 for item in layers:
206 layer = {key: value for key, value in item.items() if key != "LatestMatchingVersion"}
207 layer.update(item.get("LatestMatchingVersion"))
208 layer_versions.append(camel_dict_to_snake_dict(layer))
209 return layer_versions
210 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
211 raise LambdaLayerInfoFailure(exc=e, msg=f"Unable to list layers {params}")
212
213
214 def get_layer_version(lambda_client, layer_name, version_number):
215 try:
216 layer_version = lambda_client.get_layer_version(LayerName=layer_name, VersionNumber=version_number)
217 if layer_version:
218 layer_version.pop("ResponseMetadata")
219 return [camel_dict_to_snake_dict(layer_version)]
220 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
221 raise LambdaLayerInfoFailure(exc=e, msg="get_layer_version() failed.")
222
223
224 def execute_module(module, lambda_client):
225 name = module.params.get("name")
226 version_number = module.params.get("version_number")
227
228 try:
229 if name is not None and version_number is not None:
230 result = get_layer_version(lambda_client, name, version_number)
231 else:
232 params = {}
233 f_operation = list_layers
234 if name is not None:
235 f_operation = list_layer_versions
236 params["name"] = name
237 compatible_runtime = module.params.get("compatible_runtime")
238 if compatible_runtime is not None:
239 params["compatible_runtime"] = compatible_runtime
240 compatible_architecture = module.params.get("compatible_architecture")
241 if compatible_architecture is not None:
242 params["compatible_architecture"] = compatible_architecture
243 result = f_operation(lambda_client, **params)
244
245 module.exit_json(changed=False, layers_versions=result)
246 except LambdaLayerInfoFailure as e:
247 module.fail_json_aws(exception=e.exc, msg=e.msg)
248
249
250 def main():
251 argument_spec = dict(
252 name=dict(type="str", aliases=["layer_name"]),
253 compatible_runtime=dict(type="str"),
254 compatible_architecture=dict(type="str"),
255 version_number=dict(type="int", aliases=["layer_version"]),
256 )
257
258 module = AnsibleAWSModule(
259 argument_spec=argument_spec, supports_check_mode=True, required_by=dict(version_number=("name",))
260 )
261
262 lambda_client = module.client("lambda")
263 execute_module(module, lambda_client)
264
265
266 if __name__ == "__main__":
267 main()