使用非默认 VPC (aws-sdk-go) 时无法调用 ec2.AuthorizeSecurity

运行以下代码时,我收到错误消息


InvalidGroup.NotFound The security group 'OddName' does not exist in default VPC 'vpc-2468'

这是正确的 VPC 名称,但不是查看 sg-1357 时会看到的 VPC ID。这是一个请求错误,而不是一个 aws 错误,所以它至少有那么远。


从命令行这有效:


aws ec2 authorize-security-group-ingress --group-id sg-1357 --cidr 127.0.0.1/32  --protocol tcp --port 443

我可以确认已经添加了 ip。


从github 上的sdk修改示例代码,以下会产生默认的 VPC 错误:


func ExampleEC2_AuthorizeSecurityGroupIngress() {

svc := ec2.New(nil)


params := &ec2.AuthorizeSecurityGroupIngressInput{

    CIDRIP:    aws.String("127.0.0.1"),

    DryRun:    aws.Boolean(true),

    FromPort:  aws.Long(443),

    GroupID:   aws.String("sg-1357"),

    GroupName: aws.String("OddName"),

    IPPermissions: []*ec2.IPPermission{

        { // Required

            FromPort:   aws.Long(1),

            IPProtocol: aws.String("String"),

            IPRanges: []*ec2.IPRange{

                { // Required

                    CIDRIP: aws.String("String"),

                },

                // More values...

            },

            PrefixListIDs: []*ec2.PrefixListID{

                { // Required

                    PrefixListID: aws.String("String"),

                },

                // More values...

            },

            ToPort: aws.Long(1),

            UserIDGroupPairs: []*ec2.UserIDGroupPair{

                { // Required

                    GroupID:   aws.String("String"),

                    GroupName: aws.String("String"),

                    UserID:    aws.String("String"),

                },

                // More values...

            },

        },

        // More values...

    },

    IPProtocol:                 aws.String("String"),

    SourceSecurityGroupName:    aws.String("String"),

    SourceSecurityGroupOwnerID: aws.String("String"),

    ToPort: aws.Long(443),

}



慕码人2483693
浏览 250回答 1
1回答

潇湘沐

如果您注释掉该GroupName行,它将解决DryRun错误。params 最终看起来像这样:  params := &ec2.AuthorizeSecurityGroupIngressInput{    CIDRIP:    aws.String("127.0.0.1/32"),    DryRun:    aws.Boolean(true),    FromPort:  aws.Long(443),    GroupID:   aws.String("sg-1357"),    IPProtocol:                 aws.String("tcp"),    SourceSecurityGroupName:    aws.String(""),    SourceSecurityGroupOwnerID: aws.String(""),    ToPort: aws.Long(443),  }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go