AWS : Attaching Amazon EBS volume to an instance
Each Amazon EC2 instance that we launch has an associated root device volume, either an Amazon Elastic Block Store (Amazon EBS) volume or an instance store volume. We can use block device mapping to specify additional Amazon EBS volumes or instance store volumes to attach to an instance when it's launched.
We can also attach additional Amazon EBS volumes to a running instance.
We can attach our Amazon EBS volume to one of our instances in the same Availability Zone.
To attach an Amazon EBS volume to an instance (we're using CentOS 7) using the console:
- Open the Amazon EC2 console.
- Click Volumes in the navigation pane.
- Select a volume and then click Attach Volume.
- In the Attach Volume dialog box, start typing the name or ID of the instance to attach the volume to in the Instance box, and select it from the list of suggestion options (only instances in the same Availability Zone as the volume are displayed). Encrypted volumes can only be attached to instances that support Amazon EBS encryption.
- Verify that the suggested device name is suitable, or enter a device name in the Device box.
- Click Attach to attach the volume to the instance. The volume and instance must be in the same Availability Zone.
We should see the following in the "Description" tab of the instance:
After we attach an Amazon EBS volume to our instance, it is exposed as a block device.
We can format the volume with any file system (mkfs
) and then mount it (mount
).
After we make the EBS volume available for use, we can access it in the same ways that we access any other volume.
Use the following procedure to make an Amazon EBS volume available for use on Linux:
- Connect to our instance using SSH.
$ ssh -i my_aws_pem.pem centos@54.210.155.188 [centos@ip-172-31-61-253 ~]$
- Depending on the block device driver of the kernel, the device might be attached with a different name than what we specify.
For example, if we specify a device name of /dev/sdh, our device might be renamed /dev/xvdh or /dev/hdh by the kernel;
in most cases, the trailing letter remains the same.
In some versions of Red Hat Enterprise Linux (and its variants, such as CentOS), even the trailing letter might also change
(where /dev/sda could become /dev/xvde). In these cases, each device name trailing letter is incremented the same number of times.
For example, /dev/sdb would become /dev/xvdf and /dev/sdc would become /dev/xvdg.
Amazon Linux AMIs create a symbolic link from the renamed device path to the name we specify, but other AMIs might behave differently.
Use thelsblk
command to view our available disk devices and their mount points (if applicable) to help us determine the correct device name to use.$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk
The output of lsblk removes the /dev/ prefix from full device paths. In this example, /dev/xvda1 is mounted as the root device (note the MOUNTPOINT is listed as /, the root of the Linux file system hierarchy), and /dev/xvdf is attached, but it has not been mounted yet.
We can get more detailed info from
fdisk command:$ sudo fdisk -l Disk /dev/xvda: 8589 MB, 8589934592 bytes, 16777216 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000b723c Device Boot Start End Blocks Id System /dev/xvda1 * 2048 16777215 8387584 83 Linux Disk /dev/xvdf: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
- Determine whether we need to create a file system on the volume.
New volumes are raw block devices, and we need to create a file system on them before we can mount and use them.
Volumes that have been restored from snapshots likely have a file system on them already;
if we create a new file system on top of an existing file system, the operation overwrites our data.
Use the
file -s
command to list special information, such as file system type.$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk $ sudo file -s /dev/xvda1 /dev/xvda1: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs) $ sudo file -s /dev/xvda /dev/xvda: x86 boot sector; partition 1: ID=0x83, active, starthead 32, startsector 2048, 16775168 sectors, code offset 0x63 $ sudo file -s /dev/xvdf /dev/xvdf: data
While the instance volume contains Linux xfs filesystem, the new volume (/dev/xvdf) needs format. - (Conditional) Use the following command to create an xfs file system on the volume.
Substitute the device name (such as /dev/xvdf) for device_name.
Depending on the requirements of our application or the limitations of our operating system,
we can choose a different file system type, such as ext3 or others. List of the file types:
$ sudo fdisk /dev/xvdf Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xf09e309b. Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Command (m for help):
We can also use
mkfs
[TAB][TAB] what themkfs
can do in terms of file types:$ mkfs mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
Caution :
This formatting step assumes that we're mounting an empty volume. If we're mounting a volume that already has data on it (for example, a volume that was restored from a snapshot), don't usemkfs
before mounting the volume (skip to the next step instead). Otherwise, we'll format the volume and delete the existing data.$ sudo mkfs.xfs /dev/xvdf meta-data=/dev/xvdf isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 $ sudo file -s /dev/xvdf /dev/xvdf: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)
- Use the
mkdir mount_point
command to create a mount point directory for the volume. The mount point is where the volume is located in the file system tree. Substitute a location for mount_point, such as /vol. - Use the
mount
command to mount the volume at the location we just created. This will mount the filesystem on /dev/xvdf into the folder /vol. That means that going into /vol will show us the filesystem which is on /dev/xvdf. - We can check if the xvdf is mounted properly:
- (Optional I) To mount this Amazon EBS volume on every system reboot, add an entry for the device to the /etc/fstab file.
- Create a backup of our /etc/fstab file that we can use if we accidentally destroy or delete this file while we are editing it.
- Open the /etc/fstab file and add a new line to the end of the file for our volume using the following format:
device_name mount_point file_system_type fs_mntops fs_freq fs_passno
The last three fields on this line are the file system mount options, the dump frequency of the file system, and the order of file system checks done at boot time. If we don't know what these values should be, then use the values in the example below for them (defaults, nofail 0 2). For more information on /etc/fstab entries, see the fstab manual page (by entering man fstab on the command line). For example, to mount the xfs file system on the device /dev/xvdf at the mount point /vol, add the following entry to /etc/fstab.
(Note)
If we ever intend to boot our instance without this volume attached (for example, so this volume could move back and forth between different instances), we should add the nofail mount option that allows the instance to boot even if there are errors in mounting the volume. Debian derivatives, such as Ubuntu, must also add the nobootwait mount option./dev/xvdf /vol xfs defaults,nofail 0 2
- After we've added the new entry to /etc/fstab, we need to check that our entry works.
Run the
sudo mount -a
command to mount all file systems in /etc/fstab. - Let's check out dish using
df -h
command: -
To make sure if the volume mount at the boot time, we can check it again after rebooting the instance.
$ sudo cp /etc/fstab /etc/fstab.orig
$ sudo mount -a
$ df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 220M 0 220M 0% /dev tmpfs 243M 0 243M 0% /dev/shm tmpfs 243M 8.5M 235M 4% /run tmpfs 243M 0 243M 0% /sys/fs/cgroup /dev/xvda1 8.0G 905M 7.2G 12% / tmpfs 49M 0 49M 0% /run/user/1000 /dev/xvdf 1014M 33M 982M 4% /vol
- (Optional II) We can run the following script with root privilege (To run a script for the steps #4-8):
#!/bin/bash mkfs.xfs /dev/xvdf mkdir /vol echo "/dev/xvdf /vol auto noatime 0 0" | sudo tee -a /etc/fstab
- (Optional III) partition: we can make partitions using
fdisk
:Current block device info looks like this:
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk
Now, we'll create two partitions that will look like this:
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk ├─xvdf1 202:81 0 399M 0 part └─xvdf2 202:82 0 99M 0 part
Before we make partitions, we need to un-mount the volume:
$ umount /vol $ df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 220M 0 220M 0% /dev tmpfs 243M 0 243M 0% /dev/shm tmpfs 243M 8.5M 235M 4% /run tmpfs 243M 0 243M 0% /sys/fs/cgroup /dev/xvda1 8.0G 906M 7.2G 12% / tmpfs 49M 0 49M 0% /run/user/1000
Let's make the partitions:
$ sudo fdisk /dev/xvdf Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Partition number (1-4, default 1): First sector (2048-2097151, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +399M Partition 1 of type Linux and of size 399 MiB is set Command (m for help): p Disk /dev/xvdf: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x7301381b Device Boot Start End Blocks Id System /dev/xvdf1 2048 819199 408576 83 Linux Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): Partition number (2-4, default 2): First sector (819200-2097151, default 819200): Using default value 819200 Last sector, +sectors or +size{K,M,G} (819200-2097151, default 2097151): +399M Partition 2 of type Linux and of size 99 MiB is set Command (m for help): p Disk /dev/xvdf: 1073 MB, 1073741824 bytes, 2097152 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x7301381b Device Boot Start End Blocks Id System /dev/xvdf1 2048 819199 408576 83 Linux /dev/xvdf2 819200 1021951 101376 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
Note that the input during the above step means the following:
- n: new (create a new one)
- p: print current block
- w: write
Again, here is our new block info:
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk ├─xvdf1 202:81 0 399M 0 part └─xvdf2 202:82 0 99M 0 part
$ sudo mkdir /vol
$ sudo mount /dev/xvdf /vol
$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvda 202:0 0 8G 0 disk └─xvda1 202:1 0 8G 0 part / xvdf 202:80 0 1G 0 disk /vol
AWS (Amazon Web Services)
- AWS : EKS (Elastic Container Service for Kubernetes)
- AWS : Creating a snapshot (cloning an image)
- AWS : Attaching Amazon EBS volume to an instance
- AWS : Adding swap space to an attached volume via mkswap and swapon
- AWS : Creating an EC2 instance and attaching Amazon EBS volume to the instance using Python boto module with User data
- AWS : Creating an instance to a new region by copying an AMI
- AWS : S3 (Simple Storage Service) 1
- AWS : S3 (Simple Storage Service) 2 - Creating and Deleting a Bucket
- AWS : S3 (Simple Storage Service) 3 - Bucket Versioning
- AWS : S3 (Simple Storage Service) 4 - Uploading a large file
- AWS : S3 (Simple Storage Service) 5 - Uploading folders/files recursively
- AWS : S3 (Simple Storage Service) 6 - Bucket Policy for File/Folder View/Download
- AWS : S3 (Simple Storage Service) 7 - How to Copy or Move Objects from one region to another
- AWS : S3 (Simple Storage Service) 8 - Archiving S3 Data to Glacier
- AWS : Creating a CloudFront distribution with an Amazon S3 origin
- AWS : Creating VPC with CloudFormation
- AWS : WAF (Web Application Firewall) with preconfigured CloudFormation template and Web ACL for CloudFront distribution
- AWS : CloudWatch & Logs with Lambda Function / S3
- AWS : Lambda Serverless Computing with EC2, CloudWatch Alarm, SNS
- AWS : Lambda and SNS - cross account
- AWS : CLI (Command Line Interface)
- AWS : CLI (ECS with ALB & autoscaling)
- AWS : ECS with cloudformation and json task definition
- AWS Application Load Balancer (ALB) and ECS with Flask app
- AWS : Load Balancing with HAProxy (High Availability Proxy)
- AWS : VirtualBox on EC2
- AWS : NTP setup on EC2
- AWS: jq with AWS
- AWS & OpenSSL : Creating / Installing a Server SSL Certificate
- AWS : OpenVPN Access Server 2 Install
- AWS : VPC (Virtual Private Cloud) 1 - netmask, subnets, default gateway, and CIDR
- AWS : VPC (Virtual Private Cloud) 2 - VPC Wizard
- AWS : VPC (Virtual Private Cloud) 3 - VPC Wizard with NAT
- DevOps / Sys Admin Q & A (VI) - AWS VPC setup (public/private subnets with NAT)
- AWS - OpenVPN Protocols : PPTP, L2TP/IPsec, and OpenVPN
- AWS : Autoscaling group (ASG)
- AWS : Setting up Autoscaling Alarms and Notifications via CLI and Cloudformation
- AWS : Adding a SSH User Account on Linux Instance
- AWS : Windows Servers - Remote Desktop Connections using RDP
- AWS : Scheduled stopping and starting an instance - python & cron
- AWS : Detecting stopped instance and sending an alert email using Mandrill smtp
- AWS : Elastic Beanstalk with NodeJS
- AWS : Elastic Beanstalk Inplace/Rolling Blue/Green Deploy
- AWS : Identity and Access Management (IAM) Roles for Amazon EC2
- AWS : Identity and Access Management (IAM) Policies, sts AssumeRole, and delegate access across AWS accounts
- AWS : Identity and Access Management (IAM) sts assume role via aws cli2
- AWS : Creating IAM Roles and associating them with EC2 Instances in CloudFormation
- AWS Identity and Access Management (IAM) Roles, SSO(Single Sign On), SAML(Security Assertion Markup Language), IdP(identity provider), STS(Security Token Service), and ADFS(Active Directory Federation Services)
- AWS : Amazon Route 53
- AWS : Amazon Route 53 - DNS (Domain Name Server) setup
- AWS : Amazon Route 53 - subdomain setup and virtual host on Nginx
- AWS Amazon Route 53 : Private Hosted Zone
- AWS : SNS (Simple Notification Service) example with ELB and CloudWatch
- AWS : Lambda with AWS CloudTrail
- AWS : SQS (Simple Queue Service) with NodeJS and AWS SDK
- AWS : Redshift data warehouse
- AWS : CloudFormation
- AWS : CloudFormation Bootstrap UserData/Metadata
- AWS : CloudFormation - Creating an ASG with rolling update
- AWS : Cloudformation Cross-stack reference
- AWS : OpsWorks
- AWS : Network Load Balancer (NLB) with Autoscaling group (ASG)
- AWS CodeDeploy : Deploy an Application from GitHub
- AWS EC2 Container Service (ECS)
- AWS EC2 Container Service (ECS) II
- AWS Hello World Lambda Function
- AWS Lambda Function Q & A
- AWS Node.js Lambda Function & API Gateway
- AWS API Gateway endpoint invoking Lambda function
- AWS API Gateway invoking Lambda function with Terraform
- AWS API Gateway invoking Lambda function with Terraform - Lambda Container
- Amazon Kinesis Streams
- AWS: Kinesis Data Firehose with Lambda and ElasticSearch
- Amazon DynamoDB
- Amazon DynamoDB with Lambda and CloudWatch
- Loading DynamoDB stream to AWS Elasticsearch service with Lambda
- Amazon ML (Machine Learning)
- Simple Systems Manager (SSM)
- AWS : RDS Connecting to a DB Instance Running the SQL Server Database Engine
- AWS : RDS Importing and Exporting SQL Server Data
- AWS : RDS PostgreSQL & pgAdmin III
- AWS : RDS PostgreSQL 2 - Creating/Deleting a Table
- AWS : MySQL Replication : Master-slave
- AWS : MySQL backup & restore
- AWS RDS : Cross-Region Read Replicas for MySQL and Snapshots for PostgreSQL
- AWS : Restoring Postgres on EC2 instance from S3 backup
- AWS : Q & A
- AWS : Security
- AWS : Security groups vs. network ACLs
- AWS : Scaling-Up
- AWS : Networking
- AWS : Single Sign-on (SSO) with Okta
- AWS : JIT (Just-in-Time) with Okta
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization