使用 requestSpotInstances 的 TagSpecifications 使用 aws-sdk 的 UnexpectedParameter

2024-03-10

我正在尝试向我的 AWS Spot 请求添加标签。但它已经归还给我了{ UnexpectedParameter: Unexpected key 'TagSpecifications' found in params.LaunchSpecification。 我已关注本文档 https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#requestSpotInstances-property,并且我已经尝试将此代码移出 LaunchSpecification,但错误仍然存​​在。

  const params = {
    InstanceCount: 1,
    LaunchSpecification: {
      ImageId: config.aws.instanceAMI,
      KeyName: 'backoffice',
      InstanceType: config.aws.instanceType,
      SecurityGroupIds: [config.aws.instanceSecurityGroupId],
      TagSpecifications: [{
        ResourceType: 'instance',
        Tags: [{
          Key: 'Type',
          Value: 'Mongo-Dump',
        }],
      }],
      BlockDeviceMappings: [{
        DeviceName: '/dev/xvda',
        Ebs: {
          DeleteOnTermination: true,
          SnapshotId: 'snap-06e838ce2a80337a4',
          VolumeSize: 50,
          VolumeType: 'gp2',
          Encrypted: false,
        },
      }],
      IamInstanceProfile: {
        Name: config.aws.instanceProfileIAMName,
      },
      Placement: {
        AvailabilityZone: `${config.aws.region}a`,
      },
    },
    SpotPrice: config.aws.instancePrice,
    Type: 'one-time',
  };

  return ec2.requestSpotInstances(params).promise();

有些事情让我认为问题出在文档或 Javascript 本身的 aws-sdk 中。我的选择已经用尽了。


错误信息是正确的。根据文档 https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RequestSpotLaunchSpecification.html, the RequestSpotLaunchSpecification对象没有名为的属性TagSpecifications.

但是,您可以在创建 Spot 实例请求后对其进行标记。

ec2.requestSpotInstances(params)返回一个数组SpotInstanceRequest对象,每个对象包含一个spotInstanceRequestId (e.g. sir-012345678)。使用创建标签API https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#createTags-property使用这些 Spot 实例请求 ID 来添加标签。

const createTagParams = {
  Resources: [ 'sir-12345678' ], 
  Tags: [
    {
      Key: 'Type', 
      Value: 'Mongo-Dump'
    }
  ]
};
ec2.createTags(createTagParams, function(err, data) {
  // ...
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 requestSpotInstances 的 TagSpecifications 使用 aws-sdk 的 UnexpectedParameter 的相关文章

随机推荐