Building AWS Machine Images With Infrastructor

DZone 's Guide to

Building AWS Machine Images With Infrastructor

Check out how you can build AWS Machine Images in a single file using Infrastructor, an open source server provisioning and automation framework.

· Cloud Zone ·
Free Resource

Infrastructor is an open source server provisioning and automation framework. It is written with Groovy and provides a DSL to manage the configuration of both virtual machines and bare-metal servers. In this article, I will show you how to use Infrastructor to build and configure AWS Machine Images (AMI). This approach might be pretty convenient, since you can completely automate build and configuration processes with a single tool.


Before running through the example you need to make sure you have:

  1. Oracle Java version 8 is installed.

  2. Infrastructor version > 0.1.5 is installed. Infrastructor has "download and unpack" installation approach.

  3. Access to your private AWS account: AWS Access Key Id and AWS Access Secret Key with permissions to launch/terminate EC2 instances and register/deregister AMIs. These parameters will be passed to the provisioning process as AWS_ACCESS_KEY_ID and AWS_ACCESS_SECRET_KEY accordingly.

  4. An SSH private key file to access the EC2 instance. A path to the key file will be passed to the provisioning process as KEYPATH parameter.

How it Works

An AMI provisioning procedure can be completely described in a single file and placed under source control. The description typically consists of two parts: an AWS configuration and a provisioning plan. There is no limitation on how many AMIs can be described in a file. The description is just a Groovy script, so all power of the language can be utilized to build as many images as you want. For example, you can reuse the same provisioning plan to build AMIs for Ubuntu and CentOS with a single run.

Let's take a look at a small example (my_image.groovy):

awsMachineImage { // AWS configuration
    awsAccessKeyId     = AWS_ACCESS_KEY_ID
    awsAccessSecretKey = AWS_ACCESS_SECRET_KEY
    awsRegion          = 'eu-central-1'
    imageName          = "my-image-name"
    usePublicIp        = true
    recreate           = true
    node {
        name             = 'my-base-instance'
        imageId          = 'ami-12345678'
        instanceType     = 't2.micro'
        subnetId         = 'subnet-12345678'
        keyName          = 'aws_infrastructor_ci'
        securityGroupIds = ['sg-12345678']
        username         = 'ubuntu'
        keyfile          = KEYFILE
}.build { // Provisioning plan
    task name: 'installing docker', actions: {
        shell sudo: true, command: '''
            curl -Ssl https://get.docker.com | sh
            usermod -aG docker ubuntu

In the example above, we specify AMI parameters, including connectivity settings, and provide a set of tasks to configure the image. AWS_ACCESS_KEY_ID, AWS_ACCESS_SECRET_KEY, and KEYFILE are passed as command line arguments, but can also be loaded from a configuration file or secretly requested with an input action. You also need to update node.imageIdnode.subnetId, node.keyName, and node.securityGroupsIds to your own values, since they depend on your AWS account. Make sure the security group allows SSH connectivity on port 22. Infrastructor uses SSH to configure the machine.

To run the example, type the following command in a terminal:

$ infrastructor run -f my_image.groovy \
  -v AWS_ACCESS_KEY_ID="..." \
  -v KEYFILE="..."

Please replace "..." with your own values. In the logs, you should see the AMI ID once the build has finished:

[INFO] running script: my_image.groovy
[INFO] staring image creation for 'my-image-name'
[INFO] checking if there is an existing image with the same name
[INFO] creating a base EC2 instance
[INFO] configuring the base EC2 instance 'i-12345678901234567'
[INFO] task: 'installing docker'
[INFO] task: 'installing docker', done on 1 node|s
[INFO] stopping the base EC2 instance 'i-12345678901234567'
[INFO] creating an image 'my-image-name'
[INFO] image creation is done: 'ami-12345678'
[INFO] running script: my_image.groovy - done

EXECUTION COMPLETE in 2 minutes, 53.715 seconds

That's it. Your image is ready to use! For more information about Infrastructor, please check the references section below.


aws machine images ,cloud ,immutable infrastructure ,infrastructor ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}