
    VhW                         d Z dZdZddlZddlZ	 ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ  ej                         d	        Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r Y _w xY w)a^  
---
module: lambda_info
version_added: 5.0.0
short_description: Gathers AWS Lambda function details
description:
  - Gathers various details related to Lambda functions, including aliases, versions and event source mappings.
  - Use module M(amazon.aws.lambda) to manage the lambda function itself, M(amazon.aws.lambda_alias) to manage function aliases,
    M(amazon.aws.lambda_event) to manage lambda event source mappings, and M(amazon.aws.lambda_policy) to manage policy statements.
  - This module was originally added to C(community.aws) in release 1.0.0.
options:
  query:
    description:
      - Specifies the resource type for which to gather information.
      - Defaults to V(all) when O(function_name) is specified.
      - Defaults to V(config) when O(function_name) is NOT specified.
    choices: [ "aliases", "all", "config", "mappings", "policy", "versions", "tags" ]
    type: str
  function_name:
    description:
      - The name of the lambda function for which information is requested.
    aliases: [ "function", "name"]
    type: str
  event_source_arn:
    description:
      - When O(query=mappings), this is the Amazon Resource Name (ARN) of the Amazon Kinesis or DynamoDB stream.
    type: str
author:
  - Pierre Jodouin (@pjodouin)
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.boto3
a  
---
# Simple example of listing all info for a function
- name: List all for a specific function
  amazon.aws.lambda_info:
    query: all
    function_name: myFunction
  register: my_function_details

# Get basic information about AWS Lambda Functions in the current region
- name: List Lambda Functions
  amazon.aws.lambda_info:
  register: lambda_functions

# List all versions of a function
- name: List function versions
  amazon.aws.lambda_info:
    query: versions
    function_name: myFunction
  register: my_function_versions

# List all info for all functions
- name: List all functions
  amazon.aws.lambda_info:
    query: all
  register: output

- name: show Lambda information
  ansible.builtin.debug:
    msg: "{{ output['function'] }}"
a&  
---
function:
    description:
        - Lambda function list.
        - RV(function) has been deprecated in will be removed in the next major release after 2025-01-01.
    returned: success
    type: dict
function.TheName:
    description:
        - Lambda function information, including event, mapping, and version information.
        - RV(function) has been deprecated in will be removed in the next major release after 2025-01-01.
    returned: success
    type: dict
