[Terraform] 07 - Create a website

一、首先定义VPC

associations:(子网,路由表)

aws_route_table_association --> 

  aws_subnet --> aws_vpc

  aws_route_table --> aws_internet_gateway --> aws_vpc

 

 

二、再定义ELB

流量通过ELB导入instance。

Your load balancer nodes distribute requests from clients to registered targets.

When you have enabled the cross-zone load balancing, each load balancer node distributes traffic across the registered targets in all enabled availability zones.

When cross-zone load balancing is disabled, each load balancer node distributes traffic across the registered targets in its own availability zone itself.

resource "aws_elb" "my-elb" {
  name            = "my-elb"
subnets = [aws_subnet.main-public-1.id, aws_subnet.main-public-2.id]
security_groups
= [aws_security_group.elb-securitygroup.id]    # ----> securitygroup.tf
listener { instance_port
= 80 instance_protocol = "http" lb_port = 80 lb_protocol = "http" }
health_check { healthy_threshold
= 2 unhealthy_threshold = 2 timeout = 3 target = "HTTP:80/" interval = 30 } cross_zone_load_balancing = true connection_draining = true connection_draining_timeout = 400

tags = { Name = "my-elb" }
instances
= ["${aws_instance.example.id}"] }

 

 

三、instance配置

  • 网络配置

配置在vpc下的哪个子网

vpc+安全组:ssh,80端口 for instance; 以及80端口 for ELB。

再配置好SSH。

  # the VPC subnet
  subnet_id = aws_subnet.main-public-1.id

  # the security group
  vpc_security_group_ids = [aws_security_group.myinstance.id]

  # the public SSH key
  key_name = aws_key_pair.mykeypair.key_name

 

  • 启动后的初始脚本

具体执行内容定义在 --> cloudinit.tf

 # user data
  user_data = data.template_cloudinit_config.cloudinit-example.rendered

Here, 网页文件的加载和配置。

  

  • 添加EBS

Amazon EBS 快照是数据块数据的时间副本中的一个点。EBS 快照是以增量方式存储的,这意味着您只需为存储的更改数据块付费。

特点举例:存储卷像裸块设备一样,有块设备接口,可以在卷上创建文件系统

resource "aws_ebs_volume" "ebs-volume-1" {
  availability_zone = "ap-southeast-1a"
  size              = 1
  type              = "gp2"
  tags = {
    Name = "extra volume data"
  }
}

# ----------------------------------------------------------
resource
"aws_volume_attachment" "ebs-volume-1-attachment" { device_name = "/dev/xvdh" volume_id = aws_ebs_volume.ebs-volume-1.id instance_id = aws_instance.example.id skip_destroy = true # skip destroy to avoid issues with terraform destroy }

 

  • 添加EIP

弹性IP地址是您从AWS请求并分配给您的地址,直到您选择将其发布回AWS.这意味着只有您可以使用此IP地址.

弹性IP地址相对于自动分配的公共IP地址的好处是:

[不变的]:

如果您不为EC2实例使用弹性IP地址,则它将不具有公共IP地址,或者如果实例已停止,则其公共IP地址将更改。

使用弹性IP地址时,EC2实例可以永久保留其面向Internet的IP地址。

如果需要将EC2实例的IP地址放在DNS中,请使用弹性IP地址。

[可移动的]:

如果您的实例失败,您可以将弹性IP地址移动到另一个EC2实例,您的用户和DNS条目不必更改任何内容.他们将开始使用新的EC2实例.

 

# 在instance.tf内定义
resource "aws_eip" "lb" { instance = aws_instance.example.id  # 给instance设置 elastic ip vpc = true }

 

 

四、S3的配置

Ref: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket

resource "aws_s3_bucket" "aws_assignment" {
  bucket = "my-tf-test-bucket-fagljlkjandrew"
  acl    = "public-read"

  tags = {
    Name        = "My bucket"
    Environment = "Dev"
  }

  versioning {
    enabled = true
  }
}

 

 End.

posted @ 2020-11-13 13:00  郝壹贰叁  阅读(142)  评论(0编辑  收藏  举报