Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2: InitFile.fromAsset Construct conflict #26452

Closed
spg opened this issue Jul 20, 2023 · 2 comments
Closed

ec2: InitFile.fromAsset Construct conflict #26452

spg opened this issue Jul 20, 2023 · 2 comments
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug.

Comments

@spg
Copy link
Contributor

spg commented Jul 20, 2023

Describe the bug

When using ec2.InitFile.fromAsset(targetFileName, path) more than once in the same stack, using the same targetFileName value, an error is raised during synthesis:

VPC_ID=vpc-03d0fd37XXXXX ACCOUNT=101010101010 npx cdk deploy --all
/Users/spg/cdk-error-repo/node_modules/constructs/src/construct.ts:423
      throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^
Error: There is already a Construct with name '--etc--test.txtAsset' in MyStack [MyStack-test]
    at Node.addChild (/Users/spg/cdk-error-repo/node_modules/constructs/src/construct.ts:423:13)
    at new Node (/Users/spg/cdk-error-repo/node_modules/constructs/src/construct.ts:71:17)
    at new Construct (/Users/spg/cdk-error-repo/node_modules/constructs/src/construct.ts:483:17)
    at new Asset (/Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.js:1:473)
    at Object._doBind (/Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.js:1:7114)
    at Object._bind (/Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-ec2/lib/cfn-init-elements.js:1:8579)
    at /Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-ec2/lib/cfn-init.js:1:7758
    at Array.map (<anonymous>)
    at InitConfig.bindForType (/Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-ec2/lib/cfn-init.js:1:7741)
    at InitConfig._bind (/Users/spg/cdk-error-repo/node_modules/aws-cdk-lib/aws-ec2/lib/cfn-init.js:1:6825)

Subprocess exited with error 1

Expected Behavior

I should be able to call ec2.InitFile.fromAsset(targetFileName, path) more than once in the same stack, using the same targetFileName value.

Current Behavior

See bug description

Reproduction Steps

App to reproduce:

// app.ts
import * as cdk from 'aws-cdk-lib'
import * as autoscaling from 'aws-cdk-lib/aws-autoscaling'
import * as ec2 from 'aws-cdk-lib/aws-ec2'
import { Construct } from 'constructs'
import path from 'path'

const app = new cdk.App()

const vpcId = process.env['VPC_ID']
if (!vpcId) {
  throw new Error('VPC_ID is required')
}

const account = process.env['ACCOUNT']
if (!account) {
  throw new Error('ACCOUNT is required')
}

class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props)

    const vpc = ec2.Vpc.fromLookup(this, 'VPC', {
      vpcId,
    })

    new autoscaling.AutoScalingGroup(this, 'Asg1', {
      vpc,
      instanceType: new ec2.InstanceType('t3.nano'),
      desiredCapacity: 1,
      minCapacity: 0,
      machineImage: ec2.MachineImage.latestAmazonLinux2(),
      signals: autoscaling.Signals.waitForAll(),
      init: ec2.CloudFormationInit.fromElements(
        ec2.InitFile.fromAsset('/etc/test.txt', path.join(__dirname, 'test.txt'))
      ),
    })

    new autoscaling.AutoScalingGroup(this, 'Asg2', {
      vpc,
      instanceType: new ec2.InstanceType('t3.nano'),
      desiredCapacity: 1,
      minCapacity: 0,
      machineImage: ec2.MachineImage.latestAmazonLinux2(),
      signals: autoscaling.Signals.waitForAll(),
      init: ec2.CloudFormationInit.fromElements(
        ec2.InitFile.fromAsset('/etc/test.txt', path.join(__dirname, 'test.txt'))
      ),
    })
  }
}

new MyStack(app, 'MyStack-test', { env: { region: 'us-east-1', account } })

You should also create a test.txt file (content does not matter) sitting in the same directory as app.ts.

Possible Solution

This line seems to be causing the issue:

const bindResult = this.bind(attachedResource.stack, attachOptions);

Maybe the CloudFormation Init config should be bound to the current scope instead of the current stack?

Additional Information/Context

No response

CDK CLI Version

2.88.0 (build 5d497f9)

Framework Version

No response

Node.js Version

v16.19.0

OS

macOS Venture 13.2

Language

Typescript

Language Version

5.0.4

Other information

No response

@spg spg added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 20, 2023
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Jul 20, 2023
@pahud
Copy link
Contributor

pahud commented Jul 20, 2023

Thank you for your report. As this is duplicate I am closing this in favor of #16891

@pahud pahud removed the needs-triage This issue or PR still needs to be triaged. label Jul 20, 2023
@pahud pahud closed this as completed Jul 20, 2023
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants