亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 創建 YAML:Cloudformation 模板

使用 Python 創建 YAML:Cloudformation 模板

茅侃侃 2023-07-27 14:05:25
您好,我正在嘗試使用 Python 創建 Cloudformation 模板。我正在使用yaml圖書館來做到這一點。這是我的代碼:import yamldict_file =     {    "AWSTemplateFormatVersion": "2010-09-09",    "Description": "ding dong",    "Parameters": {        "Environment":{            "Description": "Environment for Deployment",            "Type": "String"        }    },    "Resources":{        "Queue": {            "Type": "AWS::SQS::Queue",            "Properties":{                "DelaySeconds": 0,                "MaximumMessageSize": 262144,                "MessageRetentionPeriod": 1209600,                "QueueName": '!Sub "${Environment}-Queue"',                "ReceiveMessageWaitTimeSeconds": 0,                "VisibilityTimeout": 150            }        }    }}with open(r'TopicName.yml', 'w') as file:    documents = yaml.dump(dict_file, file, sort_keys=False)問題出在 Cloudformation 標簽上,就像!Sub您在 key 中看到的那樣"QueueName"。需要!Sub位于結果 yaml 的引號之外。給出的結果 yaml 看起來像這樣QueueName: '!Sub "${LSQRegion}-TelephonyLogCall-Distributor"'我該如何解決?任何想法?請幫忙!!
查看完整描述

2 回答

?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

在 YAML 中,以 開頭的不帶引號的值!表示自定義類型。您永遠無法yaml.dump使用簡單的字符串值生成它。您將需要創建一個自定義類和一個關聯的表示器才能獲得您想要的輸出。例如:

import yaml



class Sub(object):

? ? def __init__(self, content):

? ? ? ? self.content = content


? ? @classmethod

? ? def representer(cls, dumper, data):

? ? ? ? return dumper.represent_scalar('!Sub', data.content)



dict_file = {

? ? "AWSTemplateFormatVersion": "2010-09-09",

? ? "Description": "ding dong",

? ? "Parameters": {

? ? ? ? "Environment": {

? ? ? ? ? ? "Description": "Environment for Deployment",

? ? ? ? ? ? "Type": "String"

? ? ? ? }

? ? },

? ? "Resources": {

? ? ? ? "Queue": {

? ? ? ? ? ? "Type": "AWS::SQS::Queue",

? ? ? ? ? ? "Properties": {

? ? ? ? ? ? ? ? "DelaySeconds": 0,

? ? ? ? ? ? ? ? "MaximumMessageSize": 262144,

? ? ? ? ? ? ? ? "MessageRetentionPeriod": 1209600,

? ? ? ? ? ? ? ? "QueueName": Sub("${Environment}-Queue"),

? ? ? ? ? ? ? ? "ReceiveMessageWaitTimeSeconds": 0,

? ? ? ? ? ? ? ? "VisibilityTimeout": 150,

? ? ? ? ? ? },

? ? ? ? }

? ? },

}



yaml.add_representer(Sub, Sub.representer)

print(yaml.dump(dict_file))

這將輸出:


AWSTemplateFormatVersion: '2010-09-09'

Description: ding dong

Parameters:

? Environment:

? ? Description: Environment for Deployment

? ? Type: String

Resources:

? Queue:

? ? Properties:

? ? ? DelaySeconds: 0

? ? ? MaximumMessageSize: 262144

? ? ? MessageRetentionPeriod: 1209600

? ? ? QueueName: !Sub '${Environment}-Queue'

? ? ? ReceiveMessageWaitTimeSeconds: 0

? ? ? VisibilityTimeout: 150

? ? Type: AWS::SQS::Queue


查看完整回答
反對 回復 2023-07-27
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您也可以嘗試對流層庫。它支持所有 AWS 服務(由 AWS CloudFormation 支持),并且在 Python 中創建 CloudFormation 模板確實更加 Pythonic。

我已為您的 CloudFormation 模板粘貼了對流層代碼。你也可以嘗試一下:

from troposphere import Template, Parameter, Sub

from troposphere.sqs import Queue



def get_cfn_template():

? ? template = Template()

? ? template.set_version("2010-09-09")

? ? template.set_description("ding dong")

? ? template.add_parameter(Parameter(

? ? ? ? "Environment",

? ? ? ? Type="String",

? ? ? ? Description="Environment for Deployment"

? ? ))

? ? template.add_resource(

? ? ? ? Queue(

? ? ? ? ? ? 'Queue',

? ? ? ? ? ? DelaySeconds=0,

? ? ? ? ? ? MaximumMessageSize=262144,

? ? ? ? ? ? MessageRetentionPeriod=1209600,

? ? ? ? ? ? QueueName=Sub("${Environment}-Queue"),

? ? ? ? ? ? ReceiveMessageWaitTimeSeconds=0,

? ? ? ? ? ? VisibilityTimeout=150

? ? ? ? )

? ? )

? ? return template.to_json()



print get_cfn_template()

輸出


{

? ? "AWSTemplateFormatVersion": "2010-09-09",

? ? "Description": "ding dong",

? ? "Parameters": {

? ? ? ? "Environment": {

? ? ? ? ? ? "Description": "Environment for Deployment",

? ? ? ? ? ? "Type": "String"

? ? ? ? }

? ? },

? ? "Resources": {

? ? ? ? "Queue": {

? ? ? ? ? ? "Properties": {

? ? ? ? ? ? ? ? "DelaySeconds": 0,

? ? ? ? ? ? ? ? "MaximumMessageSize": 262144,

? ? ? ? ? ? ? ? "MessageRetentionPeriod": 1209600,

? ? ? ? ? ? ? ? "QueueName": {

? ? ? ? ? ? ? ? ? ? "Fn::Sub": "${Environment}-Queue"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? "ReceiveMessageWaitTimeSeconds": 0,

? ? ? ? ? ? ? ? "VisibilityTimeout": 150

? ? ? ? ? ? },

? ? ? ? ? ? "Type": "AWS::SQS::Queue"

? ? ? ? }

? ? }

}

Troposphere 也可以將您的代碼轉換為 YAML。


查看完整回答
反對 回復 2023-07-27
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號