How to create AutoScalling Group using CDK for existing EC2 instance?

0

I have been exploring AWS-CDK recently and programming language I am using is Python. Since I am a newbie in this concept, I am trying to achieve one thing: How can I create AutoScaling Group for existing ec2 instance? I assume somewhere i need to mention the instance id, attached here by as one parameter to ASG.

Now what exact issue I am facing is, I am not able to understand how to pass the details of existing ec2 instance while configuring AutoScalingGroup. I am not creating VPC using CDK, instead I created one EC2 instance using console.

Any leads would be really helpful!!

Below is the code for AutoScalingGroup:

{ from constructs import Construct from aws_cdk import aws_autoscaling as autoscaling

from aws_cdk import (aws_ec2 as ec2, aws_ssm as ssm, Stack)

class AutoScalingStack(Stack): def init(self, scope:Construct, id: str,vpc: ec2.Vpc, **kwargs) -> None: super().init(scope,**kwargs)

autoscaling.AutoScalingGroup(self, "ASG",
vpc=vpc,
launch_template= launch_template

)

PS: I am unable to find AWS CDK tag.

질문됨 일 년 전914회 조회
1개 답변
1

Hi,

I would like to inform you that attaching an existing EC2 instance to an AutoScalingGroup created via CDK or CloudFormation is not directly possible.

If you would check the CDK Documentation for class AutoScalingGroup - https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_autoscaling/AutoScalingGroup.html

You will find there's no such property or method available that can help in attaching existing EC2 instance to ASG.

Due to this limitation, your requirement can be completed using AWS Console or AWS CLI, and the process to do so is given in the document - https://docs.aws.amazon.com/autoscaling/ec2/userguide/attach-instance-asg.html

The API that is responsible for doing this is 'AttachInstances' - https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_AttachInstances.html

So, as a workaround, you may use custom resources in CDK to trigger this API and attach the ec2 instance via CDK if you do not wish to do so via the console. https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.custom_resources/README.html

If this is only a one-time requirement, then I'd suggest doing this via console, but if it is something that you would be doing in multiple accounts or regions, then I would recommend using custom resources to automate this process.

AWS
지원 엔지니어
Sagar_T
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