4343
4444 - name : Get latest Ubuntu AMI
4545 run : |
46- # Get the latest Ubuntu 22.04 AMI ID
4746 UBUNTU_AMI=$(aws ec2 describe-images \
4847 --owners 099720109477 \
4948 --filters \
@@ -126,7 +125,6 @@ jobs:
126125
127126 - name : Get latest Ubuntu AMI
128127 run : |
129- # Get the latest Ubuntu 22.04 AMI ID
130128 UBUNTU_AMI=$(aws ec2 describe-images \
131129 --owners 099720109477 \
132130 --filters \
@@ -142,7 +140,6 @@ jobs:
142140 run : |
143141 echo "🚀 Starting EC2 Image Builder with AMI: $UBUNTU_AMI"
144142
145- # Create component
146143 cat > component.yml << 'EOF'
147144 name: InstallNginx
148145 description: Install Nginx web server
@@ -173,7 +170,6 @@ jobs:
173170
174171 echo "✅ Component created: $COMPONENT_ARN"
175172
176- # Create image recipe with the correct AMI ID
177173 RECIPE_ARN=$(aws imagebuilder create-image-recipe \
178174 --name "nginx-recipe-$(date +%s)" \
179175 --semantic-version "1.0.0" \
@@ -185,7 +181,6 @@ jobs:
185181
186182 echo "✅ Recipe created: $RECIPE_ARN"
187183
188- # Create infrastructure config
189184 INFRA_ARN=$(aws imagebuilder create-infrastructure-configuration \
190185 --name "basic-infra-$(date +%s)" \
191186 --instance-types t3.micro \
@@ -194,7 +189,6 @@ jobs:
194189
195190 echo "✅ Infrastructure config created: $INFRA_ARN"
196191
197- # Start build
198192 IMAGE_ARN=$(aws imagebuilder create-image \
199193 --image-recipe-arn "$RECIPE_ARN" \
200194 --infrastructure-configuration-arn "$INFRA_ARN" \
@@ -205,263 +199,6 @@ jobs:
205199 echo "Build Summary:" > ec2-imagebuilder-build.log
206200 echo "Base AMI: $UBUNTU_AMI" >> ec2-imagebuilder-build.log
207201 echo "Image ARN: $IMAGE_ARN" >> ec2-imagebuilder-build.log
208- echo "Recipe ARN: $RECIPE_ARN" >> ec2-imagebuilder-build.log
209- echo "Component ARN: $COMPONENT_ARN" >> ec2-imagebuilder-build.log
210-
211- - name : Upload EC2 Image Builder logs
212- uses : actions/upload-artifact@v4
213- with :
214- name : ec2-imagebuilder-logs
215- path : ec2-imagebuilder-build.log
216-
217- comparison-summary :
218- name : Build Comparison Summary
219- runs-on : ubuntu-latest
220- needs : [packer-build, ec2-imagebuilder-build]
221- if : always()
222-
223- steps :
224- - name : Generate comparison report
225- run : |
226- echo "# Image Builder Comparison Report" > comparison-report.md
227- echo "" >> comparison-report.md
228- echo "## Build Results" >> comparison-report.md
229- echo "" >> comparison-report.md
230-
231- if [ "${{ needs.packer-build.result }}" = "success" ]; then
232- echo "✅ **HashiCorp Packer**: Build completed successfully" >> comparison-report.md
233- else
234- echo "❌ **HashiCorp Packer**: Build failed or skipped" >> comparison-report.md
235- fi
236-
237- if [ "${{ needs.ec2-imagebuilder-build.result }}" = "success" ]; then
238- echo "✅ **AWS EC2 Image Builder**: Build completed successfully" >> comparison-report.md
239- else
240- echo "❌ **AWS EC2 Image Builder**: Build failed or skipped" >> comparison-report.md
241- fi
242-
243- echo "" >> comparison-report.md
244- echo "## Key Differences Observed" >> comparison-report.md
245- echo "- **Packer**: Uses direct AMI ID, faster execution" >> comparison-report.md
246- echo "- **EC2 Image Builder**: More verbose setup, better AWS integration" >> comparison-report.md
247- echo "" >> comparison-report.md
248- echo "Build completed at: $(date)" >> comparison-report.md
249-
250- cat comparison-report.md
251-
252- - name : Upload comparison report
253- uses : actions/upload-artifact@v4
254- with :
255- name : comparison-report
256- path : comparison-report.mdname: Image Builders Comparison
257-
258- on :
259- workflow_dispatch :
260- inputs :
261- run_packer :
262- description : ' Run Packer build'
263- type : boolean
264- default : true
265- run_ec2_imagebuilder :
266- description : ' Run EC2 Image Builder'
267- type : boolean
268- default : true
269-
270- env :
271- AWS_REGION : us-west-2
272-
273- permissions :
274- id-token : write
275- contents : read
276-
277- jobs :
278- packer-build :
279- name : HashiCorp Packer Build
280- runs-on : ubuntu-latest
281- if : ${{ github.event.inputs.run_packer == 'true' }}
282-
283- steps :
284- - name : Checkout
285- uses : actions/checkout@v4
286-
287- - name : Configure AWS credentials
288- uses : aws-actions/configure-aws-credentials@v4
289- with :
290- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
291- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
292- aws-region : ${{ env.AWS_REGION }}
293-
294- - name : Setup Packer
295- uses : hashicorp/setup-packer@main
296- with :
297- version : " 1.9.4"
298-
299- - name : Get latest Ubuntu AMI
300- run : |
301- # Get the latest Ubuntu 22.04 AMI ID
302- UBUNTU_AMI=$(aws ec2 describe-images \
303- --owners 099720109477 \
304- --filters \
305- "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*" \
306- "Name=state,Values=available" \
307- --query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' \
308- --output text)
309-
310- echo "Found Ubuntu AMI: $UBUNTU_AMI"
311- echo "UBUNTU_AMI=$UBUNTU_AMI" >> $GITHUB_ENV
312-
313- - name : Create Packer template
314- run : |
315- cat > packer-template.pkr.hcl << EOF
316- packer {
317- required_plugins {
318- amazon = {
319- source = "github.com/hashicorp/amazon"
320- version = "~> 1"
321- }
322- }
323- }
324-
325- source "amazon-ebs" "ubuntu" {
326- ami_name = "packer-demo-\${timestamp()}"
327- instance_type = "t3.micro"
328- region = "${{ env.AWS_REGION }}"
329- source_ami = "${{ env.UBUNTU_AMI }}"
330- ssh_username = "ubuntu"
331-
332- tags = {
333- Name = "packer-demo-\${timestamp()}"
334- Tool = "HashiCorp-Packer"
335- }
336- }
337-
338- build {
339- sources = ["source.amazon-ebs.ubuntu"]
340-
341- provisioner "shell" {
342- inline = [
343- "sudo apt-get update",
344- "sudo apt-get install -y nginx",
345- "sudo systemctl enable nginx",
346- "echo '<h1>Built with HashiCorp Packer</h1>' | sudo tee /var/www/html/index.html"
347- ]
348- }
349- }
350- EOF
351-
352- - name : Initialize and build with Packer
353- run : |
354- echo "🚀 Starting Packer build with AMI: $UBUNTU_AMI"
355- packer init packer-template.pkr.hcl
356- packer validate packer-template.pkr.hcl
357- packer build packer-template.pkr.hcl | tee packer-build.log
358- echo "✅ Packer build completed!"
359-
360- - name : Upload Packer logs
361- uses : actions/upload-artifact@v4
362- with :
363- name : packer-build-logs
364- path : packer-build.log
365-
366- ec2-imagebuilder-build :
367- name : AWS EC2 Image Builder Build
368- runs-on : ubuntu-latest
369- if : ${{ github.event.inputs.run_ec2_imagebuilder == 'true' }}
370-
371- steps :
372- - name : Checkout
373- uses : actions/checkout@v4
374-
375- - name : Configure AWS credentials
376- uses : aws-actions/configure-aws-credentials@v4
377- with :
378- aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
379- aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
380- aws-region : ${{ env.AWS_REGION }}
381-
382- - name : Get latest Ubuntu AMI
383- run : |
384- # Get the latest Ubuntu 22.04 AMI ID
385- UBUNTU_AMI=$(aws ec2 describe-images \
386- --owners 099720109477 \
387- --filters \
388- "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-22.04-amd64-server-*" \
389- "Name=state,Values=available" \
390- --query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' \
391- --output text)
392-
393- echo "Found Ubuntu AMI: $UBUNTU_AMI"
394- echo "UBUNTU_AMI=$UBUNTU_AMI" >> $GITHUB_ENV
395-
396- - name : Run EC2 Image Builder pipeline
397- run : |
398- echo "🚀 Starting EC2 Image Builder with AMI: $UBUNTU_AMI"
399-
400- # Create component
401- cat > component.yml << 'EOF'
402- name: InstallNginx
403- description: Install Nginx web server
404- schemaVersion: 1.0
405- phases:
406- - name: build
407- steps:
408- - name: UpdateOS
409- action: UpdateOS
410- - name: InstallNginx
411- action: ExecuteBash
412- inputs:
413- commands:
414- - apt-get update
415- - apt-get install -y nginx
416- - systemctl enable nginx
417- - echo '<h1>Built with EC2 Image Builder</h1>' > /var/www/html/index.html
418- EOF
419-
420- COMPONENT_ARN=$(aws imagebuilder create-component \
421- --name "nginx-component-$(date +%s)" \
422- --semantic-version "1.0.0" \
423- --description "Install Nginx" \
424- --platform Linux \
425- --data file://component.yml \
426- --query 'componentBuildVersionArn' \
427- --output text)
428-
429- echo "✅ Component created: $COMPONENT_ARN"
430-
431- # Create image recipe with the correct AMI ID
432- RECIPE_ARN=$(aws imagebuilder create-image-recipe \
433- --name "nginx-recipe-$(date +%s)" \
434- --semantic-version "1.0.0" \
435- --description "Ubuntu with Nginx" \
436- --parent-image "$UBUNTU_AMI" \
437- --components componentArn=$COMPONENT_ARN \
438- --query 'imageRecipeArn' \
439- --output text)
440-
441- echo "✅ Recipe created: $RECIPE_ARN"
442-
443- # Create infrastructure config
444- INFRA_ARN=$(aws imagebuilder create-infrastructure-configuration \
445- --name "basic-infra-$(date +%s)" \
446- --instance-types t3.micro \
447- --query 'infrastructureConfigurationArn' \
448- --output text)
449-
450- echo "✅ Infrastructure config created: $INFRA_ARN"
451-
452- # Start build
453- IMAGE_ARN=$(aws imagebuilder create-image \
454- --image-recipe-arn "$RECIPE_ARN" \
455- --infrastructure-configuration-arn "$INFRA_ARN" \
456- --query 'imageBuildVersionArn' \
457- --output text)
458-
459- echo "✅ Image build started: $IMAGE_ARN"
460- echo "Build Summary:" > ec2-imagebuilder-build.log
461- echo "Base AMI: $UBUNTU_AMI" >> ec2-imagebuilder-build.log
462- echo "Image ARN: $IMAGE_ARN" >> ec2-imagebuilder-build.log
463- echo "Recipe ARN: $RECIPE_ARN" >> ec2-imagebuilder-build.log
464- echo "Component ARN: $COMPONENT_ARN" >> ec2-imagebuilder-build.log
465202
466203 - name : Upload EC2 Image Builder logs
467204 uses : actions/upload-artifact@v4
0 commit comments