-
-
Save sgrove/b986f81554e411da21bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* For utop use: | |
#require "cohttp.lwt";; | |
#require "aws.lwt";; | |
#require "aws_ec2";; | |
#require "aws";; | |
*) | |
(* Be warned, to get ocaml-aws working with all the modules built, | |
you'll need to pin the aws package, and then manually build + pin all | |
the subpackages you care about, e.g. aws_ec2: | |
git clone https://github.com/inhabitedtype/ocaml-aws.git | |
cd ocaml-aws | |
./configure -enable-gen -enable-lwt | |
make gen | |
make | |
opam pin add aws . | |
cd libraries/ec2 | |
opam pin add . | |
*) | |
(* To build *) | |
(* $ ocamlbuild -use-ocamlfind -pkgs aws_ec2,aws.lwt,aws,cohttp.lwt -I src/ main.native *) | |
(* To run: *) | |
(* $ EC2_REGION=us-east-1 EC2_ACCESS_KEY=access_key EC2_SECRET_KEY=secret_key ./main.native | |
eu-west-1 -> ec2.eu-west-1.amazonaws.com | |
ap-southeast-1 -> ec2.ap-southeast-1.amazonaws.com | |
ap-southeast-2 -> ec2.ap-southeast-2.amazonaws.com | |
eu-central-1 -> ec2.eu-central-1.amazonaws.com | |
ap-northeast-2 -> ec2.ap-northeast-2.amazonaws.com | |
ap-northeast-1 -> ec2.ap-northeast-1.amazonaws.com | |
us-east-1 -> ec2.us-east-1.amazonaws.com | |
sa-east-1 -> ec2.sa-east-1.amazonaws.com | |
us-west-1 -> ec2.us-west-1.amazonaws.com | |
us-west-2 -> ec2.us-west-2.amazonaws.com *) | |
module EC2 = Aws_ec2 | |
open Lwt.Infix | |
let get_env key = | |
Sys.getenv key | |
let region = | |
get_env "EC2_REGION" | |
let access_key = | |
get_env "EC2_ACCESS_KEY" | |
let secret_key = | |
get_env "EC2_SECRET_KEY" | |
let ebs_device = | |
"/dev/xvdh" | |
(* let host_instance_id = *) | |
(* Describe images *) | |
let describe_images () = | |
let open EC2 in | |
let response_t = Aws_lwt.Runtime.run_request ~region ~access_key ~secret_key | |
(module EC2.DescribeImages) | |
(EC2.Types.DescribeImagesRequest.make | |
~owners:(EC2.Types.OwnerStringList.make ["self"] ()) | |
~filters:[(EC2.Types.Filter.make ~name:"architecture" ~values:["x86_64"] ())] | |
()) in | |
response_t >>= | |
function | |
| `Error _ -> raise Not_found | |
| `Ok x -> | |
let images = x.EC2.Types.DescribeImagesResult.images in | |
let mappings = List.map (fun x -> | |
let name = match x.EC2.Types.Image.name with | |
| None -> "NAME_NOT_FOUND" | |
| Some x -> x in | |
let architecture = List.assoc x.EC2.Types.Image.architecture EC2.Types.ArchitectureValues.t_to_str in | |
Printf.sprintf "Image %s is arch %s" name architecture) images in | |
Lwt.return mappings | |
(* List regions *) | |
let main () = | |
(* TODO: Figure out how to communicate error messages better *) | |
let response_t = Aws_lwt.Runtime.run_request ~region ~access_key ~secret_key | |
(module EC2.DescribeRegions) | |
(EC2.Types.DescribeRegionsRequest.make ()) in | |
response_t >>= | |
function | |
| `Error _ -> raise Not_found | |
| `Ok x -> | |
let regions = x.EC2.Types.DescribeRegionsResult.regions in | |
let mappings = List.map (fun x -> | |
let region_name = match x.EC2.Types.Region.region_name with | |
| None -> "REGION_NAME_NOT_FOUND" | |
| Some x -> x in | |
let endpoint = match x.EC2.Types.Region.endpoint with | |
| None -> "ENDPOINT_NOT_FOUND" | |
| Some x -> x in | |
Printf.sprintf "%s -> %s" region_name endpoint) regions in | |
Lwt.return mappings | |
let () = | |
print_endline "Listing regions:"; | |
let mappings = Lwt_main.run (main ()) in | |
List.iter (fun x -> print_endline x) mappings; | |
print_endline "Listing images with filter:"; | |
let mappings = Lwt_main.run (describe_images ()) in | |
List.iter (fun x -> print_endline x) mappings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment