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

使用AWS CDK Java创建EC2实例:显示Stack name而非Name标签求助

Troubleshooting AWS CDK Java ASG Name Tag Propagation Issue

Hey there! Let's dig into why your EC2 instances are showing the stack name instead of your custom Name tag, even with PropagateAtLaunch set to true. Here are the most common fixes and checks to try:

1. Verify Your Tag Key & Propagation Setup

First, double-check that you're defining the Name tag correctly (case matters for how the AWS Console displays it!) and explicitly setting propagateAtLaunch to true.

Correct Java Code Example:

// Option 1: Define tags directly in ASG props
AutoScalingGroup asg = AutoScalingGroup.Builder.create(this, "MyASG")
        .vpc(vpc)
        .instanceType(InstanceType.of(InstanceClass.T2, InstanceSize.MICRO))
        .machineImage(AmazonLinuxImage.Builder.create().generation(AmazonLinuxGeneration.AMAZON_LINUX_2).build())
        .tags(List.of(Tag.builder()
                .key("Name") // Must be exactly "Name" (capital N)
                .value("MyCustomInstanceName")
                .propagateAtLaunch(true)
                .build()))
        .build();

// Option 2: Add the tag after ASG initialization
asg.addTag("Name", "MyCustomInstanceName", true); // Third parameter enables propagation

A frequent misstep is using a lowercase name as the tag key—AWS Console prioritizes the exact Name key for instance name display, so any variation will fall back to showing the stack name.

2. Check for Launch Template/Configuration Conflicts

If your ASG uses a LaunchTemplate or LaunchConfiguration, make sure it isn't overriding the Name tag. Launch-level tags can take precedence if defined without propagation, or with a conflicting value:

  • If using a LaunchTemplate, either omit the Name tag entirely, or ensure its tags also have propagateAtLaunch set to true with your desired value.
  • Avoid mixing ASG-level and LaunchTemplate-level Name tags unless you intentionally want to override values.

3. Update Your AWS CDK Version

Older versions of the AWS CDK for Java have had minor bugs related to tag propagation for Auto Scaling Groups. Upgrading to the latest stable version can resolve unexpected behavior:

# Update CDK CLI (if you use it)
npm install -g aws-cdk

# Update your Java project's CDK dependencies (in pom.xml or build.gradle)
# For Maven, target dependencies like software.amazon.awscdk:aws-cdk-lib

4. Validate ASG Tag Configuration in AWS Console

After deploying your stack, head to the AWS Console's Auto Scaling Groups page:

  • Select your ASG, go to the Tags tab
  • Confirm your Name tag is listed with Propagate at launch set to Yes
  • If it's missing, recheck your code for typos or missing parameters

5. Test with a New Instance

Existing instances in the ASG won't pick up tag changes retroactively. Terminate an existing instance and let the ASG launch a new one—this will confirm if the tag propagation works for freshly provisioned instances.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 17:52:49