IaC基础设施即代码:Terraform 连接 aws S3 实现多资源管理

发布时间:2024年01月18日

目录

?一、实验

1.环境

2.aws 亚马逊云创建用户

2.Windows使用Terraform 初始化?aws provider

3.Windows使用Terraform 创建S3存储资源?(对象存储)

4.Windows使用Terraform 创建Dynamo DB资源 (表格存储)

5.Windows给Terraform项目添加Backend配置

6.Windows使用Terraform?创建VPC和Subnet资源

7.Windows使用Terraform?创建SecurityGroup资源

8.Windows使用Terraform?创建Route和Gateway资源

9.Windows使用Terraform?创建EC2资源

10.Windows使用Terraform?创建ELB资源

11.销毁资源

二、问题

1.Terraform初始化报错

2.Terram验证资源报错

3.Terraform初始化失败

4.Terraform删除资源失败 (负载均衡器)

5.Terraform删除资源失败 (S3)

6.AWS 有哪些区域和可用区


?

?

?

?

?一、实验

1.环境

(1)主机

表1-1 主机

主机系统软件工具备注
jia

Windows?

Terraform 1.6.6VS Code、?PowerShell、?Chocolatey?
pipepointLinuxTerraform 1.6.6??

?

2.aws 亚马逊云创建用户

(1)登录

用户 | IAM | Global (amazon.com)

?

(2)查看

IAM?

ebdd6d4f1efc49028f13a7a462cfdd0d.png

(3)创建用户?

1c76662a3bb340ebaf721a32cd7b4c93.png

?

2bf971a2ac0c43489836dd8253a2d975.png

74fc169490554282877d74af6dd0b4b8.png

(4)创建完成

073e124ab0cb4d85af816c91b18a8f66.png

(5)访问密钥

e777dd238c624aa18618d89945505130.png

7fb418b6196a4d1b871075f22ef695c1.png

(6)完成

e2d6583bbcdb4f4493b6d06a512d8e70.png

(7)创建组和权限

adcd6bd97140489490f06f02ddeacbef.png

(8)关联用户

928824839c3f499c84b204e2835417d4.png

(9)查看用户权限

05f8d1e976a2430788edc8ac37ffaade.png

?

(10)查看aws?provider 示例

Terraform Registry

?

?

?

cb4af04b6c9947ec9cdeafcc96c4f38f.png

?

USE PROVIDER? 示例

3a7eb0cd552e47bfb2c821c563c88807.png

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.32.1"
    }
  }
}

provider "aws" {
  # Configuration options
}

Example Usage? 示例

2958779de7454c67b3f85598d3f78fca.png

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# Create a VPC
resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

3.Windows使用Terraform 初始化?aws provider

(1)创建项目terraform-aws

查看目录

177404709b2e454a956f5efe8a99c00b.png

(2)格式化代码

terraform fmt

359130813ef6467493cba2bae27d0aea.png

(3) 初始化

terraform init

4694b46d38d94656a697b079b3af3dcd.png

?

(4) 验证代码

terraform validate

867850ce26c4476197ad328bc929c9c2.png

(5)查看版本及provider

terraform -v 或 terraform --version

5bdc1aef4c114106b6b8509bb43b1047.png

?

4.Windows使用Terraform 创建S3存储资源?(对象存储)

(1)查看目录

1a48f496f6764ec39adb44b0e8d9f7e0.png

(2)创建主配置文件

main.tf

# Configuration options
provider "aws" {
  region     = var.region
  access_key = var.access_key
  secret_key = var.secret_key
}

resource "aws_s3_bucket" "terraform-bucket" {
  bucket = "terraform-state-devmaojing"
  acl    = "private"
  versioning {
    enabled = true
  }
}

84650da271c1435fa25158fb9fdda95f.png

(3)创建默认变量配置文件(存储IAM密钥)

terraform.tfvars

a199b28b85e54dce83dfb15255686269.png

(4)创建版本配置文件

variables.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "5.32.1"
    }
  }
}

c28b26e7dda345ae9429250a9a54e2aa.png

(5)?创建变量配置文件

variables.tf

variable "access_key" {
   type        = string

}

variable "secret_key" {
   type        = string
}

variable "region" {
  type        = string
  default = "us-east-1"
}

b239161339b741849f4862f83d1b60ef.png

(5)?格式化代码

terraform fmt

b8ef53749f5247ba96c26b56426b2839.png

(6)验证代码

??????????????terraform validate

4e67ae28c1644651b85149e9c1e5145a.png

(7)计划与预览

 terraform plan

dc6adb15cc7f474886cd41db66061685.png

(8)申请资源

terraform apply

b4d1d84b398442f0b2dd8c6749206432.pngb4041a4515204ac6bb08ca697f171b2d.png

?

(9)登录AWS查看

查看S3存储桶

14be4533917d4cb7b8910102695a9233.png

?

5.Windows使用Terraform 创建Dynamo DB资源 (表格存储)

(1)修改主配置文件

main.tf 添加如下代码

resource "aws_dynamodb_table" "tf-state-table" {
  name           = "tf-state-tablemaojing"
  billing_mode   = "PROVISIONED"
  read_capacity  = 20
  write_capacity = 20
  hash_key       = "LockID"

  attribute {
    name = "LockID"
    type = "S"
  }
}

fbe1b78985514adeb7d87c8b6e91a7df.png

?(2)?格式化代码

terraform fmt

?

e085505396ea42599dcd02e8ae8254c0.png

(3)验证代码

??????????????terraform validate

?

ae23bc1a77c041698e2d0591f014d446.png

(4)计划与预览

 terraform plan

0d3384fa5e1a4763b0a0f456088c7afc.png

(5)申请资源

terraform apply

02af9f43087a446f85a3487e64fbb99a.png

(6)登录AWS查看

查看Dynamo DB

d24b3fa236034934b4a256e78c57e038.png

?

?

6.Windows给Terraform项目添加Backend配置

(1)添加输出配置文件

outputs.tf

output "bucket_name" {
  value = aws_s3_bucket.terraform-bucket.bucket
}

4409febbd8994a73afa8aedc084f3230.png

(2)??申请资源

terraform apply

成功输出bucket名称

9d35645a26a7463197482a2b7e1b345f.png

(3)添加后端存储配置文件

backend.tf

e19006b2a5ef4dcc97afa4a031b53d97.png

(4)格式化代码

terraform fmt

8cf77d6b0da44407856275eba3179ba4.png

?

(3)验证代码

??????????????terraform validate

45e1e74658b14509bfe857b967859355.png

(4)?初始化

terraform init

yes,系统上传配置文件到AWS 的S3

62ce6861311a4d6785e80c49c24a4ed0.png

(6)登录AWS查看

查看配置文件已上传S3

7d406b63952b4ac7810349ad02238cab.png

(7)删除配置文件

e164cfdb27634e70877d373d217d3334.png

5586e56f8c6a47daa9bd438379e5b5c4.png

(8)IAM添加VPC权限

e48a59c27fcd4c6fb7b1e24b8c8a74a6.png

?

7.Windows使用Terraform?创建VPC和Subnet资源

(1)查看目录

5e16a3170cba40dcb5762909071d74b8.png

(2)配置network网络实例资源

main.tf

# Configuration options
provider "aws" {
  region     = var.region
  access_key = var.access_key
  secret_key = var.secret_key
}

cb27e5f009894d028475cc61d9a2efe5.png

backend.tf

a832db13fe304fb8907f0ea4fd298dcc.png

terraform.tfvars、variables.tf 、versions.tf 配置文件与 global/backend下的配置文件相同。

94107b616886429db359637f04232950.png

(3)?格式化代码

terraform fmt

805298df8e3144fd8429d750ffb13fc6.png

?

(4)?初始化

terraform init

578a0c53c67646a28df2f8d1ff2071e6.png

(5)AWS查看S3已更新配置文件

f7199ee05ad844bcbd9cd144c6073a33.png

(6)添加VPC配置文件

vpc.tf

resource "aws_vpc" "main" {
  cidr_block           = "172.16.0.0/16"
  enable_dns_hostnames = true
}

1848967a30094d558b57405055bcf14e.png

?(7)申请资源

terraform apply

3c2ec1c0cada4c7d8200195a492e63b7.png

(8)AWS查看VPC

无Name

7284dd93aabb4e00bd14d4fa5c6e1e03.png

(9)修改VPC配置文件

vpc.tf ,添加标签

  tags = {
    "Name" = "tf-demo-vpc"
  }

919361eded0d49fb9099ea67aabd2a87.png

(10)申请资源

terraform apply

044f79ace0964b4e9156d96950dca80d.png

(11)AWS 再次查看VPC

出现了Name

1957e9614f1f4c52a1ba1c7d235fd5e4.png

详细信息

2f9489664fd14653b27c9d5cf69a1706.png

?

(12)修改VPC配置文件

vpc.tf,添加子网subnet信息


locals {
  azs         = ["us-east-1a", "us-east-1b"]
  cidr_blocks = ["172.16.10.0/24", "172.16.50.0/24"]
}

resource "aws_subnet" "subnet" {
  count                   = length(local.azs)
  vpc_id                  = aws_vpc.main.id
  cidr_block              = local.cidr_blocks[count.index]
  availability_zone       = local.azs[count.index]
  map_public_ip_on_launch = true

  tags = {
    Name = "subnet-${count.index}"
  }
}

map_public_ip_on_launch = true 功能为开启自动分配公有 IPv4 地址

17e73ba496244768b45493f290131ecf.png

? (13)格式化代码

terraform fmt

644fc2c5c5904b7299a915762d42b36c.png

(14)申请资源

terraform apply

5f65f734cc374d15b1eab5215d729a15.png

(14)AWS查看子网

a7cfb3035a874097aa0a7500c5f55745.png

17c6141ef3944058b072552c29d2c616.png

?

8.Windows使用Terraform?创建SecurityGroup资源

(1)修改主配置文件

将vpc,tf 的locals 字段 移动到main.tf,并添加端口信息

# Configuration options
provider "aws" {
  region     = var.region
  access_key = var.access_key
  secret_key = var.secret_key
}

locals {
  azs         = ["us-east-1a", "us-east-1b"]
  cidr_blocks = ["172.16.10.0/24", "172.16.50.0/24"]
  ports       = ["80", "443", "22"]
}

342ec1eab9764cf79794fe8a4abae2cb.png

(2)创建安全组资源配置文件

security_group.tf

resource "aws_security_group" "allow" {
  name        = "allow"
  description = "Allow  inbound traffic"
  vpc_id      = aws_vpc.main.id

  dynamic "ingress" {
    for_each = local.ports
    content {
      description      = "${ingress.value} from VPC"
      from_port        = ingress.value
      to_port          = ingress.value
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = ["::/0"]
    }
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "allow"
  }
}

5da10077e2b147c0ae21d635f3bb0b8c.png

?(3)格式化代码

terraform fmt

?

0965e7c809dd4fd4bbd5c1544483d5eb.png

(4)申请资源

terraform apply

93d4b64edcb44feab1d89ed2a37c77cc.png

1f1c9163f3c54fa6ad2adbddfd45e051.png

yes

e648ab62ab4246ca807b6d17d045db78.png

(5)AWS 查看安全组

4744d0617f8d431ba915dd4325c41f24.png

入站规则

1ec97affdb4543f98ff27074e988a24c.png

出站规则

515021419eaa4a388b5324b9b4356ddd.png

(6)添加输出配置文件

outputs.tf

output "vpc_id" {
  value = aws_vpc.main.id

}

output "subnet_id" {
  value = aws_subnet.subnet.*.id
}

output "security_group_id" {
  value = aws_security_group.allow.id

}

78b4e678a9a5425fa5af68803d097915.png

(7)?申请资源

