簡體   English   中英

使用 AWS CloudFormation 參考創建 yaml

[英]create yaml with AWS CloudFormation references

我需要一個 python 代碼來創建下面的 yaml 代碼。

    Tags:
    - Key: key1
      Value: !Ref 'AWS::StackName'
    - Key: Key2
      Value: !Ref 'AWS::StackId'

這是我所擁有的,但不能解決問題。

def generate_resource(ami, source_data):
    resource = {
         "Type": "AWS::EC2::Instance",
         "Properties": {
             "ImageId": ami["ImageId"],
             "InstanceType": ami["InstanceType"],
             "PrivateIpAddress": ami["PrivateIpAddress"],
             "KeyName": ami["KeyName"]
             "SubnetId": { "Ref": "SubnetId" },
             "SecurityGroupIds": { "Ref":  "SecurityGroupId" }, 
             "Tags": [
                 { "Key": "key1", "Value": "{!Ref 'AWS::StackName'}"},
                 { "Key": "key2", "Value": "{!Ref 'AWS::StackId'}"}
             ]
         }
     }

此代碼的yaml輸出格式不正確,因此它只是復制{!Ref 'AWS::StackName'}作為值。

import os, sys
import lib.aws as aws, lib.cft as cft, lib.inventory as inventory 

BUCKET_NAME = 'testbucket'


def generate_cft(commit_hash, file_dict, dry_run):
    return (
        "# Autogenerated CFT for commit hash " + commit_hash + "\n" + 
        cft.generate(inventory.read(file_dict["path"]))
    )


def upload_cft(commit_hash, file_dict, cft_text):
    target_key = commit_hash + "/" + file_dict["name"].split("_")[0] +    ".yaml"

    aws.upload(BUCKET_NAME, target_key, cft_text)


def show_cft(file_dict, cft_text):
    print(file_dict["path"] + " generates the following cft:")
    print("")
    print(cft_text)
    print("")


def generate_and_upload(commit_hash, file_dict, dry_run):
    cft_text = generate_cft(commit_hash, file_dict, dry_run)

    aws.validate_cft(cft_text)

    if dry_run: 
        show_cft(file_dict, cft_text)
    else:
        upload_cft(commit_hash, file_dict, cft_text)


def generate_and_upload_all(commit_hash, dry_run):
    for file_dict in inventory.list():
        print("generating cft for " + file_dict["path"])
        generate_and_upload(commit_hash, file_dict, dry_run)


if __name__ == "__main__":
    if not os.getcwd().endswith("ci"):
        print("Please run this script from the ci directory")
        exit()

    commit_hash = sys.argv[1] if len(sys.argv) >= 2 else "test"
    generate_and_upload_all(commit_hash, False)

是的!!!! 我玩弄了代碼並弄清楚了。 這是 CFT 創建期間的語法錯誤。 現在,當我使用生成的 CFT 創建堆棧時,標簽值將替換為實際的“StackId”。感謝大家的幫助和指導。

def generate_resource(ami, source_data):
resource = {
    "Type": "AWS::EC2::Instance",
    "Properties": {
        "ImageId": ami["ImageId"],
        "InstanceType": ami["InstanceType"],
        "PrivateIpAddress": ami["PrivateIpAddress"],
        "KeyName": ami["KeyName"],
        "SubnetId": { "Ref": "SubnetId" },
        "SecurityGroupIds": { "Ref":  "SecurityGroupId" }, 
        "Tags": [
            { "Key": "Name", "Value": ami["Name"] },
            { "Key": "BootUpDependsOn", "Value": ami["BootUpDependsOn"]},
            { "Key": "WaitTimeAfterBootUp", "Value": ami["WaitTimeAfterBootUp"]},
            { "Key": "Key1", "Value": { "Ref": "AWS::StackName" }},
            { "Key": "Key2", "Value": { "Ref": "AWS::StackId" }}
        ]
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM