Skip to content

Commit e0e20dd

Browse files
author
Tarun Belani
committed
feat(imagebuilder-alpha): add support for Image Pipeline Construct
1 parent a6c0288 commit e0e20dd

28 files changed

+6513
-3
lines changed

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

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,285 @@ 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 Pipeline
40+
41+
An image pipeline provides the automation framework for building secure AMIs and container images. The pipeline orchestrates the entire image creation process by combining an image recipe or container recipe with infrastructure configuration and distribution configuration. Pipelines can run on a schedule or be triggered manually, and they manage the build, test, and distribution phases automatically.
42+
43+
#### Image Pipeline Basic Usage
44+
45+
Create a simple AMI pipeline with just a recipe:
46+
47+
```ts
48+
const imageRecipe = new imagebuilder.ImageRecipe(this, 'MyImageRecipe', {
49+
baseImage: imagebuilder.BaseImage.fromSsmParameterName(
50+
'/aws/service/ami-amazon-linux-latest/al2023-ami-minimal-kernel-default-x86_64'
51+
)
52+
});
53+
54+
const imagePipeline = new imagebuilder.ImagePipeline(this, 'MyImagePipeline', {
55+
recipe: exampleImageRecipe
56+
});
57+
```
58+
59+
Create a simple container pipeline:
60+
61+
```ts
62+
const containerRecipe = new imagebuilder.ContainerRecipe(this, 'MyContainerRecipe', {
63+
baseImage: imagebuilder.BaseContainerImage.fromDockerHub('amazonlinux', 'latest'),
64+
targetRepository: imagebuilder.Repository.fromEcr(
65+
ecr.Repository.fromRepositoryName(this, 'Repository', 'my-container-repo')
66+
)
67+
});
68+
69+
const containerPipeline = new imagebuilder.ImagePipeline(this, 'MyContainerPipeline', {
70+
recipe: exampleContainerRecipe
71+
});
72+
```
73+
74+
#### Image Pipeline Scheduling
75+
76+
##### Manual Pipeline Execution
77+
78+
Create a pipeline that runs only when manually triggered:
79+
80+
```ts
81+
const manualPipeline = new imagebuilder.ImagePipeline(this, 'ManualPipeline', {
82+
imagePipelineName: 'my-manual-pipeline',
83+
description: 'Pipeline triggered manually for production builds',
84+
recipe: exampleImageRecipe
85+
// No schedule property - manual execution only
86+
});
87+
88+
// Grant Lambda function permission to trigger the pipeline
89+
manualPipeline.grantStartExecution(role);
90+
```
91+
92+
##### Automated Pipeline Scheduling
93+
94+
Schedule a pipeline to run automatically using cron expressions:
95+
96+
```ts
97+
const weeklyPipeline = new imagebuilder.ImagePipeline(this, 'WeeklyPipeline', {
98+
imagePipelineName: 'weekly-build-pipeline',
99+
recipe: exampleImageRecipe,
100+
schedule: {
101+
expression: events.Schedule.cron({
102+
minute: '0',
103+
hour: '6',
104+
weekDay: 'MON'
105+
}),
106+
timezone: TimeZone.AMERICA_NEW_YORK
107+
}
108+
});
109+
```
110+
111+
Use rate expressions for regular intervals:
112+
113+
```ts
114+
const dailyPipeline = new imagebuilder.ImagePipeline(this, 'DailyPipeline', {
115+
recipe: exampleContainerRecipe,
116+
schedule: {
117+
expression: events.Schedule.rate(Duration.days(1))
118+
}
119+
});
120+
```
121+
122+
##### Pipeline Schedule Configuration
123+
124+
Configure advanced scheduling options:
125+
126+
```ts
127+
const advancedSchedulePipeline = new imagebuilder.ImagePipeline(this, 'AdvancedSchedulePipeline', {
128+
recipe: exampleImageRecipe,
129+
schedule: {
130+
expression: events.Schedule.rate(Duration.days(7)),
131+
timezone: TimeZone.PST8PDT,
132+
// Only trigger when dependencies are updated (new base images, components, etc.)
133+
startCondition: imagebuilder.ScheduleStartCondition.EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE,
134+
// Automatically disable after 3 consecutive failures
135+
autoDisableFailureCount: 3
136+
},
137+
// Start enabled
138+
enabled: true
139+
});
140+
```
141+
142+
#### Image Pipeline Configuration
143+
144+
##### Infrastructure and Distribution
145+
146+
Configure custom infrastructure and distribution settings:
147+
148+
```ts
149+
const infrastructureConfiguration = new imagebuilder.InfrastructureConfiguration(this, 'Infrastructure', {
150+
infrastructureConfigurationName: 'production-infrastructure',
151+
instanceTypes: [
152+
ec2.InstanceType.of(ec2.InstanceClass.COMPUTE7_INTEL, ec2.InstanceSize.LARGE)
153+
],
154+
vpc: vpc,
155+
subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }
156+
});
157+
158+
const distributionConfiguration = new imagebuilder.DistributionConfiguration(this, 'Distribution');
159+
distributionConfiguration.addAmiDistributions({
160+
amiName: 'production-ami-{{ imagebuilder:buildDate }}',
161+
amiTargetAccountIds: ['123456789012', '098765432109']
162+
});
163+
164+
const productionPipeline = new imagebuilder.ImagePipeline(this, 'ProductionPipeline', {
165+
recipe: exampleImageRecipe,
166+
infrastructureConfiguration: infrastructureConfiguration,
167+
distributionConfiguration: distributionConfiguration
168+
});
169+
```
170+
171+
##### Pipeline Logging Configuration
172+
173+
Configure custom CloudWatch log groups for pipeline and image logs:
174+
175+
```ts
176+
const pipelineLogGroup = new logs.LogGroup(this, 'PipelineLogGroup', {
177+
logGroupName: '/custom/imagebuilder/pipeline/logs',
178+
retention: logs.RetentionDays.ONE_MONTH
179+
});
180+
181+
const imageLogGroup = new logs.LogGroup(this, 'ImageLogGroup', {
182+
logGroupName: '/custom/imagebuilder/image/logs',
183+
retention: logs.RetentionDays.ONE_WEEK
184+
});
185+
186+
const loggedPipeline = new imagebuilder.ImagePipeline(this, 'LoggedPipeline', {
187+
recipe: exampleImageRecipe,
188+
imagePipelineLogGroup: pipelineLogGroup,
189+
imageLogGroup: imageLogGroup
190+
});
191+
```
192+
193+
##### Workflow Integration
194+
195+
Use AWS-managed workflows for common pipeline phases:
196+
197+
```ts
198+
const workflowPipeline = new imagebuilder.ImagePipeline(this, 'WorkflowPipeline', {
199+
recipe: exampleImageRecipe,
200+
workflows: [
201+
{ workflow: imagebuilder.AwsManagedWorkflow.buildImage(this, 'BuildWorkflow') },
202+
{ workflow: imagebuilder.AwsManagedWorkflow.testImage(this, 'TestWorkflow') }
203+
]
204+
});
205+
```
206+
207+
For container pipelines, use container-specific workflows:
208+
209+
```ts
210+
const containerWorkflowPipeline = new imagebuilder.ImagePipeline(this, 'ContainerWorkflowPipeline', {
211+
recipe: exampleContainerRecipe,
212+
workflows: [
213+
{ workflow: imagebuilder.AwsManagedWorkflow.buildContainer(this, 'BuildContainer') },
214+
{ workflow: imagebuilder.AwsManagedWorkflow.testContainer(this, 'TestContainer') },
215+
{ workflow: imagebuilder.AwsManagedWorkflow.distributeContainer(this, 'DistributeContainer') }
216+
]
217+
});
218+
```
219+
220+
##### Advanced Features
221+
222+
Configure image scanning for container pipelines:
223+
224+
```ts
225+
const scanningRepository = new ecr.Repository(this, 'ScanningRepo');
226+
227+
const scannedContainerPipeline = new imagebuilder.ImagePipeline(this, 'ScannedContainerPipeline', {
228+
recipe: exampleContainerRecipe,
229+
imageScanningEnabled: true,
230+
imageScanningEcrRepository: scanningRepository,
231+
imageScanningEcrTags: ['security-scan', 'latest']
232+
});
233+
```
234+
235+
Control metadata collection and testing:
236+
237+
```ts
238+
const controlledPipeline = new imagebuilder.ImagePipeline(this, 'ControlledPipeline', {
239+
recipe: exampleImageRecipe,
240+
enhancedImageMetadataEnabled: true, // Collect detailed OS and package info
241+
imageTestsEnabled: false // Skip testing phase for faster builds
242+
});
243+
```
244+
245+
#### Image Pipeline Events
246+
247+
##### Build State Monitoring
248+
249+
Monitor pipeline execution with EventBridge rules:
250+
251+
```ts
252+
// Monitor all pipeline events
253+
examplePipeline.onEvent('AllPipelineEvents', {
254+
target: new targets.LambdaFunction(lambdaFunction)
255+
});
256+
257+
// Monitor build state changes
258+
examplePipeline.onImageBuildStateChange('BuildStateChanges', {
259+
target: new targets.SnsTopic(topic)
260+
});
261+
262+
// Monitor build failures for alerts
263+
examplePipeline.onImageBuildFailed('BuildFailureAlert', {
264+
target: new targets.SqsQueue(queue)
265+
});
266+
```
267+
268+
##### Pipeline Event Handling
269+
270+
Handle specific pipeline events:
271+
272+
```ts
273+
// React to successful builds
274+
examplePipeline.onImageBuildSuccess('BuildSuccessHandler', {
275+
target: new targets.LambdaFunction(lambdaFunction),
276+
description: 'Trigger deployment after successful build'
277+
});
278+
279+
// Monitor CVE detection
280+
examplePipeline.onCVEDetected('CVEAlert', {
281+
target: new targets.SnsTopic(topic)
282+
});
283+
284+
// Handle pipeline auto-disable events
285+
examplePipeline.onImagePipelineAutoDisabled('PipelineDisabledAlert', {
286+
target: new targets.LambdaFunction(lambdaFunction)
287+
});
288+
```
289+
290+
#### Importing Image Pipelines
291+
292+
Reference existing pipelines created outside of CDK:
293+
294+
```ts
295+
// Import by name
296+
const existingPipelineByName = imagebuilder.ImagePipeline.fromImagePipelineName(
297+
this,
298+
'ExistingPipelineByName',
299+
'my-existing-pipeline'
300+
);
301+
302+
// Import by ARN
303+
const existingPipelineByArn = imagebuilder.ImagePipeline.fromImagePipelineArn(
304+
this,
305+
'ExistingPipelineByArn',
306+
'arn:aws:imagebuilder:us-east-1:123456789012:image-pipeline/imported-pipeline'
307+
);
308+
309+
// Grant permissions to imported pipelines
310+
const automationRole = new iam.Role(this, 'AutomationRole', {
311+
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
312+
});
313+
314+
existingPipelineByName.grantStartExecution(automationRole);
315+
existingPipelineByArn.grantRead(role);
316+
```
317+
39318
### Image Recipe
40319

41320
#### Image Recipe Basic Usage

0 commit comments

Comments
 (0)