feat(aws): optional per-user EC2 capacity in the onboarding module
Phase 7 Task 3 (E1-E4, E11; onboarding module version 2): - ec2.tf: with ec2_instance_type set, the user's own cluster triple-c-<env>--u-<ec2_user_tag> with exactly one capacity provider (managed termination protection, managed scaling 100% one instance at a time, 60 s warm-up), an Auto Scaling group of 0..ec2_max_instances (desired left to ECS, protect_from_scale_in, force_delete off so the provider drains before deleting, fixed launch-template version, default tags + AmazonECSManaged), a launch template on the ECS-optimized AL2023 AMI by architecture (IMDSv2 hop 1, 50 GiB gp3 encrypted root, the environment's tasks group, public IP as the environment's tasks get one, ECS_CLUSTER + ECS_AWSVPC_BLOCK_IMDS), the instance role triple-c-<env>-<owner key>-instance (bounded, tagged triple-c:cluster, ECS agent on its cluster only, ECR pull, logs; no SSM core) and its instance profile, and the user record /triple-c/users/<env>/<owner key>. Empty type (the default): count 0 everywhere, Fargate plan unchanged. - Variables ec2_instance_type (the app's 30 types), ec2_max_instances (1-4), ec2_user_tag (as validate_ec2_user_tag, required with a type); onboarding_vars takes Option<&Ec2Compute> and validates the same. - The environment record's assign_public_ip is now format-checked. - tofu_modules.rs: ONBOARD_MODULE_VERSION 2, user_record_parameter_name, USER_RECORD_FIELDS, Ec2Compute, EC2_MAX_INSTANCES, ec2.tf embedded. - Tests: EC2 calls merged into MODULE_CALLS under real types (all_module_calls removed); gating + version-1 resource fingerprint; launch template, group, capacity provider, instance role, AMI by arch, record fields, names, variable rules; the instance role inside the boundary with its cluster principal tag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9829ff0c43
commit
9410c098ed
9 files changed
+1430
-196
No files matched your search
@@ -1592,7 +1592,7 @@ mod environment {
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, &t.e.region, &owner),
|
||||
&onboarding_vars(&name, &t.e.region, &owner, None).unwrap(),
|
||||
TofuOp::Apply,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
@@ -1643,7 +1643,7 @@ mod environment {
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, &t.e.region, &owner),
|
||||
&onboarding_vars(&name, &t.e.region, &owner, None).unwrap(),
|
||||
TofuOp::Destroy,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
@@ -2003,7 +2003,7 @@ mod environment {
|
||||
.run(
|
||||
ModuleKind::UserOnboard,
|
||||
Deployment::User { env: &name, owner_key: &okey },
|
||||
&onboarding_vars(&name, ®ion, &owner),
|
||||
&onboarding_vars(&name, ®ion, &owner, None).unwrap(),
|
||||
TofuOp::Apply,
|
||||
t.keys_env(&keys),
|
||||
)
|
||||
|
||||
@@ -16,9 +16,11 @@ use crate::models::host_connection::validate_supported_region;
|
||||
/// `locals.template_version` (and the `triple-c:template-version` default
|
||||
/// tag) of `aws/terraform/environment`.
|
||||
pub const ENVIRONMENT_MODULE_VERSION: u32 = 2;
|
||||
/// The same for the user-onboarding module.
|
||||
/// The same for the user-onboarding module. 2: optional per-user EC2
|
||||
/// capacity (phase 7); an onboarding without it plans as version 1 did,
|
||||
/// apart from this tag.
|
||||
#[allow(dead_code)] // the onboarding module's version; checked against its HCL by a test
|
||||
pub const ONBOARD_MODULE_VERSION: u32 = 1;
|
||||
pub const ONBOARD_MODULE_VERSION: u32 = 2;
|
||||
/// The same for the permissions-boundary module. 2: the instance role's ECS
|
||||
/// agent and Session Manager actions (phase 7, E4); an account admin
|
||||
/// re-applies the boundary once.
|
||||
@@ -57,6 +59,7 @@ const USER_ONBOARD_FILES: &[(&str, &str)] = module!("user-onboard":
|
||||
"main.tf",
|
||||
"iam.tf",
|
||||
"efs.tf",
|
||||
"ec2.tf",
|
||||
"outputs.tf",
|
||||
".terraform.lock.hcl",
|
||||
);
|
||||
@@ -148,7 +151,6 @@ pub const EC2_USER_TAG_KEY: &str = "triple-c-user";
|
||||
/// `[A-Za-z0-9-]`, not starting or ending with a hyphen and without `--`
|
||||
/// (the separator in [`user_ec2_stem`], which environment names can't hold
|
||||
/// either, so a per-user name can be read back only one way).
|
||||
#[allow(dead_code)] // the onboarding module (Task 3) uses it
|
||||
pub fn validate_ec2_user_tag(tag: &str) -> Result<String, String> {
|
||||
let ok = (1..=32).contains(&tag.len())
|
||||
&& tag.bytes().all(|c| c.is_ascii_alphanumeric() || c == b'-')
|
||||
@@ -171,7 +173,7 @@ pub fn validate_ec2_user_tag(tag: &str) -> Result<String, String> {
|
||||
/// characters). Environment names and user tags never contain `--`, so the
|
||||
/// workspace policy's `cluster/triple-c-*--u-*` matches exactly the
|
||||
/// per-user clusters, and `triple-c-*--u-<tag>` exactly that user's.
|
||||
#[allow(dead_code)] // the onboarding module (Task 3) uses it
|
||||
#[allow(dead_code)] // the onboarding module builds it; the app's EC2 runs (Task 5) will
|
||||
pub fn user_ec2_stem(env: &str, user_tag: &str) -> String {
|
||||
format!("triple-c-{env}--u-{user_tag}")
|
||||
}
|
||||
@@ -180,11 +182,48 @@ pub fn user_ec2_stem(env: &str, user_tag: &str) -> String {
|
||||
/// not the user tag, because role names are account-global and at most 64
|
||||
/// characters (this is at most 51). The admin's PassRole to EC2 names
|
||||
/// `role/triple-c-*-instance`.
|
||||
#[allow(dead_code)] // the onboarding module (Task 3) uses it
|
||||
#[allow(dead_code)] // the onboarding module builds it; pinned by tests
|
||||
pub fn user_instance_role_name(env: &str, owner_key: &str) -> String {
|
||||
format!("triple-c-{env}-{owner_key}-instance")
|
||||
}
|
||||
|
||||
/// The SSM parameter holding a user's EC2 record (E11), written by their
|
||||
/// onboarding module only while it has EC2 capacity: a String of JSON with
|
||||
/// exactly [`USER_RECORD_FIELDS`]. Workspace users can read it
|
||||
/// (`parameter/triple-c/*`) though not the state bucket; it lives outside
|
||||
/// `/triple-c/environments/`, which environment detection lists.
|
||||
#[allow(dead_code)] // the app reads it from Task 4 on
|
||||
pub fn user_record_parameter_name(env: &str, owner_key: &str) -> String {
|
||||
format!("/triple-c/users/{env}/{owner_key}")
|
||||
}
|
||||
|
||||
/// The fields of a user's EC2 record, schema 1: `instance_type`,
|
||||
/// `cluster_arn` (the user's cluster, [`user_ec2_stem`]),
|
||||
/// `capacity_provider` (its one provider, the same name) and `user_tag`
|
||||
/// (the `triple-c-user` value the cluster is named after).
|
||||
#[allow(dead_code)] // the app reads it from Task 4 on
|
||||
pub const USER_RECORD_FIELDS: [&str; 5] = [
|
||||
"schema",
|
||||
"instance_type",
|
||||
"cluster_arn",
|
||||
"capacity_provider",
|
||||
"user_tag",
|
||||
];
|
||||
|
||||
/// The most instances a user's EC2 capacity may run at once.
|
||||
pub const EC2_MAX_INSTANCES: u32 = 4;
|
||||
|
||||
/// A user's optional EC2 capacity, as the onboarding module takes it.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Ec2Compute {
|
||||
/// One of the types `ec2_instance_spec` knows.
|
||||
pub instance_type: String,
|
||||
/// 1 to [`EC2_MAX_INSTANCES`].
|
||||
pub max_instances: u32,
|
||||
/// The user's `triple-c-user` principal tag value.
|
||||
pub user_tag: String,
|
||||
}
|
||||
|
||||
/// Whether a project id is used in names as it is: 1 to 64 characters of
|
||||
/// `[A-Za-z0-9-]`. Every UUID (how the app makes ids) is.
|
||||
fn is_clean_project_id(id: &str) -> bool {
|
||||
@@ -400,16 +439,46 @@ pub fn environment_vars(
|
||||
}
|
||||
|
||||
/// The onboarding module's `terraform.tfvars.json` for the user whose
|
||||
/// `aws:userid` is `owner_id`. Bedrock stays on the workspace user's own
|
||||
/// credentials unless a later setting opts the task role in.
|
||||
pub fn onboarding_vars(env: &str, region: &str, owner_id: &str) -> serde_json::Value {
|
||||
json!({
|
||||
/// `aws:userid` is `owner_id`, with their EC2 capacity or none (empty
|
||||
/// `ec2_instance_type`: the module makes nothing for EC2). Applies the same
|
||||
/// rules as the module's EC2 variable validations. Bedrock stays on the
|
||||
/// workspace user's own credentials unless a later setting opts the task
|
||||
/// role in.
|
||||
pub fn onboarding_vars(
|
||||
env: &str,
|
||||
region: &str,
|
||||
owner_id: &str,
|
||||
ec2: Option<&Ec2Compute>,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
let (instance_type, max_instances, user_tag) = match ec2 {
|
||||
None => (String::new(), 1, String::new()),
|
||||
Some(c) => {
|
||||
if crate::models::project::ec2_instance_spec(&c.instance_type).is_none() {
|
||||
return Err(format!(
|
||||
"\"{}\" is not an instance type Triple-C supports (m7i, m7g, c7i, c7g, r7i, \
|
||||
r7g from large to 4xlarge; t3, t4g from large to 2xlarge)",
|
||||
c.instance_type.escape_debug()
|
||||
));
|
||||
}
|
||||
if !(1..=EC2_MAX_INSTANCES).contains(&c.max_instances) {
|
||||
return Err(format!(
|
||||
"The number of instances must be from 1 to {EC2_MAX_INSTANCES}"
|
||||
));
|
||||
}
|
||||
let tag = validate_ec2_user_tag(&c.user_tag)?;
|
||||
(c.instance_type.clone(), c.max_instances, tag)
|
||||
}
|
||||
};
|
||||
Ok(json!({
|
||||
"environment_name": env,
|
||||
"region": region,
|
||||
"owner_id": owner_id,
|
||||
"owner_key": owner_key(region, owner_id),
|
||||
"bedrock_on_task_role": false,
|
||||
})
|
||||
"ec2_instance_type": instance_type,
|
||||
"ec2_max_instances": max_instances,
|
||||
"ec2_user_tag": user_tag,
|
||||
}))
|
||||
}
|
||||
|
||||
/// The boundary module's `terraform.tfvars.json`: the shipped boundary
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1331,7 +1331,7 @@ pub async fn onboard_aws_user(
|
||||
let owner = validate_owner_id(&ctx.who.user_id).map_err(other)?;
|
||||
let found = detect(&ctx).await?;
|
||||
onboard_refusal(&found, &env).map_err(other)?;
|
||||
let vars = onboarding_vars(&env, &ctx.region, &owner);
|
||||
let vars = onboarding_vars(&env, &ctx.region, &owner, None).map_err(other)?;
|
||||
let key = DeploymentKey::User {
|
||||
owner_key: owner_key(&ctx.region, &owner),
|
||||
env,
|
||||
@@ -1420,7 +1420,7 @@ pub async fn save_aws_admin_bundle(
|
||||
.map(str::trim)
|
||||
.ok_or_else(|| other("The user's owner id (aws:userid) is required"))
|
||||
.and_then(|o| validate_owner_id(o).map_err(other))?;
|
||||
let vars = onboarding_vars(&env, ®ion_ok, &owner);
|
||||
let vars = onboarding_vars(&env, ®ion_ok, &owner, None).map_err(other)?;
|
||||
(
|
||||
DeploymentKey::User {
|
||||
owner_key: owner_key(®ion_ok, &owner),
|
||||
|
||||
@@ -195,7 +195,7 @@ fn bundles_contain_the_module_backend_vars_and_readme() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let ok = owner_key(REGION, USER_ID);
|
||||
let d = Deployment::User { env: "dev", owner_key: &ok };
|
||||
let vars = onboarding_vars("dev", REGION, USER_ID);
|
||||
let vars = onboarding_vars("dev", REGION, USER_ID, None).unwrap();
|
||||
let written = write_bundle(dir.path(), d, ACCOUNT, REGION, &vars).unwrap();
|
||||
|
||||
for (name, text) in module_files(ModuleKind::UserOnboard) {
|
||||
|
||||
@@ -0,0 +1,314 @@
|
||||
# The user's own EC2 capacity (phase 7, E1–E4, E11), made only when
|
||||
# `ec2_instance_type` is set: every resource and data source here has
|
||||
# `count = local.ec2_count`, so an onboarding without it plans exactly as
|
||||
# before. One cluster per user (`triple-c-<env>--u-<user tag>`, the name the
|
||||
# workspace policy lets only that `triple-c-user` principal tag run tasks on)
|
||||
# with exactly one capacity provider: an Auto Scaling group of 0 to
|
||||
# `ec2_max_instances` instances that ECS's managed scaling grows when a task
|
||||
# needs one and shrinks back to zero when none does.
|
||||
#
|
||||
# Removing it (re-applying with an empty type, or destroying) goes in
|
||||
# dependency order: the user record first, then the cluster's provider list
|
||||
# (ECS refuses while a task still runs on the provider, so a running
|
||||
# workspace stops the removal before anything is torn down), the capacity
|
||||
# provider, the group (`force_delete` off: the provider first drains it to
|
||||
# zero, clearing scale-in protection, and waits for the instances to
|
||||
# terminate), the launch template, the instance profile and role, and last
|
||||
# the cluster, which the launch template's user data references so that it
|
||||
# outlives every instance registered into it.
|
||||
|
||||
locals {
|
||||
ec2 = var.ec2_instance_type != ""
|
||||
ec2_count = local.ec2 ? 1 : 0
|
||||
|
||||
# `user_ec2_stem` and `user_instance_role_name` in tofu_modules.rs.
|
||||
ec2_name = "triple-c-${var.environment_name}--u-${var.ec2_user_tag}"
|
||||
instance_role_name = "${local.name}-instance"
|
||||
|
||||
# Graviton families (m7g, c7g, r7g, t4g: a `g` after the generation) run
|
||||
# the arm64 image.
|
||||
ec2_arm64 = can(regex("^[a-z]+[0-9]+g[a-z]*\\.", var.ec2_instance_type))
|
||||
ecs_ami_parameter = (
|
||||
local.ec2_arm64
|
||||
? "/aws/service/ecs/optimized-ami/amazon-linux-2023/arm64/recommended/image_id"
|
||||
: "/aws/service/ecs/optimized-ami/amazon-linux-2023/recommended/image_id"
|
||||
)
|
||||
}
|
||||
|
||||
# The provider's default tags (component, environment, owner, version), for
|
||||
# the places they don't reach by themselves: the launch template's tag
|
||||
# specifications and the group's tags.
|
||||
data "aws_default_tags" "current" {
|
||||
count = local.ec2_count
|
||||
}
|
||||
|
||||
# The current ECS-optimized Amazon Linux 2023 image, resolved at apply time.
|
||||
data "aws_ssm_parameter" "ecs_ami" {
|
||||
count = local.ec2_count
|
||||
name = local.ecs_ami_parameter
|
||||
}
|
||||
|
||||
resource "aws_ecs_cluster" "ec2" {
|
||||
count = local.ec2_count
|
||||
name = local.ec2_name
|
||||
|
||||
setting {
|
||||
name = "containerInsights"
|
||||
value = "disabled"
|
||||
}
|
||||
}
|
||||
|
||||
# The instance's role: the ECS agent on this user's cluster only (the
|
||||
# boundary allows the agent's calls only on the cluster this role's
|
||||
# `triple-c:cluster` tag names), image pulls and the environment's logs.
|
||||
# ECS Exec needs nothing here: its channels run on the task role.
|
||||
resource "aws_iam_role" "instance" {
|
||||
count = local.ec2_count
|
||||
name = local.instance_role_name
|
||||
description = "Triple-C ${var.environment_name}: EC2 instances of ${var.owner_id}"
|
||||
permissions_boundary = local.boundary_arn
|
||||
assume_role_policy = jsonencode({
|
||||
Version = "2012-10-17"
|
||||
Statement = [{
|
||||
Effect = "Allow"
|
||||
Principal = { Service = "ec2.amazonaws.com" }
|
||||
Action = "sts:AssumeRole"
|
||||
}]
|
||||
})
|
||||
|
||||
tags = { "triple-c:cluster" = local.ec2_name }
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy" "instance" {
|
||||
count = local.ec2_count
|
||||
name = "triple-c-instance"
|
||||
role = aws_iam_role.instance[0].id
|
||||
policy = jsonencode({
|
||||
Version = "2012-10-17"
|
||||
Statement = [
|
||||
{
|
||||
Sid = "EcsAgentOnOwnCluster"
|
||||
Effect = "Allow"
|
||||
Action = [
|
||||
"ecs:RegisterContainerInstance",
|
||||
"ecs:DeregisterContainerInstance",
|
||||
"ecs:Poll",
|
||||
"ecs:StartTelemetrySession",
|
||||
"ecs:Submit*",
|
||||
"ecs:UpdateContainerInstancesState",
|
||||
]
|
||||
Resource = [
|
||||
"arn:${local.partition}:ecs:${var.region}:${local.account}:cluster/${local.ec2_name}",
|
||||
"arn:${local.partition}:ecs:${var.region}:${local.account}:container-instance/${local.ec2_name}/*",
|
||||
]
|
||||
},
|
||||
{
|
||||
Sid = "EcsAgentDiscovery"
|
||||
Effect = "Allow"
|
||||
Action = "ecs:DiscoverPollEndpoint"
|
||||
Resource = "*"
|
||||
},
|
||||
{
|
||||
Sid = "EcrAuthorization"
|
||||
Effect = "Allow"
|
||||
Action = "ecr:GetAuthorizationToken"
|
||||
Resource = "*"
|
||||
},
|
||||
{
|
||||
Sid = "PullTripleCImages"
|
||||
Effect = "Allow"
|
||||
Action = [
|
||||
"ecr:BatchGetImage",
|
||||
"ecr:GetDownloadUrlForLayer",
|
||||
"ecr:BatchCheckLayerAvailability",
|
||||
]
|
||||
Resource = [
|
||||
"arn:${local.partition}:ecr:${var.region}:${local.account}:repository/triple-c-*",
|
||||
"arn:${local.partition}:ecr:${var.region}:${local.account}:repository/triple-c/*",
|
||||
]
|
||||
},
|
||||
{
|
||||
# The awslogs driver runs in the instance's Docker daemon.
|
||||
Sid = "WriteEnvironmentLogs"
|
||||
Effect = "Allow"
|
||||
Action = ["logs:CreateLogStream", "logs:PutLogEvents"]
|
||||
Resource = local.log_group_arn
|
||||
},
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
resource "aws_iam_instance_profile" "instance" {
|
||||
count = local.ec2_count
|
||||
name = aws_iam_role.instance[0].name
|
||||
role = aws_iam_role.instance[0].name
|
||||
}
|
||||
|
||||
resource "aws_launch_template" "ec2" {
|
||||
count = local.ec2_count
|
||||
name = local.ec2_name
|
||||
description = "Triple-C ${var.environment_name}: EC2 capacity of ${var.owner_id}"
|
||||
|
||||
image_id = data.aws_ssm_parameter.ecs_ami[0].insecure_value
|
||||
instance_type = var.ec2_instance_type
|
||||
|
||||
iam_instance_profile {
|
||||
name = aws_iam_instance_profile.instance[0].name
|
||||
}
|
||||
|
||||
# The environment's tasks group (outbound only), named in the template so
|
||||
# the admin policy sees it as a launch-template resource. A public IP only
|
||||
# where the environment's tasks get one: in a public-subnet environment
|
||||
# the agent reaches ECS through the internet gateway.
|
||||
network_interfaces {
|
||||
device_index = 0
|
||||
security_groups = [local.env.security_group_id]
|
||||
associate_public_ip_address = local.env.assign_public_ip
|
||||
delete_on_termination = true
|
||||
}
|
||||
|
||||
# IMDSv2 only, one hop: the agent (host network) reaches it, bridge
|
||||
# containers don't, and awsvpc tasks are blocked by the agent below.
|
||||
metadata_options {
|
||||
http_endpoint = "enabled"
|
||||
http_tokens = "required"
|
||||
http_put_response_hop_limit = 1
|
||||
instance_metadata_tags = "disabled"
|
||||
}
|
||||
|
||||
block_device_mappings {
|
||||
device_name = "/dev/xvda"
|
||||
|
||||
ebs {
|
||||
volume_size = 50
|
||||
volume_type = "gp3"
|
||||
encrypted = true
|
||||
delete_on_termination = true
|
||||
}
|
||||
}
|
||||
|
||||
user_data = base64encode(<<-EOT
|
||||
#!/bin/bash
|
||||
cat >>/etc/ecs/ecs.config <<'EOF'
|
||||
ECS_CLUSTER=${aws_ecs_cluster.ec2[0].name}
|
||||
ECS_AWSVPC_BLOCK_IMDS=true
|
||||
EOF
|
||||
EOT
|
||||
)
|
||||
|
||||
# Default tags don't reach what a launch creates: the instance, its
|
||||
# volume and its interface are tagged here (the admin policy lets a launch
|
||||
# create only component-tagged instances and volumes).
|
||||
tag_specifications {
|
||||
resource_type = "instance"
|
||||
tags = data.aws_default_tags.current[0].tags
|
||||
}
|
||||
|
||||
tag_specifications {
|
||||
resource_type = "volume"
|
||||
tags = data.aws_default_tags.current[0].tags
|
||||
}
|
||||
|
||||
tag_specifications {
|
||||
resource_type = "network-interface"
|
||||
tags = data.aws_default_tags.current[0].tags
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_autoscaling_group" "ec2" {
|
||||
count = local.ec2_count
|
||||
name = local.ec2_name
|
||||
vpc_zone_identifier = local.env.subnet_ids
|
||||
|
||||
# Zero when idle; ECS's managed scaling sets the desired count.
|
||||
min_size = 0
|
||||
max_size = var.ec2_max_instances
|
||||
protect_from_scale_in = true
|
||||
force_delete = false
|
||||
|
||||
# A fixed version, never $Latest/$Default (the admin policy refuses a
|
||||
# group that would follow later versions). Running instances keep theirs
|
||||
# until they scale in.
|
||||
launch_template {
|
||||
id = aws_launch_template.ec2[0].id
|
||||
version = aws_launch_template.ec2[0].latest_version
|
||||
}
|
||||
|
||||
# Provider default tags don't apply to groups.
|
||||
dynamic "tag" {
|
||||
for_each = data.aws_default_tags.current[0].tags
|
||||
content {
|
||||
key = tag.key
|
||||
value = tag.value
|
||||
propagate_at_launch = false
|
||||
}
|
||||
}
|
||||
|
||||
# ECS adds this when the capacity provider takes the group over; declared
|
||||
# so a re-apply doesn't remove it.
|
||||
tag {
|
||||
key = "AmazonECSManaged"
|
||||
value = "true"
|
||||
propagate_at_launch = true
|
||||
}
|
||||
|
||||
# The drain before deletion waits for the instances to terminate.
|
||||
timeouts {
|
||||
delete = "20m"
|
||||
}
|
||||
|
||||
lifecycle {
|
||||
ignore_changes = [desired_capacity]
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_ecs_capacity_provider" "ec2" {
|
||||
count = local.ec2_count
|
||||
name = local.ec2_name
|
||||
|
||||
auto_scaling_group_provider {
|
||||
auto_scaling_group_arn = aws_autoscaling_group.ec2[0].arn
|
||||
|
||||
# ECS protects an instance from scale-in while a task runs on it.
|
||||
managed_termination_protection = "ENABLED"
|
||||
|
||||
managed_scaling {
|
||||
status = "ENABLED"
|
||||
target_capacity = 100
|
||||
minimum_scaling_step_size = 1
|
||||
maximum_scaling_step_size = 1
|
||||
instance_warmup_period = 60
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_ecs_cluster_capacity_providers" "ec2" {
|
||||
count = local.ec2_count
|
||||
cluster_name = aws_ecs_cluster.ec2[0].name
|
||||
capacity_providers = [aws_ecs_capacity_provider.ec2[0].name]
|
||||
|
||||
default_capacity_provider_strategy {
|
||||
capacity_provider = aws_ecs_capacity_provider.ec2[0].name
|
||||
weight = 1
|
||||
}
|
||||
}
|
||||
|
||||
# The user's record (E11): what the app reads to run EC2 projects, since
|
||||
# workspace users can't read the state bucket. Outside
|
||||
# /triple-c/environments/, so environment detection never sees it. Written
|
||||
# last, removed first.
|
||||
resource "aws_ssm_parameter" "user" {
|
||||
count = local.ec2_count
|
||||
name = "/triple-c/users/${var.environment_name}/${var.owner_key}"
|
||||
description = "Triple-C ${var.environment_name}: EC2 capacity of ${var.owner_id}"
|
||||
type = "String"
|
||||
value = jsonencode({
|
||||
schema = 1
|
||||
instance_type = var.ec2_instance_type
|
||||
cluster_arn = aws_ecs_cluster.ec2[0].arn
|
||||
capacity_provider = aws_ecs_capacity_provider.ec2[0].name
|
||||
user_tag = var.ec2_user_tag
|
||||
})
|
||||
|
||||
depends_on = [aws_ecs_cluster_capacity_providers.ec2]
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
# One user's place in an environment: their task and execution roles and
|
||||
# their own EFS file system (D2, strict tenancy). The environment itself is
|
||||
# read only from its record, never from its state.
|
||||
# their own EFS file system (D2, strict tenancy), and optionally their own
|
||||
# EC2 capacity (ec2.tf, phase 7). The environment itself is read only from
|
||||
# its record, never from its state.
|
||||
|
||||
locals {
|
||||
# ONBOARD_MODULE_VERSION in app/src-tauri/src/aws/tofu_modules.rs.
|
||||
template_version = 1
|
||||
template_version = 2
|
||||
name = "triple-c-${var.environment_name}-${var.owner_key}"
|
||||
|
||||
partition = data.aws_partition.current.partition
|
||||
@@ -64,10 +65,11 @@ data "aws_ssm_parameter" "environment" {
|
||||
))
|
||||
&& jsondecode(self.insecure_value).log_group == "/triple-c/${var.environment_name}"
|
||||
&& can(regex("^sg-[0-9a-f]{8,17}$", jsondecode(self.insecure_value).security_group_id))
|
||||
&& alltrue([for s in jsondecode(self.insecure_value).subnet_ids : can(regex("^subnet-[0-9a-f]{8,17}$", s))]),
|
||||
&& alltrue([for s in jsondecode(self.insecure_value).subnet_ids : can(regex("^subnet-[0-9a-f]{8,17}$", s))])
|
||||
&& (jsondecode(self.insecure_value).assign_public_ip == true || jsondecode(self.insecure_value).assign_public_ip == false),
|
||||
false
|
||||
)
|
||||
error_message = "The environment record has a malformed kms_key_arn (not a key in this region and account), log_group (not /triple-c/<environment>), security_group_id or subnet id."
|
||||
error_message = "The environment record has a malformed kms_key_arn (not a key in this region and account), log_group (not /triple-c/<environment>), security_group_id, subnet id or assign_public_ip."
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,20 @@ output "efs_security_group_id" {
|
||||
value = aws_security_group.efs.id
|
||||
}
|
||||
|
||||
# The user's EC2 capacity, null without it. The app reads these from the
|
||||
# user record (/triple-c/users/<env>/<owner key>), not from the state.
|
||||
output "ec2_cluster_arn" {
|
||||
value = one(aws_ecs_cluster.ec2[*].arn)
|
||||
}
|
||||
|
||||
output "ec2_capacity_provider" {
|
||||
value = one(aws_ecs_capacity_provider.ec2[*].name)
|
||||
}
|
||||
|
||||
output "ec2_instance_type" {
|
||||
value = local.ec2 ? var.ec2_instance_type : null
|
||||
}
|
||||
|
||||
# Written only when every resource above was applied: a run that fails
|
||||
# part-way never evaluates it, so the app reads its absence from the state
|
||||
# as "the last onboarding didn't finish" (Incomplete, Join re-applies)
|
||||
@@ -34,5 +48,14 @@ output "onboarding_complete" {
|
||||
aws_iam_role_policy.task_bedrock,
|
||||
aws_iam_role.exec,
|
||||
aws_iam_role_policy.exec,
|
||||
aws_ecs_cluster.ec2,
|
||||
aws_iam_role.instance,
|
||||
aws_iam_role_policy.instance,
|
||||
aws_iam_instance_profile.instance,
|
||||
aws_launch_template.ec2,
|
||||
aws_autoscaling_group.ec2,
|
||||
aws_ecs_capacity_provider.ec2,
|
||||
aws_ecs_cluster_capacity_providers.ec2,
|
||||
aws_ssm_parameter.user,
|
||||
]
|
||||
}
|
||||
@@ -54,3 +54,58 @@ variable "bedrock_on_task_role" {
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
# Optional per-user EC2 capacity (phase 7). Empty = none: the module then
|
||||
# makes nothing for EC2 and the user's projects run on Fargate only.
|
||||
variable "ec2_instance_type" {
|
||||
description = "The instance type of the user's own EC2 capacity (e.g. m7i.large or m7g.large), or empty for none. Only the types the app can size are accepted."
|
||||
type = string
|
||||
default = ""
|
||||
|
||||
# The app's table (`ec2_instance_spec`) and the admin policy's lists.
|
||||
validation {
|
||||
condition = var.ec2_instance_type == "" || contains([
|
||||
"m7i.large", "m7i.xlarge", "m7i.2xlarge", "m7i.4xlarge",
|
||||
"m7g.large", "m7g.xlarge", "m7g.2xlarge", "m7g.4xlarge",
|
||||
"c7i.large", "c7i.xlarge", "c7i.2xlarge", "c7i.4xlarge",
|
||||
"c7g.large", "c7g.xlarge", "c7g.2xlarge", "c7g.4xlarge",
|
||||
"r7i.large", "r7i.xlarge", "r7i.2xlarge", "r7i.4xlarge",
|
||||
"r7g.large", "r7g.xlarge", "r7g.2xlarge", "r7g.4xlarge",
|
||||
"t3.large", "t3.xlarge", "t3.2xlarge",
|
||||
"t4g.large", "t4g.xlarge", "t4g.2xlarge",
|
||||
], var.ec2_instance_type)
|
||||
error_message = "ec2_instance_type must be empty or one of the m7i, m7g, c7i, c7g, r7i, r7g (large to 4xlarge), t3 or t4g (large to 2xlarge) types."
|
||||
}
|
||||
}
|
||||
|
||||
variable "ec2_max_instances" {
|
||||
description = "How many of the user's instances may run at once (1 to 4); each holds one workspace as expected use."
|
||||
type = number
|
||||
default = 1
|
||||
|
||||
validation {
|
||||
condition = var.ec2_max_instances >= 1 && var.ec2_max_instances <= 4 && floor(var.ec2_max_instances) == var.ec2_max_instances
|
||||
error_message = "ec2_max_instances must be a whole number from 1 to 4."
|
||||
}
|
||||
}
|
||||
|
||||
variable "ec2_user_tag" {
|
||||
description = "The value of the triple-c-user principal tag the user signs in with; names their EC2 cluster (triple-c-<environment>--u-<tag>). Required with ec2_instance_type."
|
||||
type = string
|
||||
default = ""
|
||||
|
||||
validation {
|
||||
condition = var.ec2_user_tag == "" || (
|
||||
can(regex("^[A-Za-z0-9-]{1,32}$", var.ec2_user_tag))
|
||||
&& !startswith(var.ec2_user_tag, "-")
|
||||
&& !endswith(var.ec2_user_tag, "-")
|
||||
&& !strcontains(var.ec2_user_tag, "--")
|
||||
)
|
||||
error_message = "ec2_user_tag must be 1 to 32 letters, digits and single hyphens, not starting or ending with a hyphen."
|
||||
}
|
||||
|
||||
validation {
|
||||
condition = var.ec2_instance_type == "" || var.ec2_user_tag != ""
|
||||
error_message = "ec2_user_tag (the user's triple-c-user principal tag) is required when ec2_instance_type is set."
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user