【问题标题】:Can't call ec2.AuthorizeSecurityGroupIngressInput when using other than the default VPC (aws-sdk-go)使用非默认 VPC (aws-sdk-go) 时无法调用 ec2.AuthorizeSecurityGroupIngressInput
【发布时间】:2015-07-08 04:01:01
【问题描述】:

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

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已经添加。

修改sdk on github中的示例代码,下面会产生默认的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),
}
resp, err := svc.AuthorizeSecurityGroupIngress(params)

if err != nil {
    if awsErr, ok := err.(awserr.Error); ok {
        // Generic AWS error with Code, Message, and original error (if any)
        fmt.Println(awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
        if reqErr, ok := err.(awserr.RequestFailure); ok {
            // A service error occurred
            // ERROR GETS REPORTED HERE
            fmt.Println(reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
        }
    } else {
        // This case should never be hit, the SDK should always return an
        // error which satisfies the awserr.Error interface.
        fmt.Println(err.Error())
    }
}

// Pretty-print the response data.
fmt.Println(awsutil.StringValue(resp))

}

【问题讨论】:

  • 您是否尝试过输入的一些变化?例如,请注意 bash 命令和 Go 中的字符串文字之间的细微差别; sg-13571357。看了一眼文档,我觉得你需要前者。
  • 我的实际代码中确实有正确的 GroupID。删除我的特定数据时出现错误。编辑代码以反映 sg-1357
  • 我越来越挑剔了,但你的 CIDRIP 值也有点不同。另外,您可以从命令中删除 groupName 吗?它说它正在查找该组的默认 VPC,但是您使用 GroupID 表明您可能正在使用默认值以外的东西?除此之外,两者都不是必需的,所以我认为两者之一就足够了。
  • 我几乎回答说我曾尝试删除 GroupName。所以答案是“注释掉 GroupName: 行”,它会导致 DryRun 错误。如果你回答我会确认。谢谢。
  • 我很高兴并非常感谢有机会回答:D

标签: amazon-web-services go amazon-ec2


【解决方案1】:

如果您注释掉 GroupName 行,它将导致 DryRun 错误。

参数最终看起来像这样:

  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),
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-18
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多