我为一个安全组创建了一个 cloudformation 模板,其中包含 3 个入口规则,一个用于 SSH,一个用于 HTTP,另一个用于 HTTP(端口 8080)
首先,当我包含 SourceSecurityGroupOwnerId 时,该组根本不会创建。当我删除它时,该组会创建但仅使用一条规则(ssh 规则)。
这是完整的模板:
AWSTemplateFormatVersion: 2010-09-09
Description: Provision security group to allow SSH access to instance
Parameters:
EnvironmentName:
Description: An environment name that will be prefixed to resource names
Type: String
SSHLocation:
Description: The IP address range that can be used to SSH to the EC2 instances
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Resources:
InstanceSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !ImportValue hvfVPC-Name
Tags:
- Key: Name
Value: !Ref EnvironmentName
GroupDescription: Enable SSH access and HTTP from the load balancer only
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref SSHLocation
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
SourceSecurityGroupOwnerId: !ImportValue wordpressELB-SG-Id
SourceSecurityGroupName: !ImportValue wordpressELB-SG-Name
- IpProtocol: tcp
FromPort: '8080'
ToPort: '8080'
SourceSecurityGroupOwnerId: !ImportValue wordpressELB-SG-Id
SourceSecurityGroupName: !ImportValue wordpressELB-SG-Name
这最终只是运行了很长一段时间而没有实际创建组。当我删除 SourceSecurityGroupId 和 SourceSecurityGroupName 时,模板会运行,但它只创建一个入口规则。
我进行了三次检查,以确保导出正确,但由于某种原因,除非我删除这两行,否则 Cloudformation 就会挂起
附上图片以供澄清 CloudFormation says the group is created Only the SSH ingress rule is actually created
Exports from the ELB template that I'm trying to use in the SG template
最佳答案
我的猜测是,您应该简单地指定 SourceSecurityGroupId
而不是 SourceSecurityGroupOwnerId
和 SourceSecurityGroupName
。大致思路是这样的:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !ImportValue wordpressELB-SG-Id
类似的配置很适合我。
关于 SourceSecurityGroupOwnerId
的文档说:
You can't specify this parameter in combination with the following parameters: the CIDR IP address range, the IP protocol, the start of the port range, and the end of the port range.
这可能是未创建规则的原因。
接下来,您似乎在 SourceSecurityGroupOwnerId
中指定了源安全组 wordpressELB-SG-Id
的 ID。 SourceSecurityGroupOwnerId
实际上是源安全组的 AWS账户 ID,因此这无论如何都是不正确的。
关于amazon-web-services - 云形成安全组未创建入口规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58071034/