[英]Accessing RDS Instance subnet for Glue Connection using AWS CDK Python
[英]Multiple Instance AWS CDK python
如何使用 Python 通過 AWS CDK 啟動多個實例。
下面的代碼將創建一個實例。
instance = ec2.Instance(self, "Instance",
instance_name = "myinstance",
instance_type=ec2.InstanceType("t3a.medium"),
machine_image=ec2.MachineImage.generic_windows({
'us-east-x': 'amiid',
}),
怎么寫成循環? 嘗試了 forloop 和 while 循環,失敗了。
盡管您沒有發布收到的錯誤或循環示例,但我將簡要說明如何啟動多個單一資源:
正如gshpychka在回答您的問題時提到的,您創建的每個構造(在您的情況下是 EC2 實例)必須唯一標識。 如果您查看實例構造或任何與此相關的構造,您會注意到所需的參數。 首先,是指您正在創建的堆棧的scope
。 其次,是id
,它是您在堆棧中創建的構造的 id(您可以在此文檔中了解有關構造 ID 的更多信息)。 此 ID 必須是唯一的。 您的代碼重新使用Instance
字符串作為 id,因此當在循環中重復傳遞時,此字符串不是唯一的。 為了在 python 中啟動多個 EC2 實例,您可以執行以下操作:
# define your desired instance quantity
num_instances = 5
# for each in that number range
for n in range(0, num_instances):
# convert the number to a str so we can add to the id
num_str = str(n)
# use leading zeros so naming is consistent for double digit numbers
suffix = num_str.zfill(2)
# create the instance
ec2.Instance(self, 'Instance{}'.format(suffix),
instance_name = "myinstance"
instance_type = ec2.InstanceType("t3a.medium"),
machine_image = ec2.MachineImage.generic_windows({
'us-east-x': 'amiid',
}),
這應該創建 5 個實例,其id
如下所示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.