Skip to content

Commit 56b4365

Browse files
author
Tarun Belani
committed
feat(imagebuilder-alpha): add support for Image Recipe Construct
1 parent 21fd959 commit 56b4365

25 files changed

+3001
-0
lines changed

packages/@aws-cdk/aws-imagebuilder-alpha/README.md

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,200 @@ EC2 Image Builder supports AWS-managed components for common tasks, AWS Marketpl
3636
that you create. Components run during specific workflow phases: build and validate phases during the build stage, and
3737
test phase during the test stage.
3838

39+
### Image Recipe
40+
41+
An image recipe is a document that defines the base AMI and the components applied to produce the desired configuration
42+
for the output AMI. You can use image recipes to duplicate builds and maintain versioned, shareable configurations.
43+
Image recipes specify the base image, build components for customization, test components for validation, as well as
44+
other customizations to make to the output image, such as the block devices associated with the output AMI.
45+
46+
#### Image Recipe Basic Usage
47+
48+
Create an image recipe with the required base image:
49+
50+
```ts
51+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'MyImageRecipe', {
52+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
53+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
54+
)
55+
});
56+
```
57+
58+
#### Image Recipe Base Images
59+
60+
##### SSM Parameters
61+
62+
Using SSM parameter references:
63+
64+
```ts
65+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'SsmImageRecipe', {
66+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
67+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
68+
)
69+
});
70+
71+
// Using an SSM parameter construct
72+
const parameter = ssm.StringParameter.fromStringParameterName(
73+
this,
74+
'BaseImageParameter',
75+
'/aws/service/ami-windows-latest/Windows_Server-2022-English-Full-Base'
76+
);
77+
const windowsRecipe = new imagebuilder.ImageRecipe(this, 'WindowsImageRecipe', {
78+
baseImage: imagebuilder.BaseImage.fromSsmParameter(parameter)
79+
});
80+
```
81+
82+
##### AMI IDs
83+
84+
When you have a specific AMI to use:
85+
86+
```ts
87+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'AmiImageRecipe', {
88+
baseImage: imagebuilder.BaseImage.fromAmiId('ami-12345678')
89+
});
90+
```
91+
92+
##### Marketplace Images
93+
94+
For marketplace base images:
95+
96+
```ts
97+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'MarketplaceImageRecipe', {
98+
baseImage: imagebuilder.BaseImage.fromMarketplaceProductId('prod-1234567890abcdef0')
99+
});
100+
```
101+
102+
#### Image Recipe Components
103+
104+
##### Custom Components in Image Recipes
105+
106+
Add your own components to the recipe:
107+
108+
```ts
109+
const customComponent = new imagebuilder.Component(this, 'MyComponent', {
110+
platform: imagebuilder.Platform.LINUX,
111+
data: imagebuilder.ComponentData.fromJsonObject({
112+
schemaVersion: imagebuilder.ComponentSchemaVersion.V1_0,
113+
phases: [
114+
{
115+
name: imagebuilder.ComponentPhaseName.BUILD,
116+
steps: [
117+
{
118+
name: 'install-app',
119+
action: imagebuilder.ComponentAction.EXECUTE_BASH,
120+
inputs: {
121+
commands: ['yum install -y my-application']
122+
}
123+
}
124+
]
125+
}
126+
]
127+
})
128+
});
129+
130+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'ComponentImageRecipe', {
131+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
132+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
133+
),
134+
components: [
135+
{
136+
component: customComponent
137+
}
138+
]
139+
});
140+
```
141+
142+
##### AWS-Managed Components in Image Recipes
143+
144+
Use pre-built AWS components:
145+
146+
```ts
147+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'AwsManagedImageRecipe', {
148+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
149+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
150+
),
151+
components: [
152+
{
153+
component: imagebuilder.AwsManagedComponent.updateOS(this, 'UpdateOS', {
154+
platform: imagebuilder.Platform.LINUX
155+
})
156+
},
157+
{
158+
component: imagebuilder.AwsManagedComponent.awsCliV2(this, 'AwsCli', {
159+
platform: imagebuilder.Platform.LINUX
160+
})
161+
}
162+
]
163+
});
164+
```
165+
166+
##### Component Parameters in Image Recipes
167+
168+
Pass parameters to components that accept them:
169+
170+
```ts
171+
const parameterizedComponent = imagebuilder.Component.fromComponentName(
172+
this,
173+
'ParameterizedComponent',
174+
'my-parameterized-component'
175+
);
176+
177+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'ParameterizedImageRecipe', {
178+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
179+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
180+
),
181+
components: [
182+
{
183+
component: parameterizedComponent,
184+
parameters: {
185+
environment: imagebuilder.ComponentParameterValue.fromString('production'),
186+
version: imagebuilder.ComponentParameterValue.fromString('1.0.0')
187+
}
188+
}
189+
]
190+
});
191+
```
192+
193+
#### Image Recipe Configuration
194+
195+
##### Block Device Configuration
196+
197+
Configure storage for the build instance:
198+
199+
```ts
200+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'BlockDeviceImageRecipe', {
201+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
202+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
203+
),
204+
blockDevices: [
205+
{
206+
deviceName: '/dev/sda1',
207+
volume: ec2.BlockDeviceVolume.ebs(100, {
208+
encrypted: true,
209+
volumeType: ec2.EbsDeviceVolumeType.GENERAL_PURPOSE_SSD_GP3
210+
})
211+
}
212+
]
213+
});
214+
```
215+
216+
##### AMI Tagging
217+
218+
Tag the output AMI:
219+
220+
```ts
221+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'TaggedImageRecipe', {
222+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
223+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
224+
),
225+
amiTags: {
226+
Environment: 'Production',
227+
Application: 'WebServer',
228+
Owner: 'DevOps Team'
229+
}
230+
});
231+
```
232+
39233
### Component
40234

