TagSpecifications with requestSpotInstances UnexpectedParameter with aws-sdk

末鹿安然 提交于 2021-02-10 15:53:59

问题


I'm trying to add a tag to my AWS Spot Request. But it has returned me { UnexpectedParameter: Unexpected key 'TagSpecifications' found in params.LaunchSpecification. I have followed this documentation, and I have already tried to move this code out of LaunchSpecification, but the error persists.

  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();

Something makes me think that the problem is in the documentation or in the aws-sdk for Javascript itself. My options are exhausted.


回答1:


The error message is correct. According to the documentation, the RequestSpotLaunchSpecification object doesn't have an attribute called TagSpecifications.

However, you can tag your Spot Instance request after you create it.

ec2.requestSpotInstances(params) returns an array of SpotInstanceRequest objects, each containing a spotInstanceRequestId (e.g. sir-012345678). Use the CreateTags API with these Spot Instance request ids to add the tags.

const createTagParams = {
  Resources: [ 'sir-12345678' ], 
  Tags: [
    {
      Key: 'Type', 
      Value: 'Mongo-Dump'
    }
  ]
};
ec2.createTags(createTagParams, function(err, data) {
  // ...
});


来源:https://stackoverflow.com/questions/61801063/tagspecifications-with-requestspotinstances-unexpectedparameter-with-aws-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!