aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.55k stars 3.87k forks source link

ec2: LookupMachineImage always return dummy `ami-1234` image #22958

Closed munjalpatel closed 1 year ago

munjalpatel commented 1 year ago

Describe the bug

I am trying to look up an image with fck-nat-amzn2-hvm-*-arm64-ebs as name. I have verified that the image from owner 568608671756 exists in my region us-west-2.

But new ec2.LookupMachineImage(...) always return the dummy ami-1234

Expected Behavior

An image object for the fck-nat-amzn2-hvm-1.2.0-20220812-arm64-ebs image with ami-005e79c34846da0a4 should be returned.

Current Behavior

An image object for the dummy image with ami-1234 is returned.

Reproduction Steps

test.ts

import { aws_ec2 as ec2 } from 'aws-cdk-lib'
import { App, Stack, StackProps } from 'aws-cdk-lib'

class Asg extends Stack {
  private readonly id: string

  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props)

    this.id = id
  }

  async create(): Promise<string> {
    const machineImage = new ec2.LookupMachineImage({
      name: 'fck-nat-amzn2-hvm-*-arm64-ebs',
      owners: ['568608671756'],
    })

    const image = machineImage.getImage(this)
    console.log(image)

    return 'asg-id'
  }
}

;(async () => {
  const app = new App()

  const asg = new Asg(app, 'aws-test', { env: { region: 'us-west-2', account: 'your-aws-account-id' } })
  await asg.create()
})()

Run this:

$ npx ts-node test.ts

Output:

{imageId: 'ami-1234', osType: 0, userData: LinuxUserData}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.50.0 (build 4c11af6)

Framework Version

No response

Node.js Version

v16.13.2

OS

MacOS Ventura 13.0 (22A380)

Language

Typescript

Language Version

No response

Other information

No response

pahud commented 1 year ago

This is my sample code. And the ImageId seems to be ami-005e79c34846da0a4. Is this something you are looking for?

import {
  App, Stack, StackProps, CfnOutput,
  aws_ec2 as ec2,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class MyStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps = {}) {
    super(scope, id, props);

    const lookupImage = new ec2.LookupMachineImage({
      name: 'fck-nat-amzn2-hvm-*-arm64-ebs',
      owners: ['568608671756'],
    });

    new CfnOutput(this, 'ImageId', { value: lookupImage.getImage(this).imageId });

  }
}

// for development, use account/region from cdk cli
const devEnv = {
  account: process.env.CDK_DEFAULT_ACCOUNT,
  region: process.env.CDK_DEFAULT_REGION,
};

const app = new App();

new MyStack(app, 'test-dev', { env: devEnv });

app.synth();

Outputs [+] Output ImageId ImageId: {"Value":"ami-005e79c34846da0a4"}

github-actions[bot] commented 1 year ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

munjalpatel commented 1 year ago

@pahud, thanks for the help!

If I run stack using npm run cdk deploy it seems to be working fine.

However, I am integrating AWS CDK in another project and need to run it using regular node-ts, and that's when it seems to be failing.

pahud commented 1 year ago

@munjalpatel I'm afraid you can't get the imageId simply by this in an async function:

 const image = machineImage.getImage(this)
 console.log(image)

You should only reference it rather than console.log() it otherwise you will get the default dummy value: https://github.com/aws/aws-cdk/blob/cea1039e3664fdfa89c6f00cdaeb1a0185a12678/packages/%40aws-cdk/aws-ec2/lib/machine-image.ts#L662

This doesn't seem to be a valid bug but it might be a good topic for discussion about your use case. I am closing this issue for now but if you are interested to discuss more about your use case using async func in CDK, feel free to create a separate topic in discussions. Thanks!

github-actions[bot] commented 1 year ago

⚠️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.