【发布时间】: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