EC2 instance creation fails with “InvalidParameterCombination: The parameter groupName cannot be used with the parameter subnet”

For the second time I ran into this same issue. When using RunInstancesCommand from the AWS SDK for Javascript, a certain parameter combination results in a misleading and confusing error message:

“InvalidParameterCombination: The parameter groupName cannot be used with the parameter subnet”

There is no parameter “groupName” in my request. The problem was that the SecurityGroupIds parameter is meant to be an array, not a string. I’m not sure what happens here. Maybe the SDK tries to evaluate SecurityGroupIds as a group name? In any case, here’s a test script that demonstrates the behaviour:

const {
  EC2Client,
  RunInstancesCommand,
} = require("@aws-sdk/client-ec2");

const ec2Config = {
  region: 'eu-west-1',
};

const ec2Client = new EC2Client(ec2Config);

// Works fine
const runInstancesParams1 = {
  ImageId: 'ami-0a8e758f5e873d1c1',
  InstanceType: 't3.medium',
  MaxCount: 1,
  MinCount: 1,
  SecurityGroupIds: ['sg-a2e119d3'],
  SubnetId: 'subnet-068bfe4e',
  TagSpecifications: [
    {
      ResourceType: 'instance',
      Tags: [
        {
          Key: 'Name',
          Value: 'Test server',
        },
      ],
    },
  ],
};

// Fails with "InvalidParameterCombination: The parameter groupName cannot be used with the parameter subnet"
const runInstancesParams2 = {
  ImageId: 'ami-0a8e758f5e873d1c1',
  InstanceType: 't3.medium',
  MaxCount: 1,
  MinCount: 1,
  SecurityGroupIds: 'sg-a2e119d3',
  SubnetId: 'subnet-068bfe4e',
  TagSpecifications: [
    {
      ResourceType: 'instance',
      Tags: [
        {
          Key: 'Name',
          Value: 'Test server',
        },
      ],
    },
  ],
};

const main = async () => {
const creationCommand1 = new RunInstancesCommand(
  runInstancesParams1,
);

let response;

try {
  response = await ec2Client.send(creationCommand1);
} catch(e) {
  console.log(e);
  process.exit();
}

console.log(response);

const creationCommand2 = new RunInstancesCommand(
  runInstancesParams2,
);

try {
  response = await ec2Client.send(creationCommand2);
} catch(e) {
  console.log(e);
  process.exit();
}

console.log(response);
};

main();

Leave a comment

Your email address will not be published.