functions:
    description: List of information for each lambda function matching the query.
    returned: always
    type: list
    elements: dict
    version_added: 4.1.0
    version_added_collection: community.aws
    contains:
        aliases:
            description: The aliases associated with the function.
            returned: when O(query=aliases) or O(query=all)
            type: list
            elements: str
        architectures:
            description: The architectures supported by the function.
            returned: success
            type: list
            elements: str
            sample: ['arm64']
        code_sha256:
            description: The SHA256 hash of the function's deployment package.
            returned: success
            type: str
            sample: 'zOAGfF5JLFuzZoSNirUtOrQp+S341IOA3BcoXXoaIaU='
        code_size:
            description: The size of the function's deployment package in bytes.
            returned: success
            type: int
            sample: 123
        dead_letter_config:
            description: The function's dead letter queue.
            returned: when the function has a dead letter queue configured
            type: dict
            sample: { 'target_arn': arn:aws:lambda:us-east-1:123456789012:function:myFunction:1 }
            contains:
                target_arn:
                    description: The ARN of an SQS queue or SNS topic.
                    returned: when the function has a dead letter queue configured
                    type: str
                    sample: arn:aws:lambda:us-east-1:123456789012:function:myFunction:1
        description:
            description: The function's description.
            returned: success
            type: str
            sample: 'My function'
        ephermal_storage:
            description: The size of the function /tmp directory in MB.
            type: dict
            contains:
                size:
                    description: The size of the function /tmp directory.
                    type: int
                    returned: always
                    sample: 512
        environment:
            description: The function's environment variables.
            returned: when environment variables exist
            type: dict
            contains:
                variables:
                    description: Environment variable key-value pairs.
                    returned: when environment variables exist
                    type: dict
                    sample: {'key': 'value'}
                error:
                    description: Error message for environment variables that could not be applied.
                    returned: when there is an error applying environment variables
                    type: dict
                    contains:
                        error_code:
                            description: The error code.
                            returned: when there is an error applying environment variables
                            type: str
                        message:
                            description: The error message.
                            returned: when there is an error applying environment variables
                            type: str
        function_arn:
            description: The function's Amazon Resource Name (ARN).
            returned: on success
            type: str
            sample: 'arn:aws:lambda:us-east-1:123456789012:function:myFunction:1'
        function_name:
            description: The function's name.
            returned: on success
            type: str
            sample: 'myFunction'
        handler:
            description: The function Lambda calls to begin executing your function.
            returned: on success
            type: str
            sample: 'index.handler'
        last_modified:
            description: The date and time that the function was last updated, in ISO-8601 format (YYYY-MM-DDThh:mm:ssTZD).
            returned: on success
            type: str
            sample: '2017-08-01T00:00:00.000+0000'
        mappings:
            description: List of configuration information for each event source mapping.
            returned: when O(query=all) or O(query=mappings)
            type: list
            elements: dict
            contains:
                uuid:
                    description: The AWS Lambda assigned opaque identifier for the mapping.
                    returned: on success
                    type: str
                batch_size:
                    description: The largest number of records that AWS Lambda will retrieve from the event source at the time of invoking the function.
                    returned: on success
                    type: int
                event_source_arn:
                    description: The ARN of the Amazon Kinesis or DyanmoDB stream that is the source of events.
                    returned: on success
                    type: str
                function_arn:
                    description: The Lambda function to invoke when AWS Lambda detects an event on the poll-based source.
                    returned: on success
                    type: str
                last_modified:
                    description: The UTC time string indicating the last time the event mapping was updated.
                    returned: on success
                    type: str
                last_processing_result:
                    description: The result of the last AWS Lambda invocation of your Lambda function.
                    returned: on success
                    type: str
                state:
                    description: The state of the event source mapping.
                    returned: on success
                    type: str
                state_transition_reason:
                    description: The reason the event source mapping is in its current state.
                    returned: on success
                    type: str
        memory_size:
            description: The memory allocated to the function.
            returned: on success
            type: int
            sample: 128
        package_type:
            description: The type of deployment package.
            type: str
            returned: always
            sample: "Zip"
        policy:
            description: The policy associated with the function.
            returned: when O(query=all) or O(query=policy)
            type: dict
        revision_id:
            description: The latest updated revision of the function or alias.
            returned: on success
            type: str
            sample: 'a2x9886d-d48a-4a0c-ab64-82abc005x80c'
        role:
            description: The function's execution role.
            returned: on success
            type: str
            sample: 'arn:aws:iam::123456789012:role/lambda_basic_execution'
        runtime:
            description: The funtime environment for the Lambda function.
            returned: on success
            type: str
            sample: 'nodejs6.10'
        runtime_version_config:
            description: The ARN of the runtime and any errors that occured.
            type: dict
            contains:
                runtime_version_arn:
                    description: The ARN of the runtime version you want the function to use.
                    type: str
                    returned: always
                    sample: "arn:aws:lambda:us-west-2::runtime:123456789012"
        snap_start:
            description: Snapshot of the initialized execution environment when you publish a function version.
            type: dict
            contains:
                apply_on:
                    description: When set to V(PublishedVersions), Lambda creates a snapshot of the execution environment when you publish a function version.
                    type: str
                    sample: "None"
                    returned: always
                optimization_status:
                    description: This element indicates whether SnapStart is activated for the specified function version.
                    type: str
                    returned: always
                    sample: "Off"
        tracing_config:
            description: The function's AWS X-Ray tracing configuration.
            returned: on success
            type: dict
            sample: { 'mode': 'Active' }
            contains:
                mode:
                    description: The tracing mode.
                    returned: on success
                    type: str
                    sample: 'Active'
        timeout:
            description: The amount of time that Lambda allows a function to run before terminating it.
            returned: on success
            type: int
            sample: 3
        version:
            description: The version of the Lambda function.
            returned: on success
            type: str
            sample: '1'
        versions:
            description: List of Lambda function versions.
            returned: when O(query=all) or O(query=versions)
            type: list
            elements: dict
        vpc_config:
            description: The function's networking configuration.
            returned: on success
            type: dict
            sample: {
            'security_group_ids': [],
            'subnet_ids': [],
            'vpc_id': '123'
            }
    N)camel_dict_to_snake_dict)is_boto3_error_code)AnsibleAWSModule)AWSRetryc                 d    | j                  |      } |j                  di |j                         S )N )get_paginatorpaginatebuild_full_result)clientfunctionparams	paginators       j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/lambda_info.py	_paginater   G  s1    $$X.I9''99;;    c                    t               }	 |j                  t        | d|      d          t        |      S # t        d      $ r |j                  g        Y t        |      S t        j
                  j                  t        j
                  j                  f$ r'}|j                  |d       Y d}~t        |      S d}~ww xY w)	z
    Returns list of aliases for a specified function.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    list_aliasesFunctionNameAliases)aliasesResourceNotFoundExceptionzTrying to get aliasesmsgN
dictupdater   r   botocore
exceptionsClientErrorBotoCoreErrorfail_json_awsr   r   modulefunction_namelambda_infoes        r   alias_detailsr)   M  s     &K=9V^R_#`aj#kl $K00 :; '2& $K00 	'')) = 	Q$;<<#K00=s   !8  C$6CB<<Cc                     | r| S |ryy)Nallconfigr   )queryr&   s     r   
_get_queryr.   f  s     r   c                    |j                   j                  d      }|r|g}n!t        | d      d   }|D cg c]  }|d   	 }}t        |j                   d   |      }g }i }|D ]  }i }	|	j	                  t        | ||             |dv r|	j	                  t        | ||             |dv r|	j	                  t        | ||             |dv r|	j	                  t        | ||             |d	v r|	j	                  t        | ||             |d
v r|	j	                  t        | ||             |	||	d   <   |j                  |	        |j                  ||d       yc c}w )z
    Returns queried facts for a specified function (or all functions).

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    r&   list_functions	Functionsr   r-   )r+   r   )r+   policy)r+   versions)r+   mappings)r+   tagsF)r   	functionschangedN)r   getr   r.   r   config_detailsr)   policy_detailsversion_detailsmapping_detailstags_detailsappend	exit_json)
r   r%   r&   function_namesall_function_infofunction_infor-   r6   	all_factsr   s
             r   r0   r0   q  sm    MM%%o6M' &f.>?LM^_M-7__v}}W-}=EI I' # 	vv}EF&&OOM&&-HI%%OON66=IJ''OOOFFMJK''OOOFFMJKO#OOLGH/7	(?+, 	"3#8 i9eLI `s   Ec                    t               }	 |j                  | j                  d|             d	|v r$d
|d	   v r|d	   d
   }t        |      }||d   d<   |S t        |      }|S # t        d      $ r |j                  i        Y Wt        j
                  j                  t        j
                  j                  f$ r!}|j                  |d| d       Y d}~d}~ww xY w)z
    Returns configuration details for a lambda function.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    T	aws_retryr   r   r   Trying to get z configurationr   NEnvironment	Variablesenvironment	variables)
r   r   get_function_configurationr   r   r    r!   r"   r#   r   )r   r%   r&   r'   r(   env_varssnaked_lambda_infos          r   r9   r9     s     &KT6<<tZg<hi #{=7Q(Q}-k:5kB9A=)+6  6kB :; (B''')) T 	Qn]O>$RSS	Ts   "A# # C6C;CCc                    t               }t               }||d<   |j                  j                  d      r|j                  j                  d      |d<   	 |j                  t	        | dfi |d          t        |      S # t        d      $ r |j                  g        Y t        |      S t        j                  j                  t        j                  j                  f$ r'}|j                  |d	       Y d
}~t        |      S d
}~ww xY w)z
    Returns all lambda event source mappings.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    r   event_source_arnEventSourceArnlist_event_source_mappingsEventSourceMappings)r4   r   z#Trying to get source event mappingsr   N)r   r   r8   r   r   r   r   r    r!   r"   r#   r   )r   r%   r&   r'   r   r(   s         r   r<   r<     s    &KVF*F>}}+,#)==#4#45G#H KIf6R$]V\$]^s$tu $K00 :; (B' $K00 	'')) K 	Q$IJJ#K00Ks    A? ? D+6D!DDc                    t               }	 |j                  t        j                  | j	                  d|      d                t        |      S # t        d      $ r |j                  i        Y t        |      S t        j                  j                  t        j                  j                  f$ r+}|j                  |d| d       Y d	}~t        |      S d	}~ww xY w)
