references/examples.md · 24000
} }
component "ecs_cluster" { source = "./modules/ecs-cluster"
inputs = { name_prefix = local.name_prefix }
providers = { aws = provider.aws.this } }
component "ecs_service" { source = "./modules/ecs-service"
inputs = { name_prefix = local.name_prefix cluster_id = component.ecs_cluster.cluster_id desired_count = var.ecs_desired_count subnet_ids = component.vpc.private_subnet_ids security_group_id = component.security_groups.app_sg_id database_endpoint = component.rds.endpoint }
providers = { aws = provider.aws.this } }
component "alb" { source = "./modules/alb"
inputs = { name_prefix = local.name_prefix vpc_id = component.vpc.vpc_id subnet_ids = component.vpc.public_subnet_ids security_group_id = component.security_groups.alb_sg_id target_group_arn = component.ecs_service.target_group_arn }
providers = { aws = provider.aws.this } }
component "cloudwatch" { source = "./modules/cloudwatch"
inputs = { name_prefix = local.name_prefix cluster_name = component.ecs_cluster.cluster_name service_name = component.ecs_service.service_name }
providers = { aws = provider.aws.this } }
### outputs.tfcomponent.hcl
```hcl
output "load_balancer_url" {
type = string
description = "Application load balancer URL"
value = component.alb.dns_name
}
output "database_endpoint" {
type = string
description = "RDS endpoint"
value = component.rds.endpoint
sensitive = true
}
output "vpc_id" {
type = string
value = component.vpc.vpc_id
}
output "ecs_cluster_name" {
type = string
value = component.ecs_cluster.cluster_name
}
deployments.tfdeploy.hcl
identity_token "aws" {
audience = ["aws.workload.identity"]
}
locals {
role_arn = "arn:aws:iam::123456789012:role/terraform-stacks"
}
deployment "staging" {
inputs = {
aws_region = "us-west-1"
environment = "staging"
app_name = "myapp"
vpc_cidr = "10.1.0.0/16"
db_instance_class = "db.t3.small"
ecs_desired_count = 2
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
deployment "production" {
inputs = {
aws_region = "us-west-1"
environment = "production"
app_name = "myapp"
vpc_cidr = "10.0.0.0/16"
db_instance_class = "db.r5.large"
ecs_desired_count = 5
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
# Deployment groups
deployment_group "staging" {
deployments = [deployment.staging]
}
deployment_group "production" {
deployments = [deployment.production]
}
# Auto-approve staging with safety checks
deployment_auto_approve "staging_safe" {
deployment_group = deployment_group.staging
check {
condition = context.plan.changes.remove == 0
reason = "Cannot auto-approve deletions in staging"
}
check {
condition = context.plan.applyable
reason = "Plan must be applyable"
}
}
Testing Configurations
Validate Stack Configuration
terraform stacks providers lock
terraform stacks validate
Plan Specific Deployment
terraform stacks plan --deployment=development
terraform stacks plan --deployment=production
Apply Deployment
terraform stacks apply --deployment=staging
Destroying Deployments
Example of safely removing a deployment from your Stack.
Scenario
You want to decommission the "development" deployment while keeping staging and production active.
Step 1: Mark Deployment for Destruction
Update your deployments.tfdeploy.hcl file to set destroy = true:
identity_token "aws" {
audience = ["aws.workload.identity"]
}
locals {
role_arn = "arn:aws:iam::123456789012:role/terraform-stacks"
}
# Mark this deployment for destruction
deployment "development" {
inputs = {
aws_region = "us-east-1"
environment = "dev"
instance_count = 1
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
destroy = true # This tells HCP Terraform to destroy all resources
}
# Keep these deployments active
deployment "staging" {
inputs = {
aws_region = "us-west-1"
environment = "staging"
instance_count = 2
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
deployment "production" {
inputs = {
aws_region = "us-west-1"
environment = "prod"
instance_count = 5
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
# Deployment groups
deployment_group "staging" {
deployments = [deployment.staging]
}
deployment_group "production" {
deployments = [deployment.production]
}
Step 2: Plan and Apply
# Review the destruction plan
terraform stacks plan --deployment=development
# Apply the destruction
terraform stacks apply --deployment=development
HCP Terraform will destroy all resources in the development deployment.
Step 3: Remove the Deployment Block
After the deployment is successfully destroyed, remove the entire deployment block from your configuration:
identity_token "aws" {
audience = ["aws.workload.identity"]
}
locals {
role_arn = "arn:aws:iam::123456789012:role/terraform-stacks"
}
# deployment "development" block has been removed
deployment "staging" {
inputs = {
aws_region = "us-west-1"
environment = "staging"
instance_count = 2
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
deployment "production" {
inputs = {
aws_region = "us-west-1"
environment = "prod"
instance_count = 5
role_arn = local.role_arn
identity_token = identity_token.aws.jwt
}
}
# Deployment groups
deployment_group "staging" {
deployments = [deployment.staging]
}
deployment_group "production" {
deployments = [deployment.production]
}
Important Notes
- Provider Authentication: The
destroyargument ensures your configuration retains the provider authentication needed to destroy resources - Do Not Remove Immediately: Don't remove the deployment block until after the destruction is complete
- Verify Before Removing: Check HCP Terraform UI to confirm all resources are destroyed before removing the block
- Alternative: You could manually destroy resources through HCP Terraform UI, but using
destroy = trueis the recommended approach for maintaining infrastructure-as-code practices