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

Time:05-09

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

CodePudding user response:

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

CodePudding user response:

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
  • Related