DaaS / Products / CI/CD Terraform Full-Stack with Security Hardening

CI/CD Terraform Full-Stack with Security Hardening

A DevOps engineer sets up a CI/CD pipeline (e.g., GitLab CI) that automates the entire Terraform lifecycle — from credential configuration through provisioning a full web application stack (VPC, ECS cluster, OSS storage) — and bakes security hardening into the pipeline stages by applying security group rules, ENI lockdown, and least-privilege access policies on every deployment, ensuring infrastructure is both automatically provisioned and hardened by default.

Products involved

Scenario

Use this workflow when you need to automate the end-to-end deployment of a hardened web stack via GitLab CI/CD. It chains secure credential injection, declarative Terraform provisioning, and automated security hardening (security groups, ENI lockdown, least-privilege IAM) into a single pipeline, ensuring every environment is provisioned securely by default.

Integration steps

  1. Inject STS Credentials: In GitLab CI/CD settings, set masked variables ALICLOUD_ACCESS_KEY_ID, ALICLOUD_ACCESS_KEY_SECRET, and ALICLOUD_SECURITY_TOKEN. Map them to a RAM Role with AliyunVPCFullAccess, AliyunECSFullAccess, and AliyunOSSFullAccess.
  2. Initialize Provider: In .gitlab-ci.yml, export credentials and run:
  3. ```yaml script:

  1. Define Core Resources: In main.tf, declare alicloud_vpc, alicloud_vswitch, alicloud_instance, and alicloud_oss_bucket. Bind ECS to the VPC via vswitch_id and security_group_id.
  2. Apply Security Group Rules: In security.tf, restrict ingress/egress:
  3. ``hcl resource "alicloud_security_group_rule" "allow_https" { type = "ingress" ip_protocol = "tcp" port_range = "443/443" security_group_id = alicloud_security_group.web_sg.id cidr_ip = "10.0.0.0/8" } ``

  4. Enforce ENI Lockdown & IAM: Attach a scoped RAM policy to the ECS instance role. Restrict outbound API calls using alicloud_ram_policy with actions limited to oss:GetObject and rds:DescribeDBInstances.
  5. Harden OSS Bucket: Configure oss_hardening.tf:
  6. ``hcl resource "alicloud_oss_bucket" "assets" { bucket = "prod-assets" acl = "private" server_side_encryption_rule { sse_algorithm = "AES256" } } ``

  7. Execute & Validate: Run terraform plan in CI, then terraform apply -auto-approve. Verify with aliyun ecs DescribeSecurityGroupAttribute --SecurityGroupId <sg-id>.

Architecture

The GitLab CI runner acts as the control plane, authenticating via STS tokens to invoke the Alibaba Cloud Terraform Provider. Terraform orchestrates the data plane: provisioning VPC networks, attaching ECS instances to isolated subnets, and creating private OSS buckets. Security configurations (SG rules, RAM policies, OSS encryption) are applied declaratively during the same apply phase. State is stored remotely in OSS, while runtime traffic flows through locked-down ENIs to the hardened compute layer.

Prerequisites

Common pitfalls

Typical questions

FAQ

Q: How does the CI/CD pipeline automate Terraform deployment and apply security hardening? A: It automates the entire Terraform lifecycle from credential configuration to provisioning a full web application stack while baking security hardening directly into the pipeline stages. This ensures infrastructure is automatically provisioned and hardened by default through applied security group rules, ENI lockdown, and least-privilege access policies on every deployment.