Getting latest Ubuntu AMI with Terraform

Posted on Fri 25 August 2017 in Development • Tagged with AWS, Terraform, Ubuntu, devops

When we need to create an EC2 resource on AWS using Terraform, we need to specify the AMI id to get the correct image. The id is not easy to memorise and it changes depending on the zone we are working one. On every new release the id changes again. So, how can we be sure to get the correct ID for our region, of the latest image available for a given Linux distribution?

Getting latest Ubuntu AMI id

In this example I will show how to get the ID for the latest version of Ubuntu 16.04 server, for the London region and create an EC2 instance using that ID.

variable "aws_region" { default = "eu-west-2" } # London

provider "aws" {
    region = "${var.aws_region}"
    access_key = "youraccesskey"
    secret_key = "yoursecretkey"
}

data "aws_ami" "ubuntu" {
    most_recent = true

    filter {
        name   = "name"
        values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"]
    }

    filter {
        name   = "virtualization-type"
        values = ["hvm"]
    }

    owners = ["099720109477"] # Canonical
}

resource "aws_instance" "web" {
    ami           = "${data.aws_ami.ubuntu.id}"
    instance_type = "t2.micro"

    tags {
        Name = "HelloUbuntu"
    }
}

output "image_id" {
    value = "${data.aws_ami.ubuntu.id}"
}

After we have initialised our script using terraform init if we run it, we will get the AMI id and the instance will be created:

  example1$: terraform apply
data.aws_ami.ubuntu: Refreshing state...
aws_instance.web: Creating...
    ami:                          "" => "ami-03998867"
    associate_public_ip_address:  "" => "<computed>"
    availability_zone:            "" => "<computed>"
    ebs_block_device.#:           "" => "<computed>"
    ephemeral_block_device.#:     "" => "<computed>"
    instance_state:               "" => "<computed>"
    instance_type:                "" => "t2.micro"
    ipv6_address_count:           "" => "<computed>"
    ipv6_addresses.#:             "" => "<computed>"
    key_name:                     "" => "<computed>"
    network_interface.#:          "" => "<computed>"
    network_interface_id:         "" => "<computed>"
    placement_group:              "" => "<computed>"
    primary_network_interface_id: "" => "<computed>"
    private_dns:                  "" => "<computed>"
    private_ip:                   "" => "<computed>"
    public_dns:                   "" => "<computed>"
    public_ip:                    "" => "<computed>"
    root_block_device.#:          "" => "<computed>"
    security_groups.#:            "" => "<computed>"
    source_dest_check:            "" => "true"
    subnet_id:                    "" => "<computed>"
    tags.%:                       "" => "1"
    tags.Name:                    "" => "HelloUbuntu"
    tenancy:                      "" => "<computed>"
    volume_tags.%:                "" => "<computed>"
    vpc_security_group_ids.#:     "" => "<computed>"
aws_instance.web: Still creating... (10s elapsed)
aws_instance.web: Still creating... (20s elapsed)
aws_instance.web: Still creating... (30s elapsed)
aws_instance.web: Creation complete (ID: i-0f58f8bd55b3a7e38)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

image_id = ami-03998867

That's all we need to spin up an EC2 instance on AWS using latest Ubuntu image available.


How to make subwoofer work in Ubuntu

Posted on Sat 22 June 2013 in HowTo • Tagged with sound, subwoofer, Ubuntu, Linux

Using the same computer with Windows 8 and Ubuntu I noticed that the sound was worse in Ubuntu and I discovered why soon: subwoofer doesn't work out of the box!

How to fix it

The fix is quite easy to apply (but it was not easy to find the right one!). First of all edit /etc/pulse/default.pa and add this line at the end:

load-module module-combine channels=6 channel_map=front-left,front-right,rear-left,rear-right,front-center,lfe

then edit /etc/pulse/daemon.conf, modify the line enable-lfe-remixing: no to enable-lfe-remixing: yes, then uncomment it (remove the semicolon in front of it). Reboot your PC and enjoy the subwoofer!

References


UDS happening online only: pros and cons

Posted on Tue 05 March 2013 in Ubuntu (EN), UDS • Tagged with Canonical, community, Linux, meeting, Ubuntu, UDS

When last week Canonical announced the usual UDS was not going to happen I was a bit shocked and disappointed: starting from the next UDS (that is going to happen tomorrow!) the event will be online only and every 3 months. During these days I've been thinking a lot about this move and I will tell you what are the pros and cons, in my opinion, followed by some final thoughts.

Pros

Having 4 UDS every year, instead of 2, is surely a better thing. I'm a big fan of Scrum methodology, so I think that iterating more often is better than iterating less. If there are any mistakes you can correct them and iterating again before releasing the final product.

Potentially more people can partecipate to the event (even the opposite is true and I will explain why). People won't need to move from home, travel, pay any expense etc... they just need a computer and a good Internet connection.

It's cheaper for everyone: I can just imagine how expensive could be for Canonical to organize a similar event. Booking a big hotel, paying travel and expenses to near one houndred of community people. People who didn't get any sponsorization had to pay all the travel expenses to attend the event.

Cons

