FEATURES OPEN SOURCE ABOUT DOCS LOGIN REGISTER

Advanced stack editing

Introduction

In this example we will create a Stack with a single VM acting as a database server. In this example we will cover:

  1. Creating a Stack
  2. Editing Stack user_data section to install MySQL server whenever a VM is built
  3. Using Custom Variables to create a database table and to place a file in user’s home folder

Table of contents

  1. Full stack
  2. Steps
    1. Creating a stack
    2. Edit your stack name
    3. Edit your VM name
    4. Modify the user_data section
    5. Create a variable section
    6. Update the user_data section
  3. Build & Test our stack
  4. Test our Stack
  5. Conclusion

Full Stack

If you want to skip the explanation, here is how your final Stack template will look:

# This stack file will build a MySQL Database VM
# and will run a SQL script to create database and a table schema

provider:
  aws:
    access_key: '${var.aws_access_key}'
    secret_key: '${var.aws_secret_key}'

variable:
  my_DB_construct:
    default: 'create database customer_accounts;use customer_accounts;create table customers(CustomerID int,LastName varchar(255),FirstName varchar(255),Address varchar(255),Phone varchar(255),PRIMARY KEY (`CustomerID`));'

resource:
  aws_instance:
    db_server:
      instance_type: t2.micro
      user_data: |-
        DEBIAN_FRONTEND=noninteractive
        apt-get update -y
        apt-get install -y mysql-server-5.6
        echo '${var.my_DB_construct}' > /home/${var.koding_user_username}/dbconstruct.sql
        mysql -u root "" < /home/${var.koding_user_username}/dbconstruct.sql

Steps

Create a stack

  1. Click Stacks
  2. Click New Stack
  3. Choose Amazon Web Services -> Click Next
  4. Set your AWS credentials in the Credentials tab

For a step by step guide on how to create an AWS stack check here

Edit your stack name

It is a good practice to name your Stack, specially if you plan to have multiple stacks. We named our stack “DB server alpha”

rename-stack-894.png

Edit your VM name

Let us also rename our VM to something more descriptive, the name of the VM is defined under the aws_instance: header. In our case we chose the name ‘db_server’.

resource:
  aws_instance:
    db_server: # name changed to 'db_server'
      instance_type: t2.micro

Modify the user_data section

We will add commands to the user_data section to install MySQL. The user-data section contains the commands we want to run when the VM is first built. All commands here run as root. In our example, we want to:

  1. Update packages list
  2. Install MySQL unattendedly
    user_data: |-
            DEBIAN_FRONTEND=noninteractive
            apt-get update -y
            apt-get install -y mysql-server-5.6

Create a variable section

We will now create a variable in our Stack file. This variable will hold our table schema example. (Later on, we will call this variable to create the Database table on the VM after MySQL is installed.)

The database schema we will use is:

create database customer_accounts;
    use customer_accounts;
    create table customers
    (
        CustomerID int,
        LastName varchar(255),
        FirstName varchar(255),
        Address varchar(255),
        Phone varchar(255),
        PRIMARY KEY (`CustomerID`)
    );

We create the variable: section between the provider and the resource sections. We define a variable name (my_DB_construct) and set its default value to our db schema string:

variable:
   my_DB_construct:
     default: 'create database customer_accounts;use customer_accounts;create table customers(CustomerID int,LastName varchar(255),FirstName varchar(255),Address varchar(255),Phone varchar(255),PRIMARY KEY (`CustomerID`));'

Update the user_data section

We will update the user_data section to create our database schema and save the schema in a file in the user home directory. This will show how variables are placed in our Stack Template

user_data: |-
    DEBIAN_FRONTEND=noninteractive
    apt-get update -y
    apt-get install -y mysql-server-5.6
    echo '${var.my_DB_construct}' > /home/${var.koding_user_username}/dbconstruct.sql
    mysql -u root "" < /home/${var.koding_user_username}/dbconstruct.sql

Let’s build our Stack & test!

  1. Click SAVE, you should see the message that your stack was built successfully if all is well.

    stack-build-success.png

  2. Click Make Team Default, this will populate your stack to the team and send them updates that a new stack is in place.

    share-creds-854.png

    Choose whether you wish to enable the checkbox share your credentials with the team, then click Share with the team

    Sharing your credentials with your team is very helpful if you don’t want your team (developers/students) to go through creating an AWS account each, or if you want to have full control over the machines created in AWS.

    Sharing your AWS credentials means all built machines will be under your AWS account, which will incur charges to your AWS account for each machine built by your team.

  3. Click Initialize

    If you are updating an already created stack, please note: Any data that was on your earlier Stack VM’s will be removed when you choose to build/re-initialize the new stack this also applies to your teammates when they click build/re-initialize new stack. When a user chooses to Build the new Stack all their VM’s will be re-initialized as complete new VM’s with the new configuration from your stack template. A warning will pop up to alert the user about this before continuing to build the new stack. Please make sure to backup your data before building your new stack in that case!

  4. The Build Your Stack modal will open, click Next

    build-stack-step1.png

    Noticed the Read Me First content? This is the readme that is defined by default when creating a stack, you can edit its content from the Readme tab during Stack creation.

  5. Choose the credentials you want to use with your stack and click Build Stack

    build-stcak-step2.png

  6. Stack building will start..

    build-stack-inprogress.png

  7. Successfully built stack, click Start Coding

    build-success.png

  8. user-data commands still running

    stack-running-commands-zoom.png

    user-data commands complete we can now start using our VM(s)

    10-commands-finished-zoom.png

  9. Let us check if our user_data commands ran successfully. If all went well, we should be able to see the file dbconstruct.sql created, and that it contains our DB Schema, MySQL installed, and the customer_accounts database created with a customer table as defined in our DB schema file. Here’s a reminder of our user_data block from our Stack Template

user_data: |-
    DEBIAN_FRONTEND=noninteractive
    apt-get update -y
    apt-get install -y mysql-server-5.6
    echo '${var.my_DB_construct}' > /home/${var.koding_user_username}/dbconstruct.sql
    mysql -u root "" < /home/${var.koding_user_username}/dbconstruct.sql

11-check-files-zoom-1.png

Yes! all is well, the file exists in our Files viewer section in Koding, or by typing in the ls command.

12-review-files-zoom2-1.png

Conclusion

Congratulations! you are now ready to create customized Stack files to install packages and use variables to help you setup your VMs. You are now sure all your team have the same exact setup. Onboarding a new team member is now a breeze, all they need to do is get invited to the team and start their stack!

Happy Koding!