terraform apply

输出3个网络信息

14e1d58ab5bf4a5da318da2fae5ce381.png

?

9.Windows使用Terraform?创建Route和Gateway资源

(1)修改VPC配置文件

vpc.tf ,新增获取路由表信息

data "aws_route_table" "table" {
  vpc_id = aws_vpc.main.id
}

3d84ed17712c4489859ca32c9527498b.png

?

(2)修改输出配置文件

outputs.tf ,新增输出路由表id

output "route_table_id" {
  value = data.aws_route_table.table.id
}

b395bfb7cafb4e5cbf7955b342f14e95.png

?

(3) 申请资源

terraform apply

新增输出路由表信息

9a49634719364043ac1790590a994ff2.png

(4) 创建Gateway资源

修改vpc.tf ,新增网关代码

resource "aws_internet_gateway" "gw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "tf-demo-ec2-gw"
  }
}

c804eeeecb2a41bd9335f0add142c9e7.png

(5)申请资源

terraform apply

c41fbd207ffb4fab8b88698a82e303bc.png

(6) AWS 查看网关

0931648ea14e4895b9469fdd08a51e3d.png

?

(7)创建路由表资源

修改vpc.tf ,添加如下代码:

resource "aws_route" "r" {
  route_table_id         = data.aws_route_table.table.id
  destination_cidr_block = "0.0.0.0/0"
  gateway_id             = aws_internet_gateway.gw.id
}

d9921440f2884c02978588cf230caf17.png

(8)格式化代码

terraform fmt

4012e847298a4439bab1b0ea3f724712.png

(9) 申请资源

terraform apply

b30f75db51ad457ebd12c0484722387f.png

(10)AWS查看路由表

c4eabcdf460142a7a062efaa7b21b87a.png

10.Windows使用Terraform?创建EC2资源

(1)查看目录

98e18b48c2714594b9d6807f9a0cf3c8.png

(2)修改主配置文件

main.tf

# Configuration options
provider "aws" {
  region     = var.region
  access_key = var.access_key
  secret_key = var.secret_key
}

86086c94d7404371b8869c3b151b8945.png

(3) 修改后端存储配置文件

7f446295cbfa497bbb3e4ea1b970c084.png

(4)初始化

terraform init

a5513506d88d4d6b8695137fcdb5ef3b.png

(5)? 申请资源

terraform apply

f4113048b4144e09a6bc9ed3243c2414.png

(6)AWS查看S3

已新增service 配置文件

ef192ddfee454e159c89d305fbd8f6dd.png

?

(7)AWS查询镜像ID

AMI ID
ami-023c11a32b0207432

51c98288e8714cfda2e9c731a602505c.png

(8)添加SSH密钥对

ssh-keygen -t rsa -m PEM 

私钥名称改为myecs_private_key.pem,公钥名称改为myecs_public_key.pub

140a799b65e94c8ba79f8fc20a8b6f14.png

(9)添加ECS配置文件

ec2.tf

resource "aws_key_pair" "deployer" {
  key_name   = "myecs_private_key"
  public_key = file("${path.module}/config/myecs_public_key.pub")
}

resource "aws_instance" "web" {
  for_each                    = toset(local.instance)
  ami                         = local.ami_id
  key_name                    = aws_key_pair.deployer.key_name
  instance_type               = local.instance_type
  associate_public_ip_address = true
  subnet_id                   = local.instance_config[each.value].subnet_id
  availability_zone           = local.instance_config[each.value].availability_zone
  private_ip                  = local.instance_config[each.value].ipv4_address
  vpc_security_group_ids      = [local.security_group_id]

  user_data = file("${path.module}/config/install-nginx.sh")

  tags = {
    Name = each.value
  }
}

763aab2e10f1450cbb721c6e04931b56.png

(10) 修改主配置文件

main.tf

2c8a90b096de40c5ac63341a47b00b74.png

(11)添加输出配置文件

terraform validate 

outputs.tf

output "ecs_ids" {
  value = aws_instance.web

}

