正在学习

Stage 2: runtime

Create a VPC

module "vpc" {

source = "terraform-aws-modules/vpc/aws"

name = "taskflow-vpc"

cidr = "10.0.0.0/16"

azs = ["{var.aws_region}b"]

public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]

enable_dns_hostnames = true

enable_dns_support = true

}

Create an ECS cluster

resource "aws_ecs_cluster" "taskflow_cluster" {

name = "taskflow-cluster"

}

Task definition

resource "aws_ecs_task_definition" "taskflow" {

family = "taskflow"

network_mode = "awsvpc"

requires_compatibilities = ["FARGATE"]

cpu = 256

memory = 512

execution_role_arn = var.ecs_execution_role

container_definitions = jsonencode([{

name = "taskflow"

image = var.docker_image

essential = true

portMappings = [{ containerPort = 8000 }]

logConfiguration = {

logDriver = "awslogs"

options = {

awslogs-group = "/ecs/taskflow"

awslogs-region = var.aws_region

awslogs-stream-prefix = "ecs"

}

}

}])

}

Service

resource "aws_ecs_service" "taskflow_service" {

name = "taskflow-service"

cluster = aws_ecs_cluster.taskflow_cluster.id

task_definition = aws_ecs_task_definition.taskflow.arn

desired_count = 2

launch_type = "FARGATE"

network_configuration {

subnets = module.vpc.public_subnets

assign_public_ip = true

}

load_balancer {

target_group_arn = aws_lb_target_group.taskflow_tg.arn

container_name = "taskflow"

container_port = 8000

}

depends_on = [aws_lb_listener.http]

}

Application Load Balancer

resource "aws_lb" "taskflow_alb" {

name = "taskflow-alb"

internal = false

load_balancer_type = "application"

subnets = module.vpc.public_subnets

}

resource "aws_lb_target_group" "taskflow_tg" {

name = "taskflow-tg"

port = 8000

protocol = "HTTP"

vpc_id = module.vpc.vpc_id

}

resource "aws_lb_listener" "http" {

load_balancer_arn = aws_lb.taskflow_alb.arn

port = 80

protocol = "HTTP"

default_action {

type = "forward"

target_group_arn = aws_lb_target_group.taskflow_tg.arn

}

}

variables.tf

variable "aws_region" {

description = "AWS region"

default = "us-east-1"

}

variable "docker_image" {

description = "TaskFlow container image"

type = string

}

variable "ecs_execution_role" {

description = "IAM role for ECS task execution"

type = string

}

outputs.tf

output "load_balancer_dns" {

description = "Public URL of TaskFlow service"

value = aws_lb.taskflow_alb.dns_name

}

Run these commands:

terraform init

terraform plan -var="docker_image=ghcr.io/your-org/taskflow:latest" -var="ecs_execution_role=arn:aws:iam::123456789012:role/ECSExecutionRole"

terraform apply -auto-approve


Once deployed, Terraform outputs your load balancer DNS, and your API becomes accessible via a public endpoint. Claude can now help you test the deployment using simple prompts like:

“Claude, write a curl test to verify that the TaskFlow/health endpoint returns a 200 response through the new load balancer.”

Claude would respond with a one-line verification command:

curl -i http:///health


Clarification Table: Claude’s Role in Infrastructure Automation

| Stage | Traditional Task | Claude’s Assistance | Outcome |
| --- | --- | --- | --- |
| Definition | Writing Terraform/Ansible templates | Generates validated IaC with comments | Saves setup time |
| Validation | Checking dependencies, syntax | Detects cyclic references, missing vars | Prevents runtime errors |
| Optimization | Improving costs and resource usage | Recommends instance types and scaling policies | Reduces cloud spend |
| Compliance | Reviewing IAM and security groups | Suggests least-privilege roles | Increases security |
| Maintenance | Updating and documenting infra | Summarizes diffs and creates changelogs | Keeps infrastructure auditable |

With Claude as your DevOps partner, infrastructure automation becomes both faster and safer. Instead of memorizing every Terraform resource or YAML syntax rule, you describe your intent and let Claude translate it into structured, validated code. Beyond generation, Claude explains the reasoning behind each resource, helping you learn IaC principles organically.

In the next section, you’ll go a step further—automating cloud environments end-to-end, including variable management, secret injection, and cross-environment synchronization—all with Claude Code acting as a real-time assistant that understands your stack and anticipates your operational needs.

练习题

In the VPC module configuration, what is the CIDR block specified for the VPC?

A. 10.0.1.0/24
B. 10.0.0.0/16
C. 192.168.1.0/24
D. 172.16.0.0/12

Which Terraform resource is used to create an ECS cluster?

A. aws_vpc
B. aws_ecs_task_definition
C. aws_ecs_cluster
D. aws_lb

Which of the following are required parameters in the ECS task definition?

A. family
B. network_mode
C. requires_compatibilities
D. execution_role_arn
E. container_definitions

The ECS service configuration assigns a public IP to the tasks.

The Application Load Balancer listens on port ___ for HTTP traffic.

What is the purpose of the 'execution_role_arn' parameter in the ECS task definition?

Which command is used to initialize a Terraform configuration?

A. terraform apply
B. terraform init
C. terraform plan
D. terraform validate

Which Terraform commands are used to plan and apply changes to the infrastructure?

A. terraform init
B. terraform plan
C. terraform apply
D. terraform destroy
E. terraform validate

The 'terraform apply' command requires manual confirmation unless the '-auto-approve' flag is used.

The output variable '___' provides the public URL of the TaskFlow service.

Explain the purpose of the 'container_definitions' parameter in the ECS task definition.

Which knowledge points are involved in creating and testing an ECS service with a load balancer?

A. VPC Creation with Terraform
B. ECS Cluster Creation with Terraform
C. Task Definition with Terraform
D. ECS Service Creation with Terraform
E. Application Load Balancer with Terraform
F. Curl Test Command for Load Balancer

How does the 'depends_on' parameter in the ECS service configuration ensure proper deployment order?

When creating an ECS task definition with Terraform, which parameter specifies the container's port mapping for HTTP traffic?

A. network_mode
B. requires_compatibilities
C. portMappings in container_definitions
D. assign_public_ip in network_configuration

Which Terraform resources are required to create a load-balanced ECS service? (Select all that apply)

A. aws_ecs_cluster
B. aws_ecs_task_definition
C. aws_lb
D. aws_lb_target_group
E. aws_lb_listener
F. aws_vpc

The depends_on argument in the aws_ecs_service resource ensures the load balancer listener is created before the service starts.

To test the deployed service, you would use the command: curl -i http://___/health

Explain why both public_subnets and assign_public_ip = true are needed in the ECS service's network configuration.

登录后解锁笔记、知识点解析、AI 问答

立即登录