【问题标题】:YAML not well formedYAML 格式不正确
【发布时间】:2022-01-01 00:22:44
【问题描述】:

我正在尝试使用 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!")

【问题讨论】:

  • 为什么会被解析为 Yaml?
  • python 是 Lambda 函数用来停止 RDS 集群的内联代码的一部分
  • 我建议您编辑问题标题以包含更多关键字,例如 lambda aws...,这将提高问题的质量和范围。
  • 如果我错了,请纠正我,您正在尝试使用 cloudFormation 创建一个 lambda,它将执行一些 RDS 活动。如果您遇到“YAML”验证问题,请分享 yaml/脚本文件而不是 python 代码。

标签: python amazon-web-services amazon-cloudformation


【解决方案1】:

您很可能会因为 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 不满足 lambda 合同。要使用此 yaml,您需要定义一个名为 lambda_handler 的函数,该函数接受两个参数,这是 lambda 在您触发函数时将导入和执行的内容。
  • 谢谢肖恩。你的回答和我的类似。您是正确的,问题出在 YAML 缩进上。奇怪,我认为它看起来是正确的
猜你喜欢
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2021-08-10
  • 2019-02-22
  • 2020-06-04
  • 2015-08-29
相关资源
最近更新 更多