How to always deploy the latest AWS AMI for a given OS

3,972 views
Skip to first unread message

RGB

unread,
Nov 7, 2017, 1:25:32 PM11/7/17
to Terraform
Hi.  I'm trying to build a script that auto-deploys the latest version of the four AMIs listed below.  I saw this in the terraform docs (https://www.terraform.io/docs/providers/aws/d/ami.html).  However, I can't find the "Owner" in the AWS UI and I'm not sure how to make sure the name is unique and will span newer versions.  Can someone please point me to where I can find the 'Owner' and how to know the name is appropriate for this use?  Of course, it would be awesome if someone offered up sample code for pulling the latest of these. :)  Thanks.

Amazon Linux AMI 2017.09.1 (HVM), SSD Volume Type
Ubuntu Server 16.04 LTS (HVM), SSD Volume Type
SUSE Linux Enterprise Server 12 SP3 (HVM), SSD Volume Type
CentOS 7 (x86_64) - with Updates HVM

David Adams

unread,
Nov 7, 2017, 6:02:30 PM11/7/17
to terrafo...@googlegroups.com
Not sure about the other distributions, or where to find the owner in the UI, but this is the code I use for the latest ubuntu 16.04 hvm-ssd AMI:

    data "aws_ami" "ami" {
      most_recent = true

      # ubuntu ami account ID
      owners = ["099720109477"]

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

Whether the names are consistent over time is up to the provider of the AMIs.

As for the owner ID, if you have an AMI ID, you can run this at the command line (with the appropriate region and AMI ID, obviously):

    $  aws ec2 describe-images --region us-east-1 --image-ids ami-da05a4a0

And OwnerId is a field in the result.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/c3c7d826-984c-491b-9419-d202f1ba62d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RGB

unread,
Nov 8, 2017, 9:37:47 AM11/8/17
to Terraform
Thanks so much, David!

RGB

unread,
Nov 10, 2017, 4:51:47 PM11/10/17
to Terraform
Hi David.  Could I bother you with a follow-up question?

When spinning up an instance and pulling the ami from this data, is it ".id" that is used?  I keep returning zero results when entering the same thing you have and I think it's because the last element (.id) isn't right.  I don't see this in the documentation though.  How do you know which element to pull?
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

RGB

unread,
Nov 17, 2017, 3:48:34 PM11/17/17
to Terraform
Figured it out.  I had the execution set to 'self'.  For anyone else looking for the answer to this, here is what worked for me:

data "aws_ami" "amazonlinux_ami_useast1" {
  most_recent       = true
  owners            = ["137112412989"]

  filter {
    name   = "name"
    values = ["amzn-ami-hvm-*-x86_64-gp2"]
  }
}

data "aws_ami" "ubuntuserver_ami_useast1" {
  most_recent       = true
  owners            = ["099720109477"]

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

data "aws_ami" "debian8jessie_ami_useast1" {
  most_recent       = true
  owners            = ["679593333241"]

  filter {
    name   = "name"
    values = ["debian-jessie-amd64-hvm-*"]
  }
}


data "aws_ami" "centos7_ami_useast1" {
  most_recent       = true
  owners            = ["679593333241"]

  filter {
    name   = "name"
    values = ["CentOS Linux 7 x86_64 HVM EBS*"]
  }
}
# Call this using: "${data.aws_ami.centos7_ami_useast1.id}"
Reply all
Reply to author
Forward
0 new messages