如何解决使用AWS CloudFormation引用创建yaml
我需要一个可以在下面创建yaml代码的python代码。
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,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,dry_run):
cft_text = generate_cft(commit_hash,dry_run)
aws.validate_cft(cft_text)
if dry_run:
show_cft(file_dict,cft_text)
else:
upload_cft(commit_hash,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,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" }}
]
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。