z
    Returns policy attached to a lambda function.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    TrE   Policy)r2   r   rH   z policyr   N)r   r   jsonloads
get_policyr   r   r    r!   r"   r#   r   r$   s        r   r:   r:     s     &K	M$**V->->\i->-jks-t"uv $K00 :; &"% $K00 	'')) M 	Qn]O7$KLL#K00Ms   9A  C<6C2CCc                    t               }	 |j                  t        | d|      d          t        |      S # t        d      $ r |j                  g        Y t        |      S t        j
                  j                  t        j
                  j                  f$ r+}|j                  |d| d       Y d	}~t        |      S d	}~ww xY w)
z
    Returns all lambda function versions.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    list_versions_by_functionr   Versions)r3   r   rH   z	 versionsr   Nr   r$   s        r   r;   r;     s     &K
Ov'BQ^_`jk 	 	
 $K00 :; (B' $K00 	'')) O 	Qn]O9$MNN#K00Os   !8  C$6CC  Cc                    t               }	 |j                  | j                  d|      j                  di              t        |      S # t	        d      $ r |j                  i        Y t        |      S t
        j                  j                  t
        j                  j                  f$ r+}|j                  |d| d	       Y d
}~t        |      S d
}~ww xY w)z
    Returns tag details for a lambda function.

    :param client: AWS API client reference (boto3)
    :param module: Ansible module reference
    :param function_name (str): Name of Lambda function to query
    :return dict:
    TrE   Tags)r5   r   rG   rH   z tagsr   N)r   r   get_functionr8   r   r   r    r!   r"   r#   r   r$   s        r   r=   r=     s     &KK 3 3dQ^ 3 _ c cdjln op $K00 :; (B' $K00 	'')) K 	Qn]O5$IJJ#K00Ks   3A
 
 C66C,CCc                     t        t        ddddg      t        dg dd      t        dd      	      } t        | d
g g       }|j                  d   }|rPt        j                  d|      s|j                  d| d       t        |      dkD  r|j                  d| d       |j                  dt        j                               }|j                  ddd       t        ||       y)z<
    Main entry point.

    :return dict: ansible facts
    FNr   name)requireddefaultr   )r   r+   r,   r4   r2   r3   r5   )rb   choicesrc   )rb   rc   )r&   r-   rQ   T)argument_specsupports_check_modemutually_exclusiverequired_togetherr&   z
^[\w\-:]+$zFunction name zI is invalid. Names must contain only alphanumeric characters and hyphens.r   @   zFunction name "z" exceeds 64 character limitlambda)retry_decoratorzThe returned key 'function', which returned a dictionary of dictionaries, is deprecated and will be replaced by 'functions', which returns a list of dictionaries. Both keys are returned for now.z
2025-01-01z
amazon.aws)datecollection_name)r   r   r   research	fail_jsonlenr   r   jittered_backoff	deprecater0   )re   r%   r&   r   s       r   mainrt   5  s    E4*fAUV$jtx
 ud;M #RThjF
 MM/2Myy6$]O3|}   }"?=/A]!^_]]8X5N5N5P]QF n $   66"r   __main__)DOCUMENTATIONEXAMPLESRETURNrW   rn   r   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   ;ansible_collections.amazon.aws.plugins.module_utils.retriesr   rr   r   r)   r.   r0   r9   r<   r:   r;   r=   rt   __name__r   r   r   <module>r      s   !F@l
\  		 V \ X P < <
124Mn@1>141612(#V zF G	  		s   A, ,A43A4