|
| 1 | +# **Build Your Own Machine Image** |
| 2 | + |
| 3 | +## **Introduction** |
| 4 | +This guide shows how to build your own Amazon® Machine Image (AMI) using the same scripts that form the basis of the build process for MathWorks® prebuilt images. |
| 5 | +You can use the scripts to install MATLAB® Parallel Server™, MATLAB toolboxes, and the other features detailed below. |
| 6 | + |
| 7 | +A HashiCorp® Packer template generates the machine image. |
| 8 | +The template is an HCL2 file that tells Packer which plugins (builders, provisioners, post-processors) to use, how to configure each of those plugins, and what order to run them in. |
| 9 | +For more information about templates, see [Packer Templates](https://www.packer.io/docs/templates#packer-templates). |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## **Requirements** |
| 14 | +Before starting, you will need: |
| 15 | +* A valid Packer installation later than 1.7.0. For more information, see [Install Packer](https://www.packer.io/downloads). |
| 16 | +* AWS credentials with sufficient permission. For more information, see [Packer Authentication](https://www.packer.io/plugins/builders/amazon#authentication). |
| 17 | + |
| 18 | +## **Costs** |
| 19 | +You are responsible for the cost of the AWS services used when you create cloud resources using this guide. Resource settings, such as instance type, will affect the cost of deployment. For cost estimates, see the pricing pages for each AWS service you will be using. Prices are subject to change. |
| 20 | + |
| 21 | +## **Quick Launch Instructions** |
| 22 | +This section shows how to build the latest MATLAB Parallel Server machine image in your own AWS account. |
| 23 | + |
| 24 | +Pull the source code and navigate to the Packer folder. |
| 25 | +```bash |
| 26 | +git clone https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws |
| 27 | +cd matlab-parallel-server-on-aws/packer/v1 |
| 28 | +``` |
| 29 | + |
| 30 | +Initialize Packer to install the required plugins. |
| 31 | +You only need to do this once. |
| 32 | +For more information, see [init command reference (Packer)](https://developer.hashicorp.com/packer/docs/commands/init). |
| 33 | +```bash |
| 34 | +packer init build-parallel-server-ami.pkr.hcl |
| 35 | +``` |
| 36 | + |
| 37 | +Launch the Packer build with the default settings. |
| 38 | +```bash |
| 39 | +packer build build-parallel-server-ami.pkr.hcl |
| 40 | +``` |
| 41 | +Packer writes its output, including the ID of the generated machine image, to a `manifest.json` file at the end of the build. |
| 42 | +To use the built image with a MathWorks CloudFormation template, see [Deploy Machine Image](#deploy-machine-image). |
| 43 | + |
| 44 | + |
| 45 | +## **How to Run the Packer Build** |
| 46 | +This section describes the complete Packer build process and the different options for launching the build. |
| 47 | + |
| 48 | +### **Build-Time Variables** |
| 49 | +The [Packer template](https://github.mathworks.com/development/parallel-server-aws-refarch/tree/dev/packer/v1/build-parallel-server-ami.pkr.hcl) supports these build-time variables. |
| 50 | +| Argument Name | Default Value | Description | |
| 51 | +|---|---|---| |
| 52 | +| [PRODUCTS](#customize-products-to-install)| MATLAB, MATLAB Parallel Server, MATLAB Production Server™, and all available toolboxes | Products to install, specified as a list of product names separated by spaces. For example, `MATLAB Simulink MATLAB_Parallel_Server MATLAB_Production_Server`.<br>MATLAB parallel server must be installed with MATLAB in order to deploy a parallel server cluster to the cloud. If no products are specified, the Packer build will install MATLAB with all available toolboxes. For more information, see [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md).| |
| 53 | +| [POLYSPACE_PRODUCTS](#customize-polyspace-products-to-install)| Polyspace® Bug Finder™ Server™ and Polyspace Code Prover™ Server™ | Polyspace products to install, specified as a list of product names separated by spaces. For example, `Polyspace_Bug_Finder_Server Polyspace_Code_Prover_Server`.<br/>If no products are specified, the Packer build will install Polyspace with Polyspace Bug Finder Server and Polyspace Code Prover Server. For more information, see [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md).| |
| 54 | +| SPKGS | List of Deep Learning Support Packages, specified in [release-config](https://github.mathworks.com/development/parallel-server-aws-refarch/tree/dev/packer/v1/release-config) | A list of support packages to install, specified as a list of support package names separated by spaces. For example, `Deep_Learning_Toolbox_Model_for_GoogLeNet_Network Deep_Learning_Toolbox_Model_for_ResNet-101_Network` | |
| 55 | +| BASE_AMI | Default AMI ID refers to ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server. | The base AMI upon which the image is built, defaults to an official Canonical® Ubuntu® image. | |
| 56 | +| VPC_ID | *unset* | VPC to assign to the Packer build instance. If no VPC is specified, the default VPC will be used.| |
| 57 | +| SUBNET_ID | *unset* | Subnet to assign to the Packer build instance. If no subnet is specified, the subnet with the most free IPv4 addresses will be used.| |
| 58 | +| INSTANCE_TAGS |{Name="Packer Builder", Build="MATLAB_Parallel_Server"} | Tags to add to the Packer build instance.| |
| 59 | +| AMI_TAGS | {Name="Packer Build", Build="MATLAB_Parallel_Server", Type="matlab-parallel-server-on-aws"} | Tags to add to the machine image.| |
| 60 | + |
| 61 | +For a full list of the variables used in the build, see the description fields in the |
| 62 | +[Packer template](https://github.mathworks.com/development/parallel-server-aws-refarch/tree/dev/packer/v1/build-parallel-server-ami.pkr.hcl). |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +### **Customize Packer Build** |
| 67 | +#### **Customize Products to Install** |
| 68 | +Use the Packer build-time variable `PRODUCTS` to specify the list of products you want to install on the machine image. If unspecified, Packer will install MATLAB, MATLAB Parallel Server, MATLAB Production Server, and all the available toolboxes. Use the Packer build-time variable `POLYSPACE_PRODUCTS` to specify the list of Polyspace products you want to install on the machine image. If unspecified, Packer will install Polyspace Bug Finder Server and Polyspace Code Prover Server. |
| 69 | + |
| 70 | +For example, install the latest version of MATLAB and Deep Learning Toolbox. |
| 71 | +```bash |
| 72 | +packer build -var="PRODUCTS=MATLAB MATLAB_Parallel_Server MATLAB_Production_Server Deep_Learning_Toolbox" build-parallel-server-ami.pkr.hcl |
| 73 | +``` |
| 74 | + |
| 75 | +#### **Customize MATLAB Parallel Server Release to Install** |
| 76 | +To use an earlier MATLAB Parallel Server release, you must use one of the variable definition files in the [release-config](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws/tree/master/packer/v1/release-config) folder. Although the files are available for MATLAB Parallel Server release R2021b and later, MathWorks recommends using MATLAB Parallel Server R2023a or later. This is because the Packer builds for earlier releases use Ubuntu 20.04, which is no longer supported by Canonical. |
| 77 | + |
| 78 | +For example, install R2023a for MATLAB Parallel Server and all necessary and available toolboxes. |
| 79 | +```bash |
| 80 | +packer build -var-file="release-config/R2023a.pkrvars.hcl" build-matlab-parallel-server-ami.pkr.hcl |
| 81 | +``` |
| 82 | +Command line arguments can also be combined. For example, install R2020a for MATLAB, MATLAB Parallel Server, MATLAB Production Server, and the Parallel Computing Toolbox only. |
| 83 | +```bash |
| 84 | +packer build -var-file="release-config/R2023a.pkrvars.hcl" -var="PRODUCTS=MATLAB MATLAB_Parallel_Server MATLAB_Production_Server Parallel_Computing_Toolbox" build-matlab-parallel-server-ami.pkr.hcl |
| 85 | +``` |
| 86 | +Launch the customized image using the corresponding CloudFormation Template. |
| 87 | +For instructions on how to use CloudFormation Templates, see the Deployment Steps |
| 88 | +section on [MATLAB Parallel Server on Amazon Web Services](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws). |
| 89 | +#### **Customize Multiple Variables** |
| 90 | +You can set multiple variables in a [Variable Definition File](https://developer.hashicorp.com/packer/docs/templates/hcl_templates/variables#standard-variable-definitions-files). |
| 91 | + |
| 92 | +For example, to generate a machine image with the most recent MATLAB installed with necessary and additional toolboxes in a custom VPC, create a variable definition file named `custom-variables.pkrvars.hcl` containing these variable definitions. |
| 93 | +``` |
| 94 | +VPC_ID = <any_VPC_id> |
| 95 | +PRODUCTS = "MATLAB MATLAB_Parallel_Server MATLAB_Production_Server Deep_Learning_Toolbox Parallel_Computing_Toolbox" |
| 96 | +POLYSPACE_PRODUCTS = "Polyspace_Bug_Finder_Server Polyspace_Code_Prover_Server" |
| 97 | +``` |
| 98 | + |
| 99 | +To specify a MATLAB release using a variable definition file, modify the variable definition file |
| 100 | +in the [release-config](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws/tree/master/packer/v1/release-config) |
| 101 | +folder corresponding to the desired release. |
| 102 | + |
| 103 | +Save the variable definition file and include it in the Packer build command. |
| 104 | +```bash |
| 105 | +packer build -var-file="custom-variables.pkrvars.hcl" build-parallel-server-ami.pkr.hcl |
| 106 | +``` |
| 107 | + |
| 108 | +### **Installation, Runtime, and Startup Scripts** |
| 109 | +The Packer build executes scripts on the image builder instance during the build. |
| 110 | +These scripts perform tasks such as |
| 111 | +installing tools needed by the build (including gcc, wget, make, CloudWatch, cfn-signal), |
| 112 | +installing MATLAB Parallel Server and toolboxes on the image using [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md), |
| 113 | +and cleaning up build leftovers (including bash history, SSH keys). |
| 114 | + |
| 115 | +For the full list of scripts that the Packer build executes during the build, see the `BUILD_SCRIPTS` parameter in the |
| 116 | +[Packer template](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws/tree/master/packer/v1/build-parallel-server-ami.pkr.hcl). |
| 117 | +The prebuilt images that MathWorks provides are built using these scripts as a base, and additionally have support packages installed. |
| 118 | + |
| 119 | +In addition to the build scripts above, the Packer build copies further scripts to the machine image, |
| 120 | +to be used during startup and at runtime. These scripts perform tasks such as |
| 121 | +mounting available storage, |
| 122 | +initializing CloudWatch logging (if this is chosen), |
| 123 | +performing MATLAB startup acceleration, |
| 124 | +setting up [MATLAB Job Scheduler](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws?tab=readme-ov-file#what-is-matlab-job-scheduler), |
| 125 | +and setting up autoscaling scripts and cluster management scripts, |
| 126 | +among other utility tasks. |
| 127 | + |
| 128 | +For the full list of startup and runtime scripts, see the `STARTUP_SCRIPTS` and the `RUNTIME_SCRIPTS` parameters in the |
| 129 | +[Packer template](https://github.com/mathworks-ref-arch/matlab-parallel-server-on-aws/tree/master/packer/v1/build-parallel-server-ami.pkr.hcl). |
| 130 | + |
| 131 | + |
| 132 | +## Validate Packer Template |
| 133 | +To validate the syntax and configuration of a Packer template, use the `packer validate` command. This command also checks whether the provided input variables meet the custom validation rules defined by MathWorks. For more information, see [validate Command](https://www.packer.io/docs/commands/validate#validate-command). |
| 134 | + |
| 135 | +You can also use command line interfaces provided by Packer to inspect and format the template. For more information, see [Packer Commands (CLI)](https://www.packer.io/docs/commands). |
| 136 | + |
| 137 | +## Deploy Machine Image |
| 138 | +When the build finishes, Packer writes |
| 139 | +the output to a `manifest.json` file, which contains these fields: |
| 140 | +```json |
| 141 | +{ |
| 142 | + "builds": [ |
| 143 | + { |
| 144 | + "name":, |
| 145 | + "builder_type": , |
| 146 | + "build_time": , |
| 147 | + "files": , |
| 148 | + "artifact_id": , |
| 149 | + "packer_run_uuid": , |
| 150 | + "custom_data": { |
| 151 | + "release": , |
| 152 | + "specified_products": , |
| 153 | + "specified_spkgs": , |
| 154 | + "specified_polyspace_products": , |
| 155 | + "build_scripts": , |
| 156 | + } |
| 157 | + } |
| 158 | + ], |
| 159 | + "last_run_uuid": "" |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +The `artifact_id` section shows the ID of the machine image generated by the most recent Packer build. |
| 164 | + |
| 165 | +The CloudFormation templates provided by MathWorks for releases R2022b onwards include an optional custom machine image ID field, `CustomAmiId`. |
| 166 | +If you do not specify a custom machine image ID, the template |
| 167 | +launches a prebuilt image provided by MathWorks. To launch a custom machine image, |
| 168 | +provide the `artifact_id` from the `manifest.json` file as the `CustomAmiId`. |
| 169 | + |
| 170 | +For AMIs built with an earlier MATLAB release, replace the AMI ID in the |
| 171 | +corresponding CloudFormation template with the AMI ID of your customized image. |
| 172 | + |
| 173 | +If the build has been customized, for example by removing or modifying one or more of the included scripts, |
| 174 | +the resultant machine image **may no longer be compatible** with the provided CloudFormation template. If the AMI has been created from the MATLAB on AWS offering, then the resultant machine image **may not be compatible** due to missing autoscaling scripts, job scheduler setup, and other customisations. |
| 175 | +Compatibility can in some cases be restored by making corresponding modifications to the CloudFormation template. |
| 176 | + |
| 177 | +## Technical Support |
| 178 | +If you require assistance or have a request for additional features or capabilities, contact [MathWorks Technical Support](https://www.mathworks.com/support/contact_us.html). |
| 179 | + |
| 180 | +---- |
| 181 | + |
| 182 | +Copyright 2025 The MathWorks, Inc. |
| 183 | + |
| 184 | +---- |
0 commit comments