Ben hai 3 meses
achega
67aa74b6a3
Modificáronse 5 ficheiros con 151 adicións e 0 borrados
  1. 35 0
      component.yaml
  2. 43 0
      distribution.yaml
  3. 42 0
      pipeline.yaml
  4. 21 0
      recipe.yaml
  5. 10 0
      setup.sh

+ 35 - 0
component.yaml

@@ -0,0 +1,35 @@
+name: "MyCustomLinuxComponent"
+description: "A custom Image Builder component for Linux with shell commands."
+schemaVersion: 1.0
+phases:
+  - name: build
+    steps:
+      - name: InstallPackages
+        action: ExecuteBash
+        inputs:
+          commands:
+            - "sudo yum update -y"
+            - "sudo yum install -y httpd"
+            - "sudo systemctl enable httpd"
+            - "sudo systemctl start httpd"
+        onFailure: "Abort" # Option to abort the build on failure
+      - name: ConfigureWebServer
+        action: ExecuteBash
+        inputs:
+          commands:
+            - "echo 'Hello from Image Builder!' | sudo tee /var/www/html/index.html"
+        onFailure: "Continue" # Option to continue even if this step fails
+  - name: test
+    steps:
+      - name: VerifyWebServer
+        action: ExecuteBash
+        inputs:
+          commands:
+            - "curl -s http://localhost | grep 'Hello from Image Builder!'"
+        onFailure: "Abort"
+      - name: CheckServiceStatus
+        action: ExecuteBash
+        inputs:
+          commands:
+            - "sudo systemctl is-active httpd"
+        onFailure: "Abort"

+ 43 - 0
distribution.yaml

@@ -0,0 +1,43 @@
+AWSTemplateFormatVersion: '2010-09-09'
+Description: Image Builder Distribution Configuration Example
+
+Resources:
+  MyDistributionConfiguration:
+    Type: AWS::ImageBuilder::DistributionConfiguration
+    Properties:
+      Name: MyExampleDistributionConfig
+      Description: Distributes the custom AMI to multiple regions and accounts.
+      Distributions:
+        - Region: us-east-1
+          AmiDistributionConfiguration:
+            Name: MyCustomAMI-{{imagebuilder:buildDate}}
+            Description: Custom AMI for us-east-1
+            AmiTags:
+              Project: MyProject
+              Environment: Production
+            LaunchPermission:
+              UserIds:
+                - '123456789012' # An AWS account ID to share the AMI with
+              OrganizationalUnitIds:
+                - 'ou-xxxxxxxxxxxx' # An Organizational Unit ID to share the AMI with
+        - Region: eu-west-1
+          AmiDistributionConfiguration:
+            Name: MyCustomAMI-{{imagebuilder:buildVersion}}
+            Description: Custom AMI for eu-west-1
+            AmiTags:
+              Project: MyProject
+              Environment: Production
+            LaunchPermission:
+              OrganizationalUnitIds:
+                - 'ou-yyyyyyyyyyyy' # Another Organizational Unit ID
+              OrganizationArns:
+                - 'arn:aws:organizations::123456789012:organization/o-xxxxxxxxxx' # An AWS Organization ARN
+        - Region: ap-southeast-2
+          AmiDistributionConfiguration:
+            Name: MyCustomAMI-{{imagebuilder:buildDate}}-AP
+            Description: Custom AMI for ap-southeast-2 with S3 export
+            S3ExportConfiguration:
+              RoleName: MyImageBuilderS3ExportRole # IAM Role for S3 export permissions
+              DiskImageFormat: VHD # or VMDK, RAW
+              S3Bucket: my-imagebuilder-export-bucket
+              S3Prefix: exported-amis/

+ 42 - 0
pipeline.yaml

@@ -0,0 +1,42 @@
+---
+name: MyCustomBuildWorkflow
+description: A custom build workflow to install updates and configure a web server.
+schemaVersion: 1.0
+
+parameters:
+  - name: BaseImageAMI
+    type: String
+    description: The AMI to use as the base image for the build.
+  - name: WebServerPort
+    type: String
+    description: The port for the web server.
+    default: "80"
+
+steps:
+  - name: InstallUpdates
+    action: ExecuteBash
+    inputs:
+      commands:
+        - "sudo yum update -y"
+        - "sudo yum install -y httpd"
+    onFailure: Abort
+
+  - name: ConfigureWebServer
+    action: ExecuteBash
+    inputs:
+      commands:
+        - "echo 'Hello from Image Builder!' | sudo tee /var/www/html/index.html"
+        - "sudo systemctl enable httpd"
+        - "sudo systemctl start httpd"
+    onFailure: Abort
+
+  - name: TestWebServer
+    action: ExecuteBash
+    inputs:
+      commands:
+        - "curl localhost:{{ WebServerPort }}"
+    onFailure: Abort
+
+outputs:
+  - name: BuiltImageArn
+    value: "{{ image.output.arn }}"

+ 21 - 0
recipe.yaml

@@ -0,0 +1,21 @@
+AWSTemplateFormatVersion: '2010-09-09'
+Description: Image Builder Recipe for a custom AMI with Nginx
+Resources:
+  MyImageRecipe:
+    Type: AWS::ImageBuilder::ImageRecipe
+    Properties:
+      Name: MyNginxRecipe
+      Description: Recipe to build an AMI with Nginx installed
+      Components:
+        - ComponentArn: arn:aws:imagebuilder:us-east-1:aws:component/amazon-linux-2-nginx-install/x.x.x # Replace x.x.x with a valid version
+        - ComponentArn: arn:aws:imagebuilder:us-east-1:aws:component/amazon-linux-2-nginx-configure/x.x.x # Replace x.x.x with a valid version
+      ParentImage: arn:aws:imagebuilder:us-east-1:aws:image/amazon-linux-2-x86/x.x.x # Replace x.x.x with a valid version
+      BlockDeviceMappings:
+        - DeviceName: /dev/xvda
+          Ebs:
+            VolumeSize: 30
+            VolumeType: gp2
+            DeleteOnTermination: true
+      Tags:
+        Project: MyCustomAMI
+        Application: NginxWebServer

+ 10 - 0
setup.sh

@@ -0,0 +1,10 @@
+aws imagebuilder create-component --cli-input-yaml file://component.json
+
+aws imagebuilder create-image-recipe --cli-input-yaml file://recipe.json
+
+aws imagebuilder create-distribution-configuration --cli-input-yaml file://distribution.json
+
+aws imagebuilder create-image-pipeline --cli-input-yaml file://pipeline.json
+
+# Example: Manually run an image pipeline
+aws imagebuilder start-image-pipeline-execution --image-pipeline-arn arn:aws:imagebuilder:your-region:your-account-id:image-pipeline/your-pipeline-name