You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS SDK v3从EC2实例创建AMI时遇command.resolveMiddleware错误

问题:使用AWS SDK v3创建EC2 AMI时触发TypeError错误

问题详情

我用AWS SDK v3编写脚本,试图从运行中的EC2实例生成AMI,但执行时出现了错误。

我的脚本代码

// Imports
// TODO: Import the ec2 client

const {
  EC2Client,
  CreateImageCommand
} = require('@aws-sdk/client-ec2')

function sendCommand (command) {
  const client = new EC2Client({ region: process.env.AWS_REGION })
  return client.send(command)
}

createImage('i-0672b492fb7f92bfd', 'hamsterImage')
  .then(() => console.log('Complete'))

async function createImage (seedInstanceId, imageName) {
  // TODO: Implement AMI creation
  const params = {
    InstanceId: seedInstanceId,
    Name: imageName
  };
  const command = new CreateImageCommand(params);
  return sendCommand(params);
}

目标EC2实例信息

实例ID来自以下运行实例的JSON数据:

[
  {
    "AmiLaunchIndex": 0,
    "ImageId": "ami-0b5eea76982371e91",    
    "InstanceId": "i-0672b492fb7f92bfd",   
    "InstanceType": "t2.micro",
    "KernelId": undefined,
    "KeyName": "hamster_key",
    "LaunchTime": "2022-12-27T03:10:12.000Z",
    "Monitoring": { "State": "disabled" },   
    "Placement": {
      "AvailabilityZone": "us-east-1c",    
      "Affinity": undefined,
      "GroupName": "",
      "PartitionNumber": undefined,        
      "HostId": undefined,
      "Tenancy": "default",
      "SpreadDomain": undefined,
      "HostResourceGroupArn": undefined
    },
    "Platform": undefined,
    "PrivateDnsName": "ip-172-31-80-74.ec2.internal",
    "PrivateIpAddress": "172.31.80.74",
    "ProductCodes": [],
    "PublicDnsName": "ec2-54-162-64-244.compute-1.amazonaws.com",
    "PublicIpAddress": "54.162.64.244",
    "RamdiskId": undefined,
    "State": { "Code": 16, "Name": "running" },
    "StateTransitionReason": "",
    "SubnetId": "subnet-09fbb143b92cb9d1b",
    "VpcId": "vpc-0ba9dffc72ac05ef6",
    "Architecture": "x86_64",
    "BlockDeviceMappings": [ [Object] ],
    "ClientToken": "bd84c832-4429-482d-bdf9-323ddbb8cad4",
    "EbsOptimized": false,
    "EnaSupport": true,
    "Hypervisor": "xen",
    "IamInstanceProfile": undefined,
    "InstanceLifecycle": undefined,
    "ElasticGpuAssociations": undefined,
    "ElasticInferenceAcceleratorAssociations": undefined,
    "NetworkInterfaces": [ [Object] ],
    "OutpostArn": undefined,
    "RootDeviceName": "/dev/xvda",
    "RootDeviceType": "ebs",
    "SecurityGroups": [ [Object] ],
    "SourceDestCheck": true,
    "SpotInstanceRequestId": undefined,
    "SriovNetSupport": undefined,
    "StateReason": undefined,
    "Tags": undefined,
    "VirtualizationType": "hvm",
    "CpuOptions": { "CoreCount": 1, "ThreadsPerCore": 1 },
    "CapacityReservationId": undefined,
    "CapacityReservationSpecification": {
      "CapacityReservationPreference": "open",
      "CapacityReservationTarget": undefined
    },
    "HibernationOptions": { "Configured": false },
    "Licenses": undefined,
    "MetadataOptions": {
      "State": "applied",
      "HttpTokens": "optional",
      "HttpPutResponseHopLimit": 1,
      "HttpEndpoint": "enabled",
      "HttpProtocolIpv6": "disabled"
    },
    "EnclaveOptions": { "Enabled": false },
    "BootMode": undefined
  }
]

错误信息

C:\Users\jiali\OneDrive\Desktop\aws-developer-designing-developing\03\demos\before\node_modules\@aws-sdk\smithy-client\dist\cjs\client.js:13
        const handler = command.resolveMiddleware(this.middlewareStack, this.config, options);                                ^

TypeError: command.resolveMiddleware is not a function
    at EC2Client.send (C:\Users\jiali\OneDrive\Desktop\aws-developer-designing-developing\03\demos\before\node_modules\@aws-sdk\smithy-client\dist\cjs\client.js:13:33)
    at sendCommand (C:\Users\jiali\OneDrive\Desktop\aws-developer-designing-developing\03\demos\before\scripts\03\create-ami.js:11:17)
    at createImage (C:\Users\jiali\OneDrive\Desktop\aws-developer-designing-developing\03\demo
PS C:\Users\jiali\OneDrive\Desktop\aws-developer-designing-developing\03\demos\before\scripts\03> node .\manage-ec2-instance.js

问题解决

错误原因

createImage函数中,你已经正确创建了CreateImageCommand实例,但调用sendCommand时传入的是普通参数对象params,而非创建好的command实例。EC2Client.send()方法要求接收的是AWS SDK的Command对象,普通参数对象没有resolveMiddleware方法,因此触发了这个错误。

修正后的代码

// Imports
const {
  EC2Client,
  CreateImageCommand
} = require('@aws-sdk/client-ec2')

function sendCommand (command) {
  const client = new EC2Client({ region: process.env.AWS_REGION })
  return client.send(command)
}

createImage('i-0672b492fb7f92bfd', 'hamsterImage')
  .then(() => console.log('Complete'))
  .catch(err => console.error('创建AMI失败:', err)) // 新增错误捕获,方便排查问题

async function createImage (seedInstanceId, imageName) {
  const params = {
    InstanceId: seedInstanceId,
    Name: imageName
  };
  const command = new CreateImageCommand(params);
  return sendCommand(command); // 这里改为传入command实例
}

额外提示

  1. 建议保留错误捕获逻辑,后续如果遇到权限不足、实例状态异常等问题,能快速定位原因
  2. 确保你的AWS凭证(环境变量、本地配置文件等)拥有ec2:CreateImage的权限
  3. 目标实例状态为running(你的实例符合要求,无需调整)

内容的提问来源于stack exchange,提问作者Danny Shen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 13:55:36