I'm using imagebuilder to create a new AMI. Is th...
# aws
w
I'm using imagebuilder to create a new AMI. Is there any way to then use the newly created AMI for an instance within the same run? It doesn't look like it, since the AMI data is only available as an output resource, and I can't use that at a later place within the same run. Just feels weird to have to run twice but 🤷
a
Can you share what AMI data is when AMI is created? There should be an AMI id you can reference later.
w
There is, but the only place I see it in the state is as an OutputResource
https://www.pulumi.com/registry/packages/aws/api-docs/imagebuilder/image/#outputs - and that's borne out here as well, AMI is only under the outputResources list
a
How do you want to use this AMI? In some ec2 instances?
w
Yep
a
imageResource.id
is the value when you setup ec2 instances
Copy code
const fooInstance = new aws.ec2.Instance("foo", {
    ami: "ami-005e54dee72cc1d00",
    instanceType: aws.ec2.InstanceType.T2_Micro,
    networkInterfaces: [{
        networkInterfaceId: foo.id,
        deviceIndex: 0,
ami: imageResource.id,
w
Strange - I sent a message and it vanished
But no, the id field is not an AMI
It's the ID of the imagebuilder image (an arn, specifically), not of the AMI that the image outputs
Copy code
"urn": "urn:pulumi:dev::console::aws:imagebuilder/image:Image::<imagename>",
        "id": "arn:aws:imagebuilder:us-west-2:<account>:image/<imagename>/1.0.0/1",
a
then ami id should be in
image.png
w
Yeah, it's definitely in the output resource - but I didn't think you could use an output resource in the same run as when it's being fetched? VScode gives me a big cranky error anyway
Screenshot 2025-08-26 at 11.21.18 AM.png
a
If it is
Output
, pulumi will wait for it to be created at runtime.
The above warning applies to all
Output
values.
w
Hmm, okay. That being said, I still haven't been able to figure the formatting of the precise code needed to get the AMI from that outputresource, but if in theory it should work then I'll keep bashing away at it. Thanks!
👍 1
l
Aside: a rule I apply everywhere I work: building any sort of package (NPM, WAR, Docker, AMI...) belongs in CI, not CD. You can use Pulumi to do it, but the packaging project (CI) must be different to the deploying project (CD). Violating this rule means you can't have "build once deploy many times", and I've never found a reason to not build once and deploy many times.
w
I'm currently just trying to work within a current setup, and this is the only thing like this at the moment so there's not a lot of uptake for breaking out into a whole new process. And the whole idea here is that the AMI is the build once, and then the deploy many times is the instances using it, so it's not a complete deviation from that pattern, just a slightly different implementation. (Also, the AMI does not pull from our codebase/github/etc, just has an AWS base and various commands that live only in the pulumi)
l
You can't deploy it tomorrow.. without rebuilding it tomorrow. But fair point, you can't make a change if no one is gong to pay for the time to make it 🙂
w
The AMI? Correct, we cannot deploy a new AMI (i.e. with changes) without rebuilding. But we can deploy new instances tomorrow based on the AMI we made today, no problem
s
Hi @witty-battery-42692, looks like getting the ami id with an apply and passing it on worked for me. Sometimes using the .apply() function can be easier with nested values like this
Copy code
// Extract the AMI ID from the ImageBuilder output
const customAmiId = image.outputResources.apply((resources) => {
  if (resources && resources.length > 0) {
    const firstResource = resources[0];
    if (
      "amis" in firstResource &&
      firstResource.amis &&
      firstResource.amis.length > 0
    ) {
      return firstResource.amis[0].image;
    }
  }
  throw new Error("No AMI found in ImageBuilder output");
});

// Create an EC2 instance using the custom AMI
const instance = new aws.ec2.Instance("custom-instance", {
  ami: customAmiId,
  instanceType: "t3.micro",
  subnetId: defaultSubnet.then((subnets) => subnets.ids[0]),
  tags: {
    Name: "Custom AMI Instance",
  },
});
👋 1
😎 1
w
Oh nice, thanks! I'd gotten partway there but hadn't quite nailed the syntax yet. Appreciate it!
🙌 1
a
@witty-battery-42692 Did it work out for you?
w
Yep, it works perfectly, thanks! 🙂
👍 1
Oh actually - realised a hitch today. If I'm working on other code and so that image isn't getting updated, this throws an error. So I'll have to play with that, since if it's not creating a new image in a given run I just need it to use the most recently created one