我刚刚进入 EC2、CloudFormation(和对流层)等。我试图从一个简单的 Selenium Grid 开始,以便在夜间运行。现在,我们在需要时使用了 12 个 selenium 节点(每个节点都有自己的 EC2 实例)。这些堆栈一次只能上升几个小时。将来我们很可能需要更多,所以我没有静态设置节点数,而是尝试设置它,以便 Jenkins 可以动态增加节点数。
现在,我有一个简单的 for 循环,看起来应该可以正常工作 - 特别是在查看了一堆示例之后:
for i in range(numNodes):
instance = ec2.Instance("Node{}".format(str(i)))
instance.ImageId = Ref(Image)
instance.UserData = Base64(Join("", userData))
instance.InstanceType = Ref(NodeSize)
instance.KeyName = Ref(SSHKey)
instance.SecurityGroups = [Ref("NodeSecurityGroup")]
instance.IamInstanceProfile = "SeleniumNode"
template.add_resource(instance)
完整的堆栈跟踪:
Traceback (most recent call last):
File "C:/dev/source/admin/scripts/troposphere/seleniumGrid.py", line 171, in <module>
print(template.to_json())
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\site-packages\troposphere\__init__.py", line 543, in to_json
sort_keys=sort_keys, separators=separators)
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 237, in dumps
**kw).encode(obj)
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\encoder.py", line 200, in encode
chunks = list(chunks)
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\encoder.py", line 429, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\encoder.py", line 403, in _iterencode_dict
yield from chunks
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\encoder.py", line 403, in _iterencode_dict
yield from chunks
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\json\encoder.py", line 436, in _iterencode
o = _default(o)
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\site-packages\troposphere\__init__.py", line 440, in default
return obj.JSONrepr()
File "C:\Users\Sathed\AppData\Local\Programs\Python\Python35-32\lib\site-packages\troposphere\__init__.py", line 223, in JSONrepr
"Resource %s required in type %s" % (k, rtype))
ValueError: Resource ImageId required in type AWS::EC2::Instance
我的图像参数如下所示:
Image = template.add_parameter(Parameter(
"Image",
Type="AWS::EC2::Image::Id", # I even tried setting this to "String"
Description="AMI To use for all windows grid instances.",
Default="ami-c06b24a0"
))
我什至尝试将所有内容都传递给构造函数。
for i in range(numNodes):
instance = ec2.Instance("Node{}".format(str(i)),
ImageId=Ref(Image),
UserData=Base64(Join("", userData)),
InstanceType=Ref(NodeSize),
KeyName=Ref(SSHKey),
SecurityGroups=[Ref("NodeSecurityGroup")],
DependsOn=["NodeSecurityGroup", "WindowsHub"],
IamInstanceProfile="SeleniumNode")
template.add_resource(instance)
但我仍然遇到同样的错误。我敢肯定这很愚蠢,但它变得非常烦人。有什么想法吗?
此外,当我尝试打印 JSON 模板时出现错误。
print(template.to_json())
对流层 1.8.2
Python 3.5.2