eac69d0e1c9e4c28b198095496b247b1.png

(12)?格式化代码

terraform fmt

fe59dd0d27bb49adb1277b11bde0f49e.png

(13)验证代码???????

terraform validate 

b09adb952868465db87fdc6ecc2fef31.png

(14)计划与预览

 terraform plan

4ee204914be84ba29caab821a7bab686.png

513b209e3b6b4166aa34bac9e9e648c1.png

0f53da134bb849b0aacd6cd32b5257e4.png

f5b2a887dc0a430a80e211164bb9c67d.png21d4c8bb9c9542fe886af60d2377168d.png

(15)申请资源

terraform apply

a23baf0c511c4e14996ff3161621ad05.png

c5d72cb94ded4acb893e548c1d8fa8b6.png

aa94f7934a9f46fca203983f5b465244.png3661ef4e592349a89b151fd4f991f840.pngab7bde570a3e4ca1bd40b523434fa759.png

yes

76b33a002ad04764aea676e512d04f34.png

输出

d29cf1e2c0f54091a527f43807a5c2a2.png

6f9bb0386f5f4763a3a2b5c993b54de5.png88c02fa9000a457186248ea6dd32b884.png6534dfffd31744d1a0aa3c96a5227367.png

(16) AWS查看EC2

2a3686b4213040e5a2ebc7e309e6089a.png

346379e41cb24a0d8af38981ebfd2076.png

?

(17)点击连接server1

ff23857fb1624e84b6a2ee9b7ac5b09f.png

(18)SSH命令

22b4cebe27b0483abba85969ce7b6557.png

(19)点击连接server1

ff23857fb1624e84b6a2ee9b7ac5b09f.png

(19)SSH命令

5ed1249a3f924cb7a114b2a871f4445d.png

?

?

(20)远程操作server1

远程连接

ssh -i "myecs_private_key.pem" ec2-user@ec2-18-232-100-200.compute-1.amazonaws.com

sudo -s

注意切换目录config

986fd9aed7f440f0bff521c854c87edc.png

查看nginx进程

ps aux | grep nginx

f3dbfa8ff2b74680a7bf654191e1122f.png

测试

 curl localhost

dca008ea000141c19ceb6c1e744eb17c.png

退出

exit

734defd94d724b778ec79510b8801fa7.png

?

(20)远程操作server2

远程连接

ssh -i "myecs_private_key.pem" ec2-user@ec2-3-215-176-187.compute-1.amazonaws.com

sudo -s

?

496ede22f96f4f02adb0abd786e2d1ec.png

查看nginx进程

ps aux | grep nginx

?

2167e23c8bfd4ac18b5b02dffa637042.png

测试

 curl localhost

?

087ed88a00dc4f5f8f4babf9343688a6.png

?退出

exit

cd469253b88644d2a42593bbf1c3831a.png

(21)公网访问

http://ec2-18-232-100-200.compute-1.amazonaws.com/

9035b7f68aef432fa366ad208f984d5d.png
?

http://ec2-3-215-176-187.compute-1.amazonaws.com/

afcca061caa947deab5adcd7cec231a0.png

(22)修改输出配置文件

outputs.tf

output "ecs_ids" {
  value = [for k, v in aws_instance.web : v.id]
}

45c6dbab355845778c4fff02e956a30b.png

?

(23)?申请资源

terraform apply

62eeecec24ee4bdaa350a8aecfc9583d.png

09287b6bb2a04c3fb86e3a15c9c43b72.png5e32d4cc6eb745f78bc1afbf86628c1b.png4881d3f5d4df4308ba599fd0d0a8815c.png

成功拿到2个ECS实例的id值

a67550d8815b43e7a1497c74b7753127.png

?

11.Windows使用Terraform?创建ELB资源

(1)添加elb配置文件

elb.tf

resource "aws_lb" "test_lb" {
  name               = "test-lb-tf"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [local.security_group_id]
  subnets            = local.subnet_id

  enable_deletion_protection = true

  tags = {
    Environment = "production"
  }
}

resource "aws_lb_target_group" "test_group" {
  name     = "tf-example-lb-tg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = local.vpc_id
}

resource "aws_lb_target_group_attachment" "test" {
  count            = length(local.ecs_ids)
  target_group_arn = aws_lb_target_group.test_group.arn
  target_id        = local.ecs_ids[count.index]
  port             = 80
}


resource "aws_lb_listener" "front_end" {
  load_balancer_arn = aws_lb.test_lb.arn
  port              = "80"
  protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.test_group.arn
  }
}

142cf9b5db954b14b687a1e3f0f2975b.png

(2) 修改主配置文件

main.tf? ?,添加如下代码

 ecs_ids = [for k, v in aws_instance.web : v.id]

4ac895c1dfcb4547b56ab427bd401c52.png

(3)格式化代码

terraform fmt

958d4791385f491bb70b209daa468e95.png

(4)验证代码???????

terraform validate

(5)计划与预览

 terraform plan

720ed7f413904894b7367bacf1fdc504.png

8d8ce3adfbf44fffa7c104afeb330c82.png

(6)申请资源

terraform apply

197037b59271486c886f3d51a5276979.png

73902b1af33d424395ebb6917953a36f.png

yes, 大概需要3分钟

6cf5c259ba694fca9cdf184b402194dc.png

(7)AWS查看

负载均衡器

e3faa816ad454fb0ba4157686a7ddb66.png

侦听器

b0eebeeffc584415bb0de6cd8a9ea2c2.png

目标组(状态均为健康)

c17d5eaba11641f3aaa752c3a5884cd6.png

?

(8)访问DNS

test-lb-tf-978979139.us-east-1.elb.amazonaws.com

d875432b04b846d2b40eb9272b4f190d.png

访问成功

a076cdb6f7a3408d84cd99a84557cf54.png

刷新会变化

f57a611dc83a419fa5a47e9ba6ae28c2.png

?

12.销毁资源

(1)查看当前目录

8f0d68bbecd8419aa9f67faa6eb7315e.png

?

(2)销毁服务资源

terraform destroy

03054374eb674f2f94b512a263d78d24.png08f769b2d4e54bb492ecfcda66ab63a0.png0ebf343a830d4a5bb5debd6306616520.pngb57543cdb67f4543961ccd168bcd70f9.png0aafb73d1d5644e7b735e2b9c68a252c.png842e4696f767430e8cc6709ab70f0821.png

关闭负载均衡器的删除保护,然后删除成功

42edf2eb432f42fc8cee12460f924440.png

(3)销毁网络资源

terraform destroy

d5e9af78f372400e8d2502ce1f06d1a0.pnga722b92f0d8841e99cfa298e1101ee6f.png51d78b0ca65240c088d0c81ef50560ee.png

yes

3a6768d27e5d4320a9c3b1d316dad299.png

(4)销毁后端存储资源

terraform destroy

?

清空存储桶内容,再销毁资源

???????

(5)登录AWS查看

ECS已终止

394216567d8449f3853d5deead43e9d4.png

负载均衡器已删除

2f4ebf842b0e41219d13b6b4b21acf64.png

DynamoDB已删除

6783708215a24125b8c3e59004f5e8c4.png

?

二、问题

1.Terraform初始化报错

(1)报错

?
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/aws: could not connect to registry.terraform.io: failed to request 
│ discovery document: Get "https://registry.terraform.io/.well-known/terraform.json": read tcp
│ [240e:3ae:b80:8240:8013:f872:f878:2e7b]:61445->[2600:9000:2646:6e00:16:1aa3:1440:93a1]:443: wsarecv: An existing connection was forcibly    
│ closed by the remote host.
?

03af66caee3749e190f108fc4587f87a.png

(2)原因分析

国内没有terraform provider源,因此可能会下载超时。

(3)解决方法

查询官网

Terraform-Provider-Aws Versions | HashiCorp Releases

找到指定版本

1e3636b47d2246d792f11aef31eca759.png

