How to create a custom component in AWS Image Builder?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we will discuss how to create a custom component in AWS Image Builder service.
What is a component in AWS Image Builder?
A component defines the sequence of steps required to either customize an instance prior to image creation (a build component), or to test an instance that was launched from the created image (a test component).
A component is created from a declarative, plain-text YAML or JSON document that describes the runtime configuration for building and validating, or testing an instance that is produced by your pipeline. Components run on the instance using a component management application. The component management application parses the documents and runs the desired steps.
After they are created, one or more components are grouped together using an image recipe or container recipe to define the plan for building and testing a virtual machine or container image. You can use public components that are owned and managed by AWS, or you can create your own.
You define the component in an YAML or JSON file called component document that describes configuration for a customization you can apply to your image. The document is used to create a build or test component.
How to create a custom component in AWS Image Builder?
Prerequisites:
AWS CLI installed and configured.
Step 1: List all available AWS Image builder components
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## List all available components aws imagebuilder list-components ## List all available components owned by AWS aws imagebuilder list-components \ --owner Amazon | jq -r .componentVersionList[].name ## List all available components owned by You aws imagebuilder list-components \ --owner Self | jq -r .componentVersionList[].name ## List all available components shared with You aws imagebuilder list-components \ --owner Shared | jq -r .componentVersionList[].name |
Step 2: Create a custom component document file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
## Create a component document cat << 'EOF' > component_config.yaml name: HelloWorld description: Hello World App schemaVersion: 1.0 phases: - name: build steps: - name: UpdateOS action: ExecuteBash inputs: commands: - sudo yum update -y - name: InstallWebServer action: ExecuteBash inputs: commands: - sudo yum install httpd -y - sudo systemctl start httpd - sudo systemctl is-enabled httpd - echo "hello world v1" > /var/www/html/index.html - curl -s localhost - name: validate steps: - name: ValidateWebServer action: ExecuteBash inputs: commands: - | CUR_STATE=$(sudo systemctl is-active httpd) if [[ $CUR_STATE == "active" ]]; then echo "Httpd service is active." exit 0 else echo "Httpd service is not active." exit 1 fi - name: TestWebServer action: ExecuteBash inputs: commands: - | OUTPUT=$(curl -s localhost) if [[ $OUTPUT == "hello world v1" ]]; then echo "Webserver is working fine" exit 0 else echo "Webserver not working fine" exit 0 fi EOF |
Step 3: Create an S3 bucket with proper bucket policy and upload the custom component document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
## Create an S3 bucket aws s3api create-bucket \ --bucket cloudaffaire-image-builder \ --create-bucket-configuration LocationConstraint=ap-south-1 ## Get S3 bucket ARN and AWS Account ID and ARN S3_BUCKET_ARN='arn:aws:s3:::cloudaffaire-image-builder' && ACCOUNT_ARN=$(aws sts get-caller-identity | jq -r .Arn) && ACCOUNT_ID=$(aws sts get-caller-identity | jq -r .Account) && IAM_ROLE_ARN=arn:aws:iam::$ACCOUNT_ID:role/HelloWorldIAMRole ## Create a s3 bucket policy definition file cat << EOF > bucket_policy_config.json { "Version": "2012-10-17", "Statement": [ { "Sid": "HelloWorldPolicy", "Effect": "Allow", "Principal": "*", "Action": "s3:*", "Resource": ["$S3_BUCKET_ARN/*"], "Condition": { "StringEquals": { "aws:SourceAccount": "$ACCOUNT_ID", "s3:x-amz-acl": "bucket-owner-full-control" } } } ] } EOF ## Create a s3 bucket policy aws s3api put-bucket-policy \ --bucket cloudaffaire-image-builder \ --policy file://bucket_policy.json ## Upload component document in S3 bucket aws s3 cp hello_world.yaml \ s3://cloudaffaire-image-builder/component_config.yaml |
Step 4: Create a custom component in AWS Image builder using AWS CLI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Create custom component config def cat << 'EOF' > image_component_config.json { "name": "HelloWorldComponent", "semanticVersion": "1.0.0", "description": "Hello World App", "changeDescription": "Initial version.", "platform": "Linux", "uri": "s3://cloudaffaire-image-builder/component_config.yaml", "tags": { "App": "Hello World" } } EOF ## Create the custom component aws imagebuilder create-component \ --cli-input-json file://image_component_config.json |
Step 5: Get details on the custom component in AWS Image Builder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
## List all available components owned by You aws imagebuilder list-components \ --owner Self ## List component build version COMPONENT_VERSION_ARN=$(aws imagebuilder list-components \ --owner Self | jq -r .componentVersionList[].arn) && COMPONENT_BUILD_VERSION_ARN=$(aws imagebuilder list-component-build-versions \ --component-version-arn $COMPONENT_VERSION_ARN | jq -r .componentSummaryList[].arn) && aws imagebuilder list-component-build-versions \ --component-version-arn $COMPONENT_VERSION_ARN ## Get custom component details aws imagebuilder get-component \ --component-build-version-arn $COMPONENT_BUILD_VERSION_ARN |
You can also create and get the custom component details in the AWS management console.
Step 6: Clean up.
1 2 3 4 5 6 7 |
## Delete the custom component aws imagebuilder delete-component \ --component-build-version-arn $COMPONENT_BUILD_VERSION_ARN ## Delete the S3 bucket with objects aws s3 rb \ s3://cloudaffaire-image-builder --force |
Hope you have enjoyed this article. To get more details in AWS Image Builder, please refer the below documentation.
https://docs.aws.amazon.com/imagebuilder/index.html