0

How can we modify ec2 instance with cdk stack or pipeline?

I am able to launch the ec2 instance but whenever i modify the stack it creates a new instance.

So, any idea how to launch and manage ec2 instances meaning change instance type , size etc programmatically?

Comments
  • 1
    I have no idea but shouldn't changing the instance type create a new one and destroy the old one?
  • 1
    It should destroy your instance and then recreate a new one. CDK is just a fancy CloudFormation generator though, so it might share the same caveats
  • 1
    It’s a good practice to use immutable infastructure. So unless we’re trying to modify a stateful resource like a db, you should always replace. Since you are doing IaC anyways, why would you even need to modify compute instances in-place?
  • 0
    @100110111 actually the idea is to give user a customised ec2 instance when they buy a plan with some ready made service on ec2 and when user upgrade it i wished to modify ec2 instance.

    But as i can see.. it is not possible to modify ec2 .

    However the problem will be in changing the existing ebs volume with new updated ec2 instance.
  • 0
    @asgs @asgs i think thats what i need to do, but what about existing storage what needs to attach to new ec2 if i destroy and create new one
  • 1
    you can always use an awscli/aws SDK from inside the ec2 to spin up another ec2 from the same AMI and change its specs. Once that's done, switch to that newer instance. And terminate the old one.

    Or you could write and trigger a lambda that

    - captures your ec2's state (sessid, configurations, whatever)

    - scales your _current_ ec2 instance

    - when ec2 starts back up - restores state/configs/whatever to the upgraded ec2.

    Though lambda has a runtime limit of 15 minutes, so you'll have to make sure this manoeuvre completes sooner, otherwise, it'll all go down the gutter.

    To keep your files intact, you can use non-ephemeral storage (EBS) instead of the ephemeral intance-storage
  • 1
    https://docs.aws.amazon.com/AWSEC2/...

    Looks like stop and upgrade does the job
  • 0
    Thanks all, for your comments.

    I think i have to use either aws cli with bash script or aws sdk to modify ec2 and ebs volume without creating a new one.
Add Comment