下载对应系统支持的版本

03f4e726fef0418a8b04c1d7e2a651cf.png

成功:

d039af978d544d9cb0a5ef189c1a4493.png

2.Terram验证资源报错

(1)报错

?
│ Error: Reference to undeclared input variable
│
│   on main.tf line 4, in provider "aws":
│    4:   access_key = var.access_key
│
│ An input variable with the name "access_key" has not been declared. This variable can be declared with a variable "access_key" {} block.    
?
?
│ Error: Reference to undeclared input variable
│
│   on main.tf line 5, in provider "aws":
│    5:   secret_key = var.secret_key
│
│ An input variable with the name "secret_key" has not been declared. This variable can be declared with a variable "secret_key" {} block.  

de8b54887bd1439f8f3f16b84593913e.png

(2)原因分析

变量未申明

(3)解决方法

申明变量。

b239161339b741849f4862f83d1b60ef.png

成功:

235f2aced9ff4a63992f13f019053bee.png

?

3.Terraform初始化失败

(1)报错

?
│ Error: Variables not allowed
│
│   on backend.tf line 5, in terraform:
│    5:     region = var.region
│
│ Variables may not be used here.
?

PS C:\Gocode\src\terraform-aws\global\backend> terraform init

Initializing the backend...
?
│ Error: No valid credential sources found
│
│ Please see https://www.terraform.io/docs/language/settings/backends/s3.html
│ for more information about providing credentials.
│
│ Error: failed to refresh cached credentials, no EC2 IMDS role found, operation error ec2imds: GetMetadata, request canceled, context deadline exceeded

dc54da8361a34db7a7ea462338a9c038.png

(2)原因分析

backend 模块里 不允许出现变量,未添加IAM密钥验证信息。

(3)解决方法

修改配置文件。

e19006b2a5ef4dcc97afa4a031b53d97.png

成功:

35114a7cdfa34c8982715cf9bb599cf0.png

4.Terraform删除资源失败 (负载均衡器)

(1)报错

?
│ Error: deleting ELBv2 Load Balancer (arn:aws:elasticloadbalancing:us-east-1:754381516519:loadbalancer/app/test-lb-tf/acc75028b14a17a1): OperationNotPermitted: Load balancer 'arn:aws:elasticloadbalancing:us-east-1:754381516519:loadbalancer/app/test-lb-tf/acc75028b14a17a1' cannot be deleted because deletion protection is enabled
│       status code: 400, request id: 46687c4f-1016-496c-af63-f2b5840796c2

ef8767738e2a4106897ca52e2b74151e.png

(2)原因分析

负载均衡器开启了删除保护。

4f008b18d2c8469298aad730ab741ef8.png

(3)解决方法

查看属性

f8e1e1b76f54463fb54fa81237f4b669.png

关闭保护

a1a0902782d64c79a510cc0945b18472.png

成功

42edf2eb432f42fc8cee12460f924440.png

5.Terraform删除资源失败 (S3)

(1)报错

?
│ Error: deleting S3 Bucket (terraform-state-devmaojing): operation error S3: DeleteBucket, https response error StatusCode: 409, RequestID: MHHWXPQC0K43P5R4, HostID: 6CaQATqB7FfLhMwfaYhbTwMR+g1SVHho5FFrycVXtRXXo5P5b3+43oEJMlXY1+MopR5hcXD34zEW0/p2lWUUXYL3MRiWnKj6, api error BucketNotEmpty: The bucket you tried to delete is not empty. You must delete all versions in the bucket.
│

955ec0129efb4c16b104064cc8416cc6.png

(2)原因分析

存储桶未清空

?

(3)解决方法

先清空后删除

cf88a02084ae4c55b9139d8d373fdb6d.png

?

?

6.AWS 有哪些区域和可用区

(1)查询

区域和可用区 - Amazon Elastic Compute Cloud

4d11e9071977459792d3ab378054e742.png

?

文章来源:https://blog.csdn.net/cronaldo91/article/details/135658928
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。