Home > Mobile >  Building Lambda Function in yaml - Issue
Building Lambda Function in yaml - Issue

Time:12-12

I'm building a CloudFormation deployment that includes a Lambda function built out in python 3.9. However, when I build the function, it will not allow me to keep the single quotes. This hasn't been an issue for most of the script as I simply import json and the double quote (") work fine, but one section requires the single quotes.

Here is the code:

import boto3
import json

def lambda_handler(event, context):
    client = client_obj()
    associated = associated_list(client)
    response = client.list_resolver_query_log_configs(
        MaxResults=1,
    )
    config = response['ResolverQueryLogConfigs'][0]['Id']
    ec2 = boto3.client('ec2')
    vpc = ec2.describe_vpcs()
    vpcs = vpc['Vpcs']
 
    for v in vpcs:
        if v['VpcId'] not in associated:
            client.associate_resolver_query_log_config(
                ResolverQueryLogConfigId= f"{config}",
                ResourceId=f"{v['VpcId']}"
            )
        else:
            print(f"{v['VpcId']} is already linked.")
 
def client_obj():
    client = boto3.client('route53resolver')
    return client
 
def associated_list(client_object):
    associated = list()
    assoc = client_object.list_resolver_query_log_config_associations()
    for element in assoc['ResolverQueryLogConfigAssociations']:
        associated.append(element['ResourceId'])
    return associated

any section that includes f"{v['VpcId']}" requires the single quote inside the [] for the script to run properly. Since yaml requires the script to be encapsulated in single quotes for packaging, how can I fix this?

Example in yaml from another script:

CreateIAMUser:
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        ZipFile: !Join
          - | 

          - - import boto3
            - 'import json'
            - 'from botocore.exceptions import ClientError'
            - ''
            - ''
            - 'def lambda_handler(event, context):'
            - '    iam_client = boto3.client("iam")'
            - ''
            - '    account_id = boto3.client("sts").get_caller_identity()["Account"]'
            - ''

I imagine I could re-arrange the script to avoid this, but I would like to use this opportunity to learn something new if possible.

CodePudding user response:

Not sure what you are trying to do, but usually you just use pipe in yaml for that:

      Code:
        ZipFile: |
            import boto3
            import json

            def lambda_handler(event, context):
                client = client_obj()
                associated = associated_list(client)
                response = client.list_resolver_query_log_configs(
                    MaxResults=1,
                )
                config = response['ResolverQueryLogConfigs'][0]['Id']
                ec2 = boto3.client('ec2')
                vpc = ec2.describe_vpcs()
                vpcs = vpc['Vpcs']
             
                for v in vpcs:
                    if v['VpcId'] not in associated:
                        client.associate_resolver_query_log_config(
                            ResolverQueryLogConfigId= f"{config}",
                            ResourceId=f"{v['VpcId']}"
                        )
                    else:
                        print(f"{v['VpcId']} is already linked.")
             
            def client_obj():
                client = boto3.client('route53resolver')
                return client
             
            def associated_list(client_object):
                associated = list()
                assoc = client_object.list_resolver_query_log_config_associations()
                for element in assoc['ResolverQueryLogConfigAssociations']:
                    associated.append(element['ResourceId'])
                return associated
  • Related