Search code examples
amazon-web-servicesamazon-s3amazon-ec2aws-sdkamazon-ami

command.resolveMiddleware is not a function for AWS SDK when creating AMI from EC2 instanceID, wondering why?


I was working on an AWS SDK version 3 and was trying to create an AMI from an EC2 instance programmatically. The following is the script I use:

// 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);
}

The Instance ID was captured from the following JSON object for listing all the running instance on my code

[
  {
    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
  }
]

The error I got was

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

I was confused as to why, is there any error on my code?


Solution

  • Turns out you had the same issue I did which was accidentally forgetting to pass in the actual command object.

    You use the params to build the command object but execute sendCommand with params instead of command as the parameter.

    You should be executing sendCommand(command); and not sendCommand(params);