I have a bastion EC2 instance that I am trying to mount a Volume to (I want the Volume to persist even on Bastion replacement). I have read the docs but they tend to have bad non-working examples in the first place. I have created the Bastion and the Volume but I am not able to get the Volume to attach to the EC2 instance.
This is the code I am using currently (note: part of a larger construct I am working on):
// Bastion
this.bastion = new Instance(this, 'Bastion', {
instanceName: 'BASTION-' + this._vpcName,
vpc: this.vpc,
vpcSubnets: {
subnets: [_bastionSubnet],
availabilityZones:[
Stack.of(this).availabilityZones[0] // Force to same as Volume
]
},
machineImage: MachineImage.latestAmazonLinux({
generation: AmazonLinuxGeneration.AMAZON_LINUX_2,
}),
instanceType: _instanceType,
role: bastionRole,
userData: UserData.custom(bootscript),
userDataCausesReplacement: true,
securityGroup: this.securityGroup_Bastion,
keyName: this._props.bastion.keyName,
blockDevices: [
{
deviceName: '/dev/xvda',
volume: BlockDeviceVolume.ebs(_rootVolumeSize, {
volumeType: EbsDeviceVolumeType.GP2,
}),
},
],
});
const _targetDevice = '/dev/xvdz';
// Create Volume
this.volumeBackups = new Volume(this, 'backupsVolume', {
availabilityZone: Stack.of(this).availabilityZones[0], // Force to same as Bastion
size: Size.gibibytes(200),
encrypted: true,
volumeName: _targetDevice
});
// Add attach access
this.volumeBackups.grantAttachVolumeByResourceTag(this.bastion.grantPrincipal, [this.bastion]);
So far, what I am seeing, is the Bastion gets created and the Volume gets created. They have the expected VolumeGrantAttach-<suffix>
tag and they both match. When checking in AWS console, I do not see the Volume under the Storage Tab for the instance. When I log into the instance and run lsblk
, I do not see the volume available (just my root device).
$ sudo lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
nvme0n1 259:0 0 30G 0 disk
├─nvme0n1p1 259:1 0 30G 0 part /
└─nvme0n1p128 259:2 0 1M 0 part
At this point, I thought I was missing a mapping. I tried to add that to the bastion blockDevices
prop but that doesn't seem to be correct as the types don't work together. I even tried to add the mount to the first-run script but it isn't even recognized by the Instance, I have no way to mount it.
I am still not able to get the Volume to mount. I really have no idea what else to do.
question from:
https://stackoverflow.com/questions/65922864/unable-to-attach-volumes-to-ec2-instance-grantattachvolumebyresourcetag-not-cle