41235
A component defines the sequence of steps required to customize an instance during image creation (build component) or
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as ssm from 'aws-cdk-lib/aws-ssm';
2+
3+
/**
4+
* Represents a base image that is used to start from in EC2 Image Builder image builds
5+
*/
6+
export class BaseImage {
7+
/**
8+
* The AMI ID to use as a base image in an image recipe
9+
*
10+
* @param amiId The AMI ID to use as the base image
11+
*/
12+
public static fromAmiId(amiId: string): BaseImage {
13+
return new BaseImage(amiId);
14+
}
15+
16+
/**
17+
* TODO: Uncomment once the Image construct is implemented
18+
*
19+
* The EC2 Image Builder image to use as the base image in an image recipe
20+
*
21+
* @param image The EC2 Image Builder image to use as the base image
22+
*/
23+
// public static fromImage(image: IImage): BaseImage {
24+
// return new BaseImage(image.imageArn);
25+
// }
26+
27+
/**
28+
* The marketplace product ID for an AMI product to use as the base image in an image recipe
29+
*
30+
* @param productId The Marketplace AMI product ID to use as the base image
31+
*/
32+
public static fromMarketplaceProductId(productId: string): BaseImage {
33+
return new BaseImage(productId);
34+
}
35+
36+
/**
37+
* The SSM parameter to use as the base image in an image recipe
38+
*
39+
* @param parameter The SSM parameter to use as the base image
40+
*/
41+
public static fromSsmParameter(parameter: ssm.IStringParameter): BaseImage {
42+
return this.fromSsmParameterName(parameter.parameterName);
43+
}
44+
45+
/**
46+
* The parameter name for the SSM parameter to use as the base image in an image recipe
47+
*
48+
* @param parameterName The name of the SSM parameter to use as the base image
49+
*/
50+
public static fromSsmParameterName(parameterName: string): BaseImage {
51+
return new BaseImage(`ssm:${parameterName}`);
52+
}
53+
54+
/**
55+
* The string value of the base image to use in an image recipe
56+
*
57+
* @param string The base image as a direct string value
58+
*/
59+
public static fromString(string: string): BaseImage {
60+
return new BaseImage(string);
61+
}
62+
63+
/**
64+
* The rendered base image to use
65+
**/
66+
public readonly image: string;
67+
68+
protected constructor(image: string) {
69+
this.image = image;
70+
}
71+
}

0 commit comments

Comments
 (0)