无法挂载terraform创建的体积

时间:2019-07-11 07:04:07

标签: terraform terraform-provider-aws

我正在使用以下Terraform模板

resource "aws_instance" "ec2" {
  ami           = "${var.ami_id}"
  instance_type = "${var.flavor}"
  key_name = "${var.key_name}"
  availability_zone = "${var.availability_zone}"
  security_groups= ["${var.security_group}"]
  tags = {Name = "${var.instance_name}"}
  }

resource "aws_volume_attachment" "ebs_volume" {
  device_name = "/dev/sdg"
  volume_id = "vol-006d716dad719545c"
  instance_id = "${aws_instance.ec2.id}"
}

在aws中启动实例并将卷附加到该实例。

执行此操作时,我看到实例已创建,并且卷也已附加到实例。

ubuntu@ip-172-31-10-43:~$ lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0     7:0    0   91M  1 loop /snap/core/6350
loop1     7:1    0   18M  1 loop /snap/amazon-ssm-agent/930
loop2     7:2    0 88.4M  1 loop /snap/core/6964
loop3     7:3    0   18M  1 loop /snap/amazon-ssm-agent/1335
xvda    202:0    0   50G  0 disk
└─xvda1 202:1    0   50G  0 part /
xvdg    202:96   0   20G  0 disk

但是当我尝试挂载该卷时,我收到了这个奇怪的错误

ubuntu@ip-172-31-10-43:~$ sudo mkdir -p /goutham
ubuntu@ip-172-31-10-43:~$ sudo mount /dev/xvdg /goutha,
mount: /goutha,: mount point does not exist.
ubuntu@ip-172-31-10-43:~$ sudo mount /dev/xvdg /goutham
mount: /goutham: wrong fs type, bad option, bad superblock on /dev/xvdg, missing codepage or helper program, or other error.

任何人都可以帮我解决我在练习中犯的错误。

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为我错过了在该卷中创建文件系统的步骤,因为我试图附加的卷是一个空卷

所以这帮助了我

$ sudo mkfs -t xfs /dev/xvdg

sudo mkdir -p /goutham

sudo mount /dev/xvdg /goutham

谢谢

答案 1 :(得分:0)

您可以使用用户数据和terraform脚本在连接的磁盘上建立文件系统。

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

https://www.terraform.io/docs/providers/aws/r/instance.html#user_data

  1. 创建一个sh文件,templates / mkfs.sh
#!/bin/bash
while ! ls /dev/xvdg > /dev/null
do 
    sleep 5
done

if [ `file -s /dev/xvdg | cut -d ' ' -f 2` = 'data' ]
then
    mkfs.xfs /dev/xvdg
fi
  1. terraform脚本
data "template_file" "mkfs" {
  template = "${file("${path.module}/templates/mkfs.sh")}"
}

resource "aws_instance" "ec2" {
  ...
  user_data = "${data.template_file.mkfs}"
  ...
}

它将在创建ec2实例时运行,并等待直到安装磁盘为止。之后,它将创建文件系统。