Skip to content

Instantly share code, notes, and snippets.

@eddmann
Created November 3, 2017 15:32

Revisions

  1. eddmann created this gist Nov 3, 2017.
    19 changes: 19 additions & 0 deletions modify-ec2-instance-type.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    // Demonstration video can be found at: https://youtu.be/_gJyK1-NGq8

    // ModifyEC2InstanceType

    const AWS = require('aws-sdk');

    exports.handler = (event, context, callback) => {
    const { instanceId, instanceRegion, instanceType } = event;

    const ec2 = new AWS.EC2({ region: instanceRegion });

    Promise.resolve()
    .then(() => ec2.stopInstances({ InstanceIds: [instanceId] }).promise())
    .then(() => ec2.waitFor('instanceStopped', { InstanceIds: [instanceId] }).promise())
    .then(() => ec2.modifyInstanceAttribute({InstanceId: instanceId, InstanceType: { Value: instanceType } }).promise())
    .then(() => ec2.startInstances({ InstanceIds: [instanceId] }).promise())
    .then(() => callback(null, `Successfully modified ${event.instanceId} to ${event.instanceType}`))
    .catch(err => callback(err));
    };