Skip to main content

commonware_deployer/aws/
mod.rs

1//! AWS EC2 deployer
2//!
3//! Deploy a custom binary (and configuration) to any number of EC2 instances across multiple regions. View metrics and logs
4//! from all instances with Grafana.
5//!
6//! # Features
7//!
8//! * Automated creation, update, and destruction of EC2 instances across multiple regions
9//! * Provide a unique name, instance type, region, binary, and configuration for each deployed instance
10//! * Collect metrics, profiles (when enabled), and logs from all deployed instances on a long-lived monitoring instance
11//!   (accessible only to the deployer's IP)
12//!
13//! # Architecture
14//!
15//! ```txt
16//!                    Deployer's Machine (Public IP)
17//!                                  |
18//!                                  |
19//!                                  v
20//!               +-----------------------------------+
21//!               | Monitoring VPC (us-east-1)        |
22//!               |  - Monitoring Instance            |
23//!               |    - Prometheus                   |
24//!               |    - Loki                         |
25//!               |    - Pyroscope                    |
26//!               |    - Tempo                        |
27//!               |    - Grafana                      |
28//!               |    - Tracer                       |
29//!               |  - Security Group                 |
30//!               |    - All: Deployer IP             |
31//!               |    - 3100: Binary VPCs            |
32//!               |    - 4040: Binary VPCs            |
33//!               |    - 4318: Binary VPCs            |
34//!               +-----------------------------------+
35//!                     ^                       ^
36//!                (Telemetry)             (Telemetry)
37//!                     |                       |
38//!                     |                       |
39//! +------------------------------+  +------------------------------+
40//! | Binary VPC 1                 |  | Binary VPC 2                 |
41//! |  - Binary Instance           |  |  - Binary Instance           |
42//! |    - Binary A                |  |    - Binary B                |
43//! |    - Promtail                |  |    - Promtail                |
44//! |    - Node Exporter           |  |    - Node Exporter           |
45//! |    - Pyroscope Agent         |  |    - Pyroscope Agent         |
46//! |  - Security Group            |  |  - Security Group            |
47//! |    - All: Deployer IP        |  |    - All: Deployer IP        |
48//! |    - 9090: Monitoring IP     |  |    - 9090: Monitoring IP     |
49//! |    - 9100: Monitoring IP     |  |    - 9100: Monitoring IP     |
50//! |    - 8012: 0.0.0.0/0         |  |    - 8765: 12.3.7.9/32       |
51//! +------------------------------+  +------------------------------+
52//! ```
53//!
54//! ## Instances
55//!
56//! ### Monitoring
57//!
58//! * Deployed in `us-east-1` with a configurable instance type (e.g., `t4g.small` for ARM64, `t3.small` for x86_64) and storage (e.g., 10GB gp2). Architecture is auto-detected from the instance type.
59//! * Runs:
60//!     * **Prometheus**: Scrapes binary metrics at `:9090` and system metrics from all instances at `:9100`.
61//!     * **Loki**: Listens at `:3100`, storing logs in `/loki/chunks` with a TSDB index at `/loki/index`.
62//!     * **Pyroscope**: Listens at `:4040`, storing profiles in `/var/lib/pyroscope`.
63//!     * **Tempo**: Listens at `:4318`, storing traces in `/tempo`.
64//!     * **Grafana**: Hosted at `:3000`, provisioned with Prometheus, Loki, and Tempo datasources and a custom dashboard.
65//!     * **Tracer**: Hosted at `:8080`, rendering multi-instance trace flamegraphs from the local Tempo.
66//! * Ingress:
67//!     * Allows deployer IP access (TCP 0-65535).
68//!     * Binary instance traffic to Loki (TCP 3100) and Tempo (TCP 4318).
69//!
70//! ### Binary
71//!
72//! * Deployed in user-specified regions with configurable ARM64 or AMD64 instance types and storage.
73//!   Instances whose type includes EC2 NVMe instance store automatically mount it at `/home/ubuntu`.
74//! * Run:
75//!     * **Custom Binary**: Executes with `--hosts=/home/ubuntu/hosts.yaml --config=/home/ubuntu/config.conf`, exposing metrics at `:9090`.
76//!       The binary uses the default allocator on the target Linux platform.
77//!     * **Promtail**: Forwards `/var/log/binary.log` to Loki on the monitoring instance.
78//!     * **Node Exporter**: Exposes system metrics at `:9100`.
79//!     * **Pyroscope Agent**: Forwards `perf` profiles to Pyroscope on the monitoring instance.
80//! * Ingress:
81//!     * Deployer IP access (TCP 0-65535).
82//!     * Monitoring IP access to `:9090` and `:9100` for Prometheus.
83//!     * User-defined ports from the configuration.
84//!
85//! _For allocator-sensitive workloads, consider compiling the binary with `jemalloc` or
86//! `mimalloc`._
87//!
88//! ## Networking
89//!
90//! ### VPCs
91//!
92//! One per region with CIDR `10.<region-index>.0.0/16` (e.g., `10.0.0.0/16` for `us-east-1`).
93//!
94//! ### Subnets
95//!
96//! One subnet per availability zone that supports any required instance type in the region
97//! (e.g., `10.<region-index>.<az-index>.0/24`), linked to a shared route table with an internet gateway.
98//! Each instance is placed in an AZ that supports its instance type, distributed round-robin across
99//! eligible AZs, with automatic fallback to other AZs on capacity errors.
100//! Instances that set the same `availability_zone_group` and region are launched in one
101//! mutually-supported AZ.
102//!
103//! ### VPC Peering
104//!
105//! Connects the monitoring VPC to each binary VPC, with routes added to route tables for private communication.
106//!
107//! ### Security Groups
108//!
109//! Separate for monitoring (tag) and binary instances (`{tag}-binary`), dynamically configured for deployer and inter-instance traffic.
110//!
111//! # Workflow
112//!
113//! ## Lifecycle
114//!
115//! Deployments are managed through `aws create`, `aws update`, and `aws destroy`. Stopping,
116//! starting, or rebooting EC2 instances outside this lifecycle is not supported. Deployment
117//! metadata, generated host files, security group rules, monitoring scrape targets, and service
118//! configuration are derived from the instance addresses observed during `aws create`.
119//! Additionally, instance types with EC2 NVMe instance store use ephemeral storage mounted at
120//! `/home/ubuntu`.
121//!
122//! ## `aws create`
123//!
124//! 1. Validates configuration and generates an SSH key pair, stored in `$HOME/.commonware_deployer/{tag}/id_rsa_{tag}`.
125//! 2. Persists deployment metadata (tag, regions, instance names) to `$HOME/.commonware_deployer/{tag}/metadata.yaml`.
126//!    This enables `destroy --tag` cleanup if creation fails.
127//! 3. Ensures the shared S3 bucket exists and caches tools if not already present.
128//! 4. Caches required container images as `docker save` tarballs in S3 (one per architecture) if not already present.
129//! 5. Uploads deployment-specific files (binaries, configs) to S3.
130//! 6. Creates VPCs, subnets, internet gateways, route tables, and security groups per region (concurrently).
131//! 7. Establishes VPC peering between the monitoring region and binary regions.
132//! 8. Launches the monitoring instance.
133//! 9. Launches binary instances.
134//! 10. Caches all static config files and uploads per-instance configs (hosts.yaml, Promtail, Pyroscope) to S3.
135//! 11. Configures monitoring and binary instances in parallel via SSH (BBR, service installation, service startup).
136//! 12. Updates the monitoring security group to allow telemetry traffic from binary instances.
137//! 13. Marks completion with `$HOME/.commonware_deployer/{tag}/created`.
138//!
139//! ## `aws update`
140//!
141//! Performs rolling updates across all binary instances:
142//!
143//! 1. Uploads the latest binary and configuration to S3.
144//! 2. For each instance (up to `--concurrency` at a time, default 128):
145//!    a. Stops the `binary` service.
146//!    b. Downloads the updated files from S3 via pre-signed URLs.
147//!    c. Restarts the `binary` service.
148//!    d. Waits for the service to become active before proceeding.
149//!
150//! _Use `--concurrency 1` for fully sequential updates that wait for each instance to be healthy
151//! before updating the next._
152//!
153//! ## `aws authorize`
154//!
155//! 1. Obtains the deployer's current public IP address (or parses the one provided).
156//! 2. For each security group in the deployment, adds an ingress rule for the IP (if it doesn't already exist).
157//!
158//! ## `aws destroy`
159//!
160//! Can be invoked with either `--config <path>` or `--tag <tag>`. When using `--tag`, the command
161//! reads regions from the persisted `metadata.yaml` file, allowing destruction without the original
162//! config file.
163//!
164//! 1. Terminates all instances across regions.
165//! 2. Deletes security groups, subnets, route tables, VPC peering connections, internet gateways, key pairs, and VPCs in dependency order.
166//! 3. Deletes deployment-specific data from S3 (cached tools and images remain for future deployments).
167//! 4. Marks destruction with `$HOME/.commonware_deployer/{tag}/destroyed`, retaining the directory to prevent tag reuse.
168//!
169//! ## `aws clean`
170//!
171//! 1. Deletes the shared S3 bucket and all its contents (cached tools, image tarballs, and any remaining deployment data).
172//! 2. Use this to fully clean up when you no longer need the deployer cache.
173//!
174//! ## `aws list`
175//!
176//! Lists all active deployments (created but not destroyed). For each deployment, displays the tag,
177//! creation timestamp, regions, and number of instances.
178//!
179//! ## `aws profile`
180//!
181//! 1. Loads the deployment configuration and locates the specified instance.
182//! 2. Caches the samply binary in S3 if not already present.
183//! 3. SSHes to the instance, downloads samply, and records a CPU profile of the running binary for the specified duration.
184//! 4. Downloads the profile locally via SCP.
185//! 5. Opens Firefox Profiler with symbols resolved from your local debug binary.
186//!
187//! # Profiling
188//!
189//! The deployer supports two profiling modes:
190//!
191//! ## Continuous Profiling (Pyroscope)
192//!
193//! Enable continuous CPU profiling by setting `profiling: true` in your instance config. This runs
194//! Pyroscope in the background, continuously collecting profiles that are viewable in the Grafana
195//! dashboard on the monitoring instance.
196//!
197//! For best results, build and deploy your binary with debug symbols and frame pointers:
198//!
199//! ```bash
200//! CARGO_PROFILE_RELEASE_DEBUG=true RUSTFLAGS="-C force-frame-pointers=yes" cargo build --release
201//! ```
202//!
203//! ## On-Demand Profiling (samply)
204//!
205//! To generate an on-demand CPU profile (viewable in the Firefox Profiler UI), run the
206//! following:
207//!
208//! ```bash
209//! deployer aws profile --config config.yaml --instance <name> --binary <path-to-binary-with-debug>
210//! ```
211//!
212//! This captures a 30-second profile (configurable with `--duration`) using samply on the remote
213//! instance, downloads it, and opens it in Firefox Profiler. Unlike Continuous Profiling, this mode
214//! does not require deploying a binary with debug symbols (reducing deployment time).
215//!
216//! Like above, build your binary with debug symbols (but not frame pointers):
217//!
218//! ```bash
219//! CARGO_PROFILE_RELEASE_DEBUG=true cargo build --release
220//! ```
221//!
222//! Now, strip symbols and deploy via `aws create` (preserve the original binary for profile symbolication
223//! when you run the `aws profile` command shown above):
224//!
225//! ```bash
226//! cp target/release/my-binary target/release/my-binary-debug
227//! strip target/release/my-binary
228//! ```
229//!
230//! # Persistence
231//!
232//! * A directory `$HOME/.commonware_deployer/{tag}` stores:
233//!   * SSH private key (`id_rsa_{tag}`)
234//!   * Deployment metadata (`metadata.yaml`) containing tag, creation timestamp, regions, and instance names
235//!   * Status files (`created`, `destroyed`)
236//! * The deployment state is tracked via these files, ensuring operations respect prior create/destroy actions.
237//! * The `metadata.yaml` file enables `aws destroy --tag` and `aws list` to work without the original config file.
238//!
239//! ## S3 Caching
240//!
241//! A shared S3 cache avoids repeated downloads from upstream sources. The cache name is stored in
242//! `$HOME/.commonware_deployer/bucket` and reused by later deployments.
243//!
244//! 1. **Faster deployments**: Tools are downloaded from upstream sources once and cached in S3.
245//!    Required container images are saved (via `docker save`) as per-architecture tarballs once and
246//!    `docker load`ed during instance setup, so instances never authenticate against a registry.
247//!
248//! 2. **Reduced bandwidth**: Instead of requiring the deployer to push binaries to each instance,
249//!    unique binaries are uploaded once to S3 and then pulled from there.
250//!
251//! Per-deployment data (binaries, configs, hosts files) is isolated under `deployments/{tag}/` to prevent
252//! conflicts between concurrent deployments.
253//!
254//! The bucket stores:
255//!   * `tools/binaries/{tool}/{version}/{platform}/{filename}` - Tool binaries and packages
256//!   * `tools/binaries/images/{image}/{platform}/image.tar.gz` - Cached container image tarballs
257//!   * `tools/configs/{deployer-version}/{component}/{file}` - Static configs and service files
258//!   * `deployments/{tag}/{kind}/{digest}` - Deployment-specific files, deduplicated by content
259//!     digest (kinds: `binaries`, `configs`, `hosts`, `promtail`, `pyroscope`, `monitoring`)
260//!
261//! Cached packages and images are namespaced by version and platform. Static configs are namespaced
262//! by deployer version to ensure cache invalidation when the deployer is updated.
263//!
264//! # Example Configuration
265//!
266//! ```yaml
267//! tag: ffa638a0-991c-442c-8ec4-aa4e418213a5
268//! monitoring:
269//!   instance_type: t4g.small  # ARM64 (Graviton)
270//!   storage_size: 10
271//!   storage_class: gp2
272//!   # storage_iops: 3000  # Required for io1/io2, optional for gp3.
273//!   # storage_throughput: 125  # Optional for gp3.
274//!   dashboard: /path/to/dashboard.json
275//! instances:
276//!   - name: node1
277//!     region: us-east-1
278//!     instance_type: t4g.small  # ARM64 (Graviton)
279//!     storage_size: 10
280//!     storage_class: gp2
281//!     # storage_iops: 3000
282//!     # storage_throughput: 125
283//!     binary: /path/to/binary-arm64
284//!     config: /path/to/config.conf
285//!     profiling: true
286//!   - name: node2
287//!     region: us-west-2
288//!     instance_type: t3.small  # x86_64 (Intel/AMD)
289//!     storage_size: 10
290//!     storage_class: gp2
291//!     # storage_iops: 3000
292//!     # storage_throughput: 125
293//!     binary: /path/to/binary-x86
294//!     config: /path/to/config2.conf
295//!     profiling: false
296//! ports:
297//!   - protocol: tcp
298//!     port: 4545
299//!     cidr: 0.0.0.0/0
300//! ```
301
302use serde::{Deserialize, Serialize};
303use std::net::IpAddr;
304
305cfg_if::cfg_if! {
306    if #[cfg(feature = "aws")] {
307        use std::path::PathBuf;
308        use thiserror::Error;
309
310        /// CPU architecture for EC2 instances
311        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
312        pub enum Architecture {
313            Arm64,
314            X86_64,
315        }
316
317        impl Architecture {
318            /// Returns the architecture string used in AMI names, download URLs, and labels
319            pub const fn as_str(&self) -> &'static str {
320                match self {
321                    Self::Arm64 => "arm64",
322                    Self::X86_64 => "amd64",
323                }
324            }
325
326        }
327
328        impl std::fmt::Display for Architecture {
329            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
330                f.write_str(self.as_str())
331            }
332        }
333
334        /// Metadata persisted during deployment creation
335        #[derive(Serialize, Deserialize)]
336        pub struct Metadata {
337            pub tag: String,
338            pub created_at: u64,
339            pub regions: Vec<String>,
340            pub instance_count: usize,
341        }
342
343        mod create;
344        pub mod ec2;
345        mod images;
346        pub mod services;
347        pub use create::create;
348        mod update;
349        pub use update::update;
350        mod authorize;
351        pub use authorize::authorize;
352        mod destroy;
353        pub use destroy::destroy;
354        mod clean;
355        pub use clean::clean;
356        mod profile;
357        pub use profile::profile;
358        mod attach;
359        pub use attach::attach;
360        mod list;
361        pub use list::list;
362        pub mod s3;
363        pub mod utils;
364
365        /// Name of the monitoring instance
366        const MONITORING_NAME: &str = "monitoring";
367
368        /// AWS region where monitoring instances are deployed
369        const MONITORING_REGION: &str = "us-east-1";
370
371        /// File name that indicates the deployment completed
372        const CREATED_FILE_NAME: &str = "created";
373
374        /// File name that indicates the deployment was destroyed
375        const DESTROYED_FILE_NAME: &str = "destroyed";
376
377        /// File name for deployment metadata
378        const METADATA_FILE_NAME: &str = "metadata.yaml";
379
380        /// Port on instance where system metrics are exposed
381        const SYSTEM_PORT: u16 = 9100;
382
383        /// Port on monitoring where logs are pushed
384        const LOGS_PORT: u16 = 3100;
385
386        /// Port on monitoring where profiles are pushed
387        const PROFILES_PORT: u16 = 4040;
388
389        /// Port on monitoring where traces are pushed
390        const TRACES_PORT: u16 = 4318;
391
392        /// Maximum instances to manipulate at one time
393        pub const DEFAULT_CONCURRENCY: &str = "128";
394
395        /// Subcommand name
396        pub const CMD: &str = "aws";
397
398        /// Create subcommand name
399        pub const CREATE_CMD: &str = "create";
400
401        /// Update subcommand name
402        pub const UPDATE_CMD: &str = "update";
403
404        /// Authorize subcommand name
405        pub const AUTHORIZE_CMD: &str = "authorize";
406
407        /// Destroy subcommand name
408        pub const DESTROY_CMD: &str = "destroy";
409
410        /// Clean subcommand name
411        pub const CLEAN_CMD: &str = "clean";
412
413        /// Profile subcommand name
414        pub const PROFILE_CMD: &str = "profile";
415
416        /// Attach subcommand name
417        pub const ATTACH_CMD: &str = "attach";
418
419        /// List subcommand name
420        pub const LIST_CMD: &str = "list";
421
422        /// Directory where deployer files are stored
423        fn deployer_directory(tag: Option<&str>) -> PathBuf {
424            let base_dir = std::env::var("HOME").expect("$HOME is not configured");
425            let path = PathBuf::from(base_dir).join(".commonware_deployer");
426            match tag {
427                Some(tag) => path.join(tag),
428                None => path,
429            }
430        }
431
432        /// S3 operations that can fail
433        #[derive(Debug, Clone, Copy)]
434        pub enum S3Operation {
435            CreateBucket,
436            DeleteBucket,
437            HeadObject,
438            ListObjects,
439            DeleteObjects,
440        }
441
442        /// Reasons why accessing a bucket may be forbidden
443        #[derive(Debug, Clone, Copy)]
444        pub enum BucketForbiddenReason {
445            /// Access denied (missing s3:ListBucket permission or bucket owned by another account)
446            AccessDenied,
447        }
448
449        impl std::fmt::Display for BucketForbiddenReason {
450            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
451                match self {
452                    Self::AccessDenied => write!(
453                        f,
454                        "access denied (check IAM permissions or bucket ownership)"
455                    ),
456                }
457            }
458        }
459
460        impl std::fmt::Display for S3Operation {
461            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
462                match self {
463                    Self::CreateBucket => write!(f, "CreateBucket"),
464                    Self::DeleteBucket => write!(f, "DeleteBucket"),
465                    Self::HeadObject => write!(f, "HeadObject"),
466                    Self::ListObjects => write!(f, "ListObjects"),
467                    Self::DeleteObjects => write!(f, "DeleteObjects"),
468                }
469            }
470        }
471
472        /// Errors that can occur when deploying infrastructure on AWS
473        #[derive(Error, Debug)]
474        pub enum Error {
475            #[error("AWS EC2 error: {0}")]
476            AwsEc2(#[from] aws_sdk_ec2::Error),
477            #[error("AWS security group ingress error: {0}")]
478            AwsSecurityGroupIngress(#[from] aws_sdk_ec2::operation::authorize_security_group_ingress::AuthorizeSecurityGroupIngressError),
479            #[error("AWS describe instances error: {0}")]
480            AwsDescribeInstances(
481                #[from] aws_sdk_ec2::operation::describe_instances::DescribeInstancesError,
482            ),
483            #[error("S3 operation failed: {operation} on bucket '{bucket}'")]
484            AwsS3 {
485                bucket: String,
486                operation: S3Operation,
487                #[source]
488                source: Box<aws_sdk_s3::Error>,
489            },
490            #[error("S3 bucket '{bucket}' forbidden: {reason}")]
491            S3BucketForbidden {
492                bucket: String,
493                reason: BucketForbiddenReason,
494            },
495            #[error("IO error: {0}")]
496            Io(#[from] std::io::Error),
497            #[error("YAML error: {0}")]
498            Yaml(#[from] serde_yaml::Error),
499            #[error("creation already attempted")]
500            CreationAttempted,
501            #[error("invalid instance name: {0}")]
502            InvalidInstanceName(String),
503            #[error("invalid storage class for {target}: {storage_class}")]
504            InvalidStorageClass {
505                target: String,
506                storage_class: String,
507            },
508            #[error("storage_iops is required for {target} when storage_class is {storage_class}")]
509            MissingStorageIops {
510                target: String,
511                storage_class: String,
512            },
513            #[error("storage_iops for {target} is invalid for storage_class {storage_class}: {storage_iops}")]
514            InvalidStorageIops {
515                target: String,
516                storage_class: String,
517                storage_iops: i32,
518            },
519            #[error("storage_throughput is only supported for {target} when storage_class is gp3: {storage_class}")]
520            UnsupportedStorageThroughput {
521                target: String,
522                storage_class: String,
523            },
524            #[error("storage_throughput for {target} must be between 125 and 2000 MiB/s: {storage_throughput}")]
525            InvalidStorageThroughput {
526                target: String,
527                storage_throughput: i32,
528            },
529            #[error("reqwest error: {0}")]
530            Reqwest(#[from] reqwest::Error),
531            #[error("SSH failed")]
532            SshFailed,
533            #[error("keygen failed")]
534            KeygenFailed,
535            #[error("service timeout({0}): {1}")]
536            ServiceTimeout(String, String),
537            #[error("deployment does not exist: {0}")]
538            DeploymentDoesNotExist(String),
539            #[error("deployment is not complete: {0}")]
540            DeploymentNotComplete(String),
541            #[error("deployment already destroyed: {0}")]
542            DeploymentAlreadyDestroyed(String),
543            #[error("private key not found")]
544            PrivateKeyNotFound,
545            #[error("invalid IP address: {0}")]
546            IpAddrParse(#[from] std::net::AddrParseError),
547            #[error("IP address is not IPv4: {0}")]
548            IpAddrNotV4(std::net::IpAddr),
549            #[error("download failed: {0}")]
550            DownloadFailed(String),
551            #[error("command failed: {command}: {stderr}")]
552            CommandFailed { command: String, stderr: String },
553            #[error("S3 presigning config error: {0}")]
554            S3PresigningConfig(#[from] aws_sdk_s3::presigning::PresigningConfigError),
555            #[error("S3 presigning failed: {0}")]
556            S3PresigningFailed(
557                Box<aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::get_object::GetObjectError>>,
558            ),
559            #[error("S3 builder error: {0}")]
560            S3Builder(#[from] aws_sdk_s3::error::BuildError),
561            #[error("duplicate instance name: {0}")]
562            DuplicateInstanceName(String),
563            #[error("instance not found: {0}")]
564            InstanceNotFound(String),
565            #[error("symbolication failed: {0}")]
566            Symbolication(String),
567            #[error("no subnet supports instance type: {0}")]
568            UnsupportedInstanceType(String),
569            #[error("no subnets available")]
570            NoSubnetsAvailable,
571            #[error("availability zone group '{group}' in region '{region}' has no mutually-supported AZ for instance types {instance_types:?}")]
572            AvailabilityZoneGroupUnsupported {
573                region: String,
574                group: String,
575                instance_types: Vec<String>,
576            },
577            #[error("metadata not found for deployment: {0}")]
578            MetadataNotFound(String),
579            #[error("must specify either --config or --tag")]
580            MissingTagOrConfig,
581            #[error("regions not enabled: {0:?}")]
582            RegionsNotEnabled(Vec<String>),
583        }
584
585        impl From<aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::get_object::GetObjectError>>
586            for Error
587        {
588            fn from(
589                err: aws_sdk_s3::error::SdkError<aws_sdk_s3::operation::get_object::GetObjectError>,
590            ) -> Self {
591                Self::S3PresigningFailed(Box::new(err))
592            }
593        }
594    }
595}
596
597/// Port on binary where metrics are exposed
598pub const METRICS_PORT: u16 = 9090;
599
600/// Host deployment information
601#[derive(Serialize, Deserialize, Clone)]
602pub struct Host {
603    /// Name of the host
604    pub name: String,
605
606    /// Region where the host is deployed
607    pub region: String,
608
609    /// Public IP address of the host
610    pub ip: IpAddr,
611}
612
613/// Instance IP addresses.
614#[derive(Serialize, Deserialize, Clone)]
615pub struct Ips {
616    /// Public IP address of the instance.
617    pub public: IpAddr,
618
619    /// Private IP address of the instance.
620    pub private: IpAddr,
621}
622
623/// List of hosts
624#[derive(Serialize, Deserialize, Clone)]
625pub struct Hosts {
626    /// Public and private IP addresses of the monitoring instance.
627    pub monitoring: Ips,
628
629    /// Hosts deployed across all regions
630    pub hosts: Vec<Host>,
631}
632
633/// Port configuration
634#[derive(Serialize, Deserialize, Clone)]
635pub struct PortConfig {
636    /// Protocol (e.g., "tcp")
637    pub protocol: String,
638
639    /// Port number
640    pub port: u16,
641
642    /// CIDR block
643    pub cidr: String,
644}
645
646/// Instance configuration
647#[derive(Serialize, Deserialize, Clone)]
648pub struct InstanceConfig {
649    /// Name of the instance
650    pub name: String,
651
652    /// AWS region where the instance is deployed
653    pub region: String,
654
655    /// Optional group whose instances in the same region must launch in the same availability zone.
656    ///
657    /// The same group name may be reused in different regions, but each region is resolved
658    /// independently and the group will not span regions.
659    #[serde(default, skip_serializing_if = "Option::is_none")]
660    pub availability_zone_group: Option<String>,
661
662    /// Instance type (e.g., `t4g.small` for ARM64, `t3.small` for x86_64)
663    pub instance_type: String,
664
665    /// Storage size in GB
666    pub storage_size: i32,
667
668    /// Storage class (e.g., "gp2")
669    pub storage_class: String,
670
671    /// Provisioned IOPS for volumes that support it
672    #[serde(default, skip_serializing_if = "Option::is_none")]
673    pub storage_iops: Option<i32>,
674
675    /// Provisioned throughput in MiB/s for volumes that support it
676    #[serde(default, skip_serializing_if = "Option::is_none")]
677    pub storage_throughput: Option<i32>,
678
679    /// Path to the binary to deploy
680    pub binary: String,
681
682    /// Path to the binary configuration file
683    pub config: String,
684
685    /// Whether to enable profiling
686    pub profiling: bool,
687}
688
689/// Monitoring configuration
690#[derive(Serialize, Deserialize, Clone)]
691pub struct MonitoringConfig {
692    /// Instance type (e.g., `t4g.small` for ARM64, `t3.small` for x86_64)
693    pub instance_type: String,
694
695    /// Storage size in GB
696    pub storage_size: i32,
697
698    /// Storage class (e.g., "gp2")
699    pub storage_class: String,
700
701    /// Provisioned IOPS for volumes that support it
702    #[serde(default, skip_serializing_if = "Option::is_none")]
703    pub storage_iops: Option<i32>,
704
705    /// Provisioned throughput in MiB/s for volumes that support it
706    #[serde(default, skip_serializing_if = "Option::is_none")]
707    pub storage_throughput: Option<i32>,
708
709    /// Path to a custom dashboard file that is automatically
710    /// uploaded to grafana
711    pub dashboard: String,
712}
713
714/// Deployer configuration
715#[derive(Serialize, Deserialize, Clone)]
716pub struct Config {
717    /// Unique tag for the deployment
718    pub tag: String,
719
720    /// Monitoring instance configuration
721    pub monitoring: MonitoringConfig,
722
723    /// Instance configurations
724    pub instances: Vec<InstanceConfig>,
725
726    /// Ports open on all instances
727    pub ports: Vec<PortConfig>,
728}