Potentially less people can partecipate to the event. Yes, like I said before even this sentence is true and I will explain why. First of all, using Google+ there are at least three countries that will be cut out: China, Thailand and Vietnam. Google+ is not available in those countries.

Are you sure that special people will be able to follow the event? For example blind people won't be able to chat or to ask question in the chat.

Only 10 people will be able to talk. In normal UDS sessions more people could raise the hand and ask a question or interact with the track leaders. Who will choose the 10 people with audio+video streaming rights?

We will completly miss the social aspect of the UDS. If you think this was only a secondary part, please go on. I felt more committed to work and collaborate with people I met in person than with someone I've never met before.

Announcing an event, even if online, just one week before it happens. Really? Some people had already taken vacation from work, booked flights etc... not counting many people that can't take 2 days off from work just with 1 week notice period. It's also almost impossible that community members have the time to schedule a blueprint and be able to discuss about a subject.

Final thoughts

From a cutting costs point of view I really can't say anything. Organizing UDS was surely very expensive for Canonical and nobody can blame them if they decided to spend those money in a different way.

What really concerns me: is UDS still useful? My opinion is that at least since latest 2 or 3 UDS the presence of the Community was not so relevant, because I had the clear sensation that the most important decisions were made by Canonical before the UDS and then there was just some details tuning. Another proof of my thoughts is the today announcement: despite the fact that I 100% agree with Unity switching to Qt/QML (I already proposed this 2 years ago during Budapest UDS, but nobody listened to me) I completly disagree with the way the decision was made: not a single involvement or discussion with the community.

I would apreaciate more openness and honesty from Canonical. Do you want to take all the decisions? That's fine, but at least state it clearly.


Using Twitter Bootstrap with Node.js, Express and Jade

Posted on Sun 24 February 2013 in HowTo • Tagged with Bootstrap, css, Express, Jade, Javascript, NodeJs, npm, twitter, Ubuntu

I've decided to write this post as a note to myself. I'm still learning Node.js and digging into Express/Jade, but I've read many people using the nice Twitter Bootstrap and I was wondering if there was a way to integrate all these technologies. The short answer is: yes, we can!

Note: once again, I'm not a Node.js expert and surely there are other ways to achieve this task (for example there is a Node.js module called twitter-bootstrap, but I haven't tried it). This tutorial is based on another tutorial I found, but it was not very updated and it had a more complicated way to install Bootstrap, so I decided to write a new one basing it on the original http://www.rs.au.com/31/how-to-install-bootstrap-v2-0-2-in-expressjs-v3-0-0

Preparing the environment

I will assume that you're running any Linux distribution (in my case I'm using Ubuntu 12.10, but feel free to use your own distribution). Be sure to have installed a recent version of nodejs and npm packages (I'm using Node.js 0.8.20 and npm 1.2.11).

Create a project folder and install the required dependencies

mkdir node-bootstrap
cd node-bootstrap
npm install express
npm install jade

Create the basic project structure with Express

andrea@andrea-Inspiron-660:~/Documents/sviluppo/nodejs/node-bootstrap$ node_modules/express/bin/express nodebootstrap

create : nodebootstrap
create : nodebootstrap/package.json
create : nodebootstrap/app.js
create : nodebootstrap/public
create : nodebootstrap/public/javascripts
create : nodebootstrap/public/images
create : nodebootstrap/public/stylesheets
create : nodebootstrap/public/stylesheets/style.css
create : nodebootstrap/routes
create : nodebootstrap/routes/index.js
create : nodebootstrap/routes/user.js
create : nodebootstrap/views
create : nodebootstrap/views/layout.jade
create : nodebootstrap/views/index.jade

install dependencies:
$ cd nodebootstrap && npm install

run the app:
$ node app

You should already have installed all the needed dependencies, even without executing npm install, anyway executing it won't hurt.

Download and install Bootstrap

Download Twitter Boostrap from the official website http://twitter.github.com/bootstrap/assets/bootstrap.zip and unzip it under the nodebootstrap/public folder.

Bootstrap integration with Jade template system

At this point you need to edit the views/layout.jade file and include the references to Bootsrap

html
  head
    title= title
    link(rel='stylesheet', href='/bootstrap/css/bootstrap.min.css')
    link(rel='stylesheet', href='/bootstrap/css/bootstrap-responsive.min.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js')
    script(src='/bootstrap/js/bootstrap.min.js')
  body
    block content

Test the Bootstrap integration

At this point we will modify views/index.jade that is the default template used to render the index

extends layout

    block content
      div.top
        form.form-horizontal(method="post", id="loginForm")
          label Username
          input.span3(id="username", type="text", name="User", placeholder="Enter your username")
          label Password
          input.span3(id="password", type="password", name="Password")
          input.btn(type="submit", value="Log In")
      div.container
        div.content
          table.table.table-striped
            thead
              tr
                th Table
                th Heading
            tbody
              tr
                td Blah
                td Test
              tr
                td Hello
                td World

      div.footer

Now go back to the terminal and execute the app:

andrea@andrea-Inspiron-660:~/Documents/sviluppo/nodejs/node-bootstrap/nodebootstrap$ node app.js
Express server listening on port 3000

Open your favourite browse and visit http://localhost:3000 to see your first Bootstrap + Node.js application app and running.