-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
59 lines (50 loc) · 1.75 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2022 Stefan Sundin
// Licensed under the Apache License 2.0
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), std::io::Error> {
env_logger::init();
// Read the container user-data which contains the allocation id
let allocation_id =
std::fs::read_to_string("/.bottlerocket/bootstrap-containers/current/user-data")
.expect("could not read container user-data");
if !allocation_id.starts_with("eipalloc-") {
panic!(
"Error: user-data does not contain an eipalloc: {:?}",
allocation_id
);
}
println!("Allocation ID: {}", allocation_id);
// Get region and instance id from instance metadata
let region_provider = aws_config::imds::region::ImdsRegionProvider::builder().build();
let region = region_provider.region().await;
if region == None {
panic!("Error: could not get region from IMDS.");
}
println!("Region: {:?}", region);
let imds_client = aws_config::imds::client::Client::builder()
.build()
.await
.expect("could not initialize the IMDS client");
let instance_id = imds_client
.get("/latest/meta-data/instance-id")
.await
.expect("could not get the instance ID from IMDS");
println!("Instance ID: {}", instance_id);
let shared_config = aws_config::from_env()
.credentials_provider(aws_config::imds::credentials::ImdsCredentialsProvider::builder().build())
.region(region)
.load()
.await;
let ec2_client = aws_sdk_ec2::Client::new(&shared_config);
let response = ec2_client
.associate_address()
.allocation_id(allocation_id)
.instance_id(instance_id)
.allow_reassociation(true)
.send()
.await
.expect("could not associate EIP");
println!("Success!");
eprintln!("{:?}", response);
return Ok(());
}