gpt4 book ai didi

python-3.x - 您好,我编写了一段代码来使用对流层创建 CloudFormation 模板,但运行代码时出现语法错误

转载 作者:行者123 更新时间:2023-12-03 07:35:36 25 4
gpt4 key购买 nike

我已经编写了一段代码来使用对流层创建 CloudFormation 模板,其中我正在创建 EC2 实例并辅助 SG 和 EIP,但在运行 python 代码时出现错误。代码如下:

from troposphere import Ref, Template, join, GetAtt, Parameter
import troposphere.ec2 as ec2

template = Template()

#Creating Security Group

SSHSecurityGroup = ec2.SecurityGroup('SSHSecurityGroup')
SSHSecurityGroup.GroupDescription = "Enable SSH access bia port 22"
SSHSecurityGroup.SecurityGroupIngress = [
ec2.SecurityGroupRule(
CidrIP="0.0.0.0/0",
FromPort="22",
IpProtocol="tcp",
ToPort="22",
)]

ServerSecurityGroup = ec2.SecurityGroup('ServerSecurityGroup')
ServerSecurityGroup.GroupDescription("Allow access from specified CIDR range")
ServerSecurityGroup.SecurityGroupIngress = [
ec2.SecurityGroupRule(
IpProtocol="tcp",
FromPort="80",
ToPort="80",
CidrIp="0.0.0.0/0",
IpProtocol="tcp",
FromPort="22",
ToPort="22",
CidrIP="192.168.1.1/32",
)]
template.add_resource(SSHSecurityGroup)
template.add_resource(ServerSecurityGroup)
#Creating EC2 Instance
ec2_instance = template.add_resource(ec2.Instance("MyEC2Intsance",
ImageId="ami-i23df45832",
AvailabilityZone="us-east-2",
InstanceType="t2.micro",
EIP=Ref(MyEIP),
SecurityGroups=[Ref(SSHSecurityGroup),Ref(ServerSecurityGroup)],
))

#Creating EIP
MyEIP = ec2.EIP("MyEIP", InstanceId=Ref(MyEC2Intsance)
#template.add_resource(MyEIP)
print(template.to_yaml())```

最佳答案

该行需要一个尾随(结束)括号:更改此:

MyEIP = ec2.EIP("MyEIP", InstanceId=Ref(MyEC2Intsance)

对此:

MyEIP = ec2.EIP("MyEIP", InstanceId=Ref(MyEC2Intsance))

SecurityGroupIngress/SecurityGroupRules 还存在其他问题,但不知道如何指导,因为不确定您正在尝试什么。

关于python-3.x - 您好,我编写了一段代码来使用对流层创建 CloudFormation 模板,但运行代码时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60554327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com