【问题标题】:Building Lambda Function in yaml - Issue在 yaml 中构建 Lambda 函数 - 问题
【发布时间】:2022-01-15 13:49:02
【问题描述】:

我正在构建一个 CloudFormation 部署,其中包含一个在 python 3.9 中构建的 Lambda 函数。但是,当我构建函数时,它不允许我保留单引号。这对于大多数脚本来说都不是问题,因为我只是导入 json 并且双引号 (") 可以正常工作,但有一部分需要单引号。

代码如下:

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

任何包含f"{v['VpcId']}" 的部分都需要在 [] 中加上单引号,脚本才能正常运行。由于yaml需要将脚本封装在单引号中进行打包,请问如何解决?

来自另一个脚本的 yaml 示例:

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"]'
            - ''

我想我可以重新安排脚本来避免这种情况,但如果可能的话,我想利用这个机会学习新的东西。

【问题讨论】:

    标签: python amazon-web-services aws-lambda yaml amazon-cloudformation


    【解决方案1】:

    不确定您要做什么,但通常您只需在 yaml 中使用 pipe 即可:

          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
    

    【讨论】:

    • 如此简单。我不知道我为什么要使用 !Join 功能,这是我被教导的方式。这效果好多了,现在我感到很尴尬。谢谢!
    猜你喜欢
    • 2014-11-23
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多