Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created September 5, 2012 01:37
Show Gist options
  • Save uemuraj/3628964 to your computer and use it in GitHub Desktop.
Save uemuraj/3628964 to your computer and use it in GitHub Desktop.
Amazon Linux の最新版 AMI はどれ?
#!/usr/bin/env groovy
@Grab('com.amazonaws:aws-java-sdk:*')
import com.amazonaws.*
import com.amazonaws.auth.*
import com.amazonaws.services.ec2.*
import com.amazonaws.services.ec2.model.*
AWSCredentials credentials = new DefaultAWSCredentialsProviderChain().getCredentials()
ClientConfiguration config = new ClientConfiguration()
String proxyHost = System.getProperty('http.proxyHost')
String proxyPort = System.getProperty('http.proxyPort')
if (proxyHost != null && proxyPort != null) {
config.proxyHost = proxyHost
config.proxyPort = proxyPort.toInteger()
}
AmazonEC2 ec2 = new AmazonEC2Client(credentials, config)
ec2.endpoint = 'ec2.ap-northeast-1.amazonaws.com'
// name が "amzn-ami-pv-2012.03.2.x86_64-ebs" の形式であることに頼っています
// 最新版の判断がつく、適当なフィルタがあれば良いのですが...
DescribeImagesRequest request = new DescribeImagesRequest().with {
owners = ['amazon']
filters = [
new Filter('virtualization-type').withValues('paravirtual'),
new Filter('root-device-type').withValues('ebs'),
new Filter('architecture').withValues('x86_64'),
new Filter('owner-alias').withValues('amazon'),
new Filter('is-public').withValues('true'),
new Filter('state').withValues('available'),
new Filter('name').withValues('amzn-ami-pv-*'),
]
return it
}
DescribeImagesResult result = ec2.describeImages(request)
List<Image> images = result.images.sort { o1, o2 ->
return o2.name.compareTo(o1.name) // 逆順!
}
images.each {
println(it)
}
@uemuraj
Copy link
Author

uemuraj commented Sep 5, 2012

やっぱり内容を見て自動で決定するよりは、使用する予定の AMI にタグ付けしておいて、それを探す方が良いと思う。
タグは他人のリソースにも貼れて、それ見ることができるのは自分だけなので、困らないはず。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment