使用Pulumi和Golang创建新的EC2实例时,出现了意外的MissingParameter ImageId错误。

huangapple go评论134阅读模式
英文:

Creating new ec2 instance using Pulumi on Golang gives unexpected MissingParameter ImageId error

问题

我正在尝试使用Pulumi和Golang创建一个新的AWS EC2实例。

这是我的函数:

ec2, errEc2 := ec2.NewInstance(ctx, name, &ec2.InstanceArgs{
	SubnetId:           pulumi.String(subnet.Id),
	Ami:                pulumi.String("ami-0022f774911c1d690"),
	AvailabilityZone:   pulumi.String("us-east-1"),
	InstanceType:       pulumi.String("t3.micro"),
	VpcSecurityGroupIds: pulumi.StringArray{secGroup.ID()},
	Tags: pulumi.StringMap{
		"Project":     pulumi.String("projectName"),
		"Name":        pulumi.String("exampleName"),
		"Environment": pulumi.String("staging"),
	},
})

根据这个文档作为参考,我的输入参数看起来没问题。

但是当我运行pulumi up时,我得到了这个奇怪的错误:

error: 1 error occurred:
* creating EC2 Instance: MissingParameter: The request must contain the parameter ImageId
status code: 400, request id: 91babb9b....

在文档或者网上都没有找到需要使用ImageId的要求,当我尝试将其提供给ec2.NewInstance函数时,我得到了"unknown field"的语法错误。

注意:

我正在使用以下包:

github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ec2
github.com/pulumi/pulumi/sdk/v3/go/pulumi
英文:

I am trying to create a new AWS EC2 instance using Pulumi on Golang.

This is my function:

	ec2, errEc2 := ec2.NewInstance(ctx, name, &ec2.InstanceArgs{
	SubnetId: 				pulumi.String(subnet.Id),
	Ami:          			pulumi.String("ami-0022f774911c1d690"),
	AvailabilityZone: 		pulumi.String("us-east-1"]),
	InstanceType: 			pulumi.String("t3.micro"),
	VpcSecurityGroupIds: 	pulumi.StringArray{secGroup.ID()},
	Tags: pulumi.StringMap{
		"Project": pulumi.String("projectName"),
		"Name": pulumi.String("exampleName"),
		"Environment": pulumi.String("staging"),
	},
})

The input parameters look fine to me using this documentation as a reference

But when I am running

pulumi up

I get this weird error

    error: 1 error occurred:
    * creating EC2 Instance: MissingParameter: The request must contain the parameter ImageId
    status code: 400, request id: 91babb9b....

Nowhere, inside the documentation or the web, have I found the need to use ImageId, and when I am trying to provide it to the ec2.NewInstance function, I get "unknown field" syntax error

Note:

And I am using the packages

github.com/pulumi/pulumi-aws/sdk/v5/go/aws/ec2

github.com/pulumi/pulumi/sdk/v3/go/pulumi

答案1

得分: 0

我看到你的代码中有几个错误:

AvailabilityZone: pulumi.String("us-east-1"]),

这里多了一个 ],应该是:

AvailabilityZone: pulumi.String("us-east-1"),

此外,us-east-1 不是一个有效的可用区,你可能想要使用 us-east-1a,所以最终应该是:

AvailabilityZone: pulumi.String("us-east-1a"),

关于你的镜像 ID 问题,我建议检查你引用的 AMI 是否确实存在于你的账户中。镜像 ID 属性是通过 Ami id 发送的,所以应该足够了。

英文:

I see a few errors with your code:

AvailabilityZone: pulumi.String("us-east-1"]),

There's an extra ] in here, it should be:

AvailabilityZone: pulumi.String("us-east-1"),

In addition to this, us-east-1 isn't a valid availability zone, you probably want us-east-1a, so ultimately:

AvailabilityZone: pulumi.String("us-east-1a"),

With regards to your image id issue, I would check the AMI you're reference actually exists in your account. The image ID property is sent by the Ami id, so that should be enough

答案2

得分: 0

修复了一个错误 - 我不知道问题是什么,因为id是相同的,但我修复的方法是这样的:

在之前使用LookupAmi函数:

func GetAmi(ctx *pulumi.Context, Ami map[string]string) (*ec2.LookupAmiResult, error){
    ami, amiErr := ec2.LookupAmi(ctx, &ec2.LookupAmiArgs{
        NameRegex:  pulumi.StringRef(Ami["Name"]),
        Owners: []string {
            Ami["Owner"],
        },
    })
    if amiErr != nil {
        log.Fatal("尝试获取ami镜像时出错!", amiErr)
    }
    return ami, amiErr
}

然后只使用前一个函数中的ami.Id字段创建ec2实例:

ec2, errEc2 := ec2.NewInstance(ctx, name, &ec2.InstanceArgs{
    SubnetId:               pulumi.String(subnet.Id),
    Ami:                    pulumi.String(ami.Id),
    AvailabilityZone:       pulumi.String(Ec2["AvailabilityZone"]),
    InstanceType:           pulumi.String(Ec2["InstanceType"]),
    VpcSecurityGroupIds:    pulumi.StringArray{secGroup.ID()},
    Tags: pulumi.StringMap{
        "Project": pulumi.String(Ec2["Name"]),
        "Name": pulumi.String(name),
        "Environment": pulumi.String(environment),
    },
})
if errEc2 != nil {
    log.Fatalln("尝试创建ec2实例时出错!", errEc2)
}
return ec2, errEc2
英文:

Fixed the bug - I don't know what was the issue since the id's are identical, but the way I fixed it is by doing so:

use the LookupAmi func before:

func GetAmi(ctx *pulumi.Context, Ami map[string]string) (*ec2.LookupAmiResult, error){
ami, amiErr := ec2.LookupAmi(ctx, &ec2.LookupAmiArgs{
	NameRegex:  pulumi.StringRef(Ami["Name"]),
	Owners: []string {
		Ami["Owner"],
	},
})
if amiErr != nil {
	log.Fatal("Got error while trying to get ami image!", amiErr)
}
return ami, amiErr

}

And only then create the ec2 instance using the ami.Id field from the previous func:

	ec2, errEc2 := ec2.NewInstance(ctx, name, &ec2.InstanceArgs{
	SubnetId: 				pulumi.String(subnet.Id),
	Ami:          			pulumi.String(ami.Id),
	AvailabilityZone: 		pulumi.String(Ec2["AvailabilityZone"]),
	InstanceType: 			pulumi.String(Ec2["InstanceType"]),
	VpcSecurityGroupIds: 	pulumi.StringArray{secGroup.ID()},
	Tags: pulumi.StringMap{
		"Project": pulumi.String(Ec2["Name"]),
		"Name": pulumi.String(name),
		"Environment": pulumi.String(environment),
	},
})
if errEc2 != nil {
	log.Fatalln("Got error while trying to create ec2 instance!", errEc2)
}
return ec2, errEc2

huangapple
  • 本文由 发表于 2022年5月8日 23:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/72162693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定