gpt4 book ai didi

python - YAML 格式不正确

转载 作者:行者123 更新时间:2023-12-03 07:46:57 25 4
gpt4 key购买 nike

我正在尝试使用 Lambda 停止我的 RDS 集群,并使用下面所示的 python 代码来定位集群标签,然而,当尝试创建 CloudFormation 堆栈时,AWS 告诉我 YAML 格式不正确。

例如,它表明 Key = 'True' 的格式不正确。如果我删除那段代码,它就会告诉我下一段代码,client = boto3.client('rds') 的格式也不好。

我已将此代码放入在线 python 验证器中,它没有报告任何问题。有人能帮忙吗?谢谢


Tag = 'AutoPower'
Key = 'True'


client = boto3.client('rds')
response = client.describe_db_cluster()

for resp in response['DBCluster']:
db_cluster_arn = resp['DBClusterArn']

response = client.list_tags_for_resource(ResourceName=db_cluster_arn)
for tags in response['TagList']:
if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
status = resp['DBClusterStatus']
ClusterID = resp['DBClusterIdentifier']
print(InstanceID)

if status == 'available':
print("shutting down %s " % ClusterID)
client.stop_db_cluster(DBClusterIdentifier=ClusterID)

# print ("Do something with it : %s" % db_instance_arn)
elif status == 'stopped':
print("starting up %s " % ClusterID)
client.start_db_cluster(DBClusterIdentifier=ClusterID)
else:
print("The database is " + status + " status!")

最佳答案

您很可能会因 CloudFormation YAML 和内联函数代码之间的缩进问题而遇到问题。以下是 CloudFormation YAML 的示例,它将使用内联代码来创建函数:

Resources:
YourFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: |
Tag = 'AutoPower'
Key = 'True'

client = boto3.client('rds')
response = client.describe_db_cluster()

for resp in response['DBCluster']:
db_cluster_arn = resp['DBClusterArn']

response = client.list_tags_for_resource(ResourceName=db_cluster_arn)
for tags in response['TagList']:
if tags['Key'] == str(Key) and tags['Value'] == str(Tag):
status = resp['DBClusterStatus']
ClusterID = resp['DBClusterIdentifier']
print(InstanceID)

if status == 'available':
print("shutting down %s " % ClusterID)
client.stop_db_cluster(DBClusterIdentifier=ClusterID)

# print ("Do something with it : %s" % db_instance_arn)
elif status == 'stopped':
print("starting up %s " % ClusterID)
client.start_db_cluster(DBClusterIdentifier=ClusterID)
else:
print("The database is " + status + " status!")

Handler: index.lambda_handler
Role: !Sub "arn:aws:iam::${AWS::AccountId}:role/YourRoleNameHere"
Runtime: python3.9
Description: This is an example of a function in CloudFormation
FunctionName: Your_Function_Name
MemorySize: 128
Timeout: 180

关于python - YAML 格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70542947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com