项目作者: tajroshith

项目描述 :
EC2 deployed using AWS-CLI for a client.
高级语言:
项目地址: git://github.com/tajroshith/awscli-ec2-creation.git
创建时间: 2021-08-17T16:09:28Z
项目社区:https://github.com/tajroshith/awscli-ec2-creation

开源协议:

下载


EC2 - Configuration & Deployment Using AWS-CLI

This is a project i did for a client, the requirement was to build an infrastucture for a webserver with no access to the AWS console.
Further requirement was to create and attach an additional volume to the ec2 instance. An IAM user with programmatic access was also provided.
Below is a detailed summary of steps that i have performed to complete the project.

Pre-requisites for this project

  • Needs AWS CLI access preferrably a user with admin access
  • AWS CLI needs to be installed on your system
  • Knowledge of AWS CLI commands

Resources

List of AWS resources created via AWS-CLI

  • EC2
  • Security Group
  • AMI (Amazon Machine Image)
  • Key-Pair
  • Additional EBS Volume
  • Internet Gateway
  • Route Table
  • VPC

Configuring the AWS CLI User

  1. aws configure --profile=taj-awscli

Output

  1. AWS Access Key ID [None]: Provide AWS Access Key ID
  2. AWS Secret Access Key [None]: Provide AWS Secret Access Key
  3. Default region name [None]: Provide region name
  4. Default output format [None]: Provide output format

We configured the AWS-CLI admin user with the access key and secrey key.

Creating Infrastructure for EC2 Deployment

We create a VPC with CIDR-Block of 172.17.0.0/16

  1. aws ec2 create-vpc --cidr-block 172.17.0.0/16

Output

  1. {
  2. "Vpc": {
  3. "CidrBlock": "172.17.0.0/16",
  4. "DhcpOptionsId": "dopt-37b8c65c",
  5. "State": "pending",
  6. "VpcId": "vpc-0f8eb530f20a64a39",
  7. "OwnerId": "898279796273",
  8. "InstanceTenancy": "default",
  9. "Ipv6CidrBlockAssociationSet": [],
  10. "CidrBlockAssociationSet": [
  11. {
  12. "AssociationId": "vpc-cidr-assoc-016cd714c07a59db3",
  13. "CidrBlock": "172.17.0.0/16",
  14. "CidrBlockState": {
  15. "State": "associated"
  16. }
  17. }
  18. ],
  19. "IsDefault": false
  20. }
  21. }

Assigning the VPC a tag “webserver-vpc” for proper identification.

  1. aws ec2 create-tags --resources vpc-0f8eb530f20a64a39 --tags Key=Name,Value=Webserver-vpc

We now create 3 subnets for our infrasturcture, the subnets will be assigned with the following CIDR blocks

  1. aws ec2 create-subnet --vpc-id vpc-0f8eb530f20a64a39 --cidr-block 172.17.0.0/18
  1. aws ec2 create-subnet --vpc-id vpc-0f8eb530f20a64a39 --cidr-block 172.17.64.0/18
  1. aws ec2 create-subnet --vpc-id vpc-0f8eb530f20a64a39 --cidr-block 172.17.128.0/18

To provide access to the internet we create an internet gateway for our VPC.

  1. aws ec2 create-internet-gateway

Output

  1. {
  2. "InternetGateway": {
  3. "Attachments": [],
  4. "InternetGatewayId": "igw-0264fa7f3b74797b8",
  5. "OwnerId": "898279796273",
  6. "Tags": []
  7. }
  8. }

As with the previous steps we assign tags for proper identification.

  1. aws ec2 create-tags --resources igw-0264fa7f3b74797b8 --tags Key=Name,Value=igw-webserver

Now we need to attach the internet gateway to our VPC

  1. aws ec2 attach-internet-gateway --internet-gateway-id igw-0264fa7f3b74797b8
  2. --vpc-id vpc-0f8eb530f20a64a39

We create a custom route table so as to route the traffic to our internet gateway.

  1. aws ec2 create-route-table --vpc-id vpc-0f8eb530f20a64a39

Output

  1. {
  2. "RouteTable": {
  3. "Associations": [],
  4. "PropagatingVgws": [],
  5. "RouteTableId": "rtb-0e17ff7cacbe06aac",
  6. "Routes": [
  7. {
  8. "DestinationCidrBlock": "172.17.0.0/16",
  9. "GatewayId": "local",
  10. "Origin": "CreateRouteTable",
  11. "State": "active"
  12. }
  13. ],
  14. "Tags": [],
  15. "VpcId": "vpc-0f8eb530f20a64a39",
  16. "OwnerId": "898279796273"
  17. }
  18. }

Now pointing our route table to our internet gateway to route all traffic.

  1. aws ec2 create-route --route-table-id rtb-0e17ff7cacbe06aac --destination-cidr-block 0.0.0.0/0
  2. --gateway-id igw-0264fa7f3b74797b8

Tagging the route table.

  1. aws ec2 create-tags --resources rtb-0e17ff7cacbe06aac --tags Key=Name,Value=rtb-webserver

To verify the routes we created in route table we use the following command to describe the route table and its contents.

  1. aws ec2 describe-route-tables --route-table-id rtb-0e17ff7cacbe06aac

Output

  1. {
  2. "RouteTables": [
  3. {
  4. "Associations": [],
  5. "PropagatingVgws": [],
  6. "RouteTableId": "rtb-0e17ff7cacbe06aac",
  7. "Routes": [
  8. {
  9. "DestinationCidrBlock": "172.17.0.0/16",
  10. "GatewayId": "local",
  11. "Origin": "CreateRouteTable",
  12. "State": "active"
  13. },
  14. {
  15. "DestinationCidrBlock": "0.0.0.0/0",
  16. "GatewayId": "igw-0264fa7f3b74797b8",
  17. "Origin": "CreateRoute",
  18. "State": "active"
  19. }
  20. ],
  21. "Tags": [
  22. {
  23. "Key": "Name",
  24. "Value": "rtb-webserver"
  25. }
  26. ],
  27. "VpcId": "vpc-0f8eb530f20a64a39",
  28. "OwnerId": "898279796273"
  29. }
  30. ]
  31. }

We now associate the created subnets with our route table which in turn is now connected to the internet gateway. First we use the following command to identify our subnets.

  1. aws ec2 describe-subnets --filters "Name=vpc-id,Values=vpc-0f8eb530f20a64a39"
  2. --query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock}"

Output

  1. [
  2. {
  3. "ID": "subnet-00c0a8f7de82f11a8",
  4. "CIDR": "172.17.128.0/18"
  5. },
  6. {
  7. "ID": "subnet-0e84cc4af28cca9ef",
  8. "CIDR": "172.17.64.0/18"
  9. },
  10. {
  11. "ID": "subnet-0032eb27a14442a1e",
  12. "CIDR": "172.17.0.0/18"
  13. }
  14. ]

Associating subnets with our route table

  1. aws ec2 associate-route-table --subnet-id subnet-00c0a8f7de82f11a8
  2. --route-table-id rtb-0e17ff7cacbe06aac
  3. aws ec2 associate-route-table --subnet-id subnet-0e84cc4af28cca9ef
  4. --route-table-id rtb-0e17ff7cacbe06aac
  5. aws ec2 associate-route-table --subnet-id subnet-0032eb27a14442a1e
  6. --route-table-id rtb-0e17ff7cacbe06aac

Instances launched using these subnets require public-IP so we modify the subnets attribute as such it auto-assigns a public-IP.

  1. aws ec2 modify-subnet-attribute --subnet-id subnet-00c0a8f7de82f11a8
  2. --map-public-ip-on-launch
  3. aws ec2 modify-subnet-attribute --subnet-id subnet-0e84cc4af28cca9ef
  4. --map-public-ip-on-launch
  5. aws ec2 modify-subnet-attribute --subnet-id subnet-0032eb27a14442a1e
  6. --map-public-ip-on-launch

Now our basic infrastructure is completed and we move onto creation of EC2.

Creating Security Group

We create the security group in our VPC and tag it for proper identification.

  1. aws ec2 create-security-group --group-name web-sg --description "Security group for webserver"
  2. --vpc-id vpc-0f8eb530f20a64a39
  1. aws ec2 create-tags --resources sg-08e4bfd92f793c6a5 --tags Key=Name,Value=web-sg

Output

  1. {
  2. "GroupId": "sg-08e4bfd92f793c6a5"
  3. }

Inorder to access our EC2 instance via SSH we are opening port 22 and also ports 80 & 443 as we need to access the webserver.

  1. aws ec2 authorize-security-group-ingress --group-id sg-08e4bfd92f793c6a5 --protocol tcp
  2. --port 22 --cidr 0.0.0.0/0
  3. aws ec2 authorize-security-group-ingress --group-id sg-08e4bfd92f793c6a5 --protocol tcp
  4. --port 80 --cidr 0.0.0.0/0
  5. aws ec2 authorize-security-group-ingress --group-id sg-08e4bfd92f793c6a5 --protocol tcp
  6. --port 443 --cidr 0.0.0.0/0

Creating Key-Pair

The proceeding line creates a 2048-bit RSA key pair. The aws ec2 command stores the public key and outputs the private key to save to a file.

  1. aws ec2 create-key-pair --key-name cli-web-keypair --query "KeyMaterial"
  2. --output text > cli-web-keypair-key.pem

AMI (Amazon Machine Image)

When creating a EC2 instance from the command line, we specify the operating system using the amazon machine image (AMI) ID. To get the image ID we use the following command which lists the latest AMI Image Id.

  1. aws ec2 describe-images --owners amazon
  2. --filters "Name=name,Values=amzn2-ami-hvm-2.0.????????-x86_64-gp2"
  3. "Name=state,Values=available" --output json

From the output we get the AMI ID

Output

  1. {
  2. "Architecture": "x86_64",
  3. "CreationDate": "2019-02-19T22:42:34.000Z",
  4. "ImageId": "ami-0ed72083dbed1d548",
  5. "ImageLocation": "amazon/amzn2-ami-hvm-2.0.20190218-x86_64-gp2",
  6. "ImageType": "machine",
  7. "Public": true,
  8. "OwnerId": "137112412989",
  9. "PlatformDetails": "Linux/UNIX",
  10. "UsageOperation": "RunInstances",
  11. "State": "available",
  12. "BlockDeviceMappings": [
  13. {

EC2 Creation

We provide the AMI ID, Security group id, Subnet id and the keypair name.

  1. aws ec2 run-instances --image-id ami-0ed72083dbed1d548 --count 1 --instance-type t2.micro
  2. --key-name cli-web-keypair
  3. --security-group-ids sg-08e4bfd92f793c6a5 --subnet-id subnet-00c0a8f7de82f11a8

We can list our created EC2 instance with the following command

  1. aws ec2 describe-instances

We now have to create an additional storage of 2GB. We have to create the additional EBS volume in the same availability zone. The following command will provide details of the availability zone.

  1. aws ec2 describe-availability-zones

We now create the additional EBS volume in the same availabilitz zone of the EC2 instance.

  1. aws ec2 create-volume --region us-east-2 --availability-zone us-east-2b --size 2 --volume-type gp2

Output

  1. {
  2. "AvailabilityZone": "us-east-2b",
  3. "CreateTime": "2021-08-19T07:21:56.000Z",
  4. "Encrypted": false,
  5. "Size": 2,
  6. "SnapshotId": "",
  7. "State": "creating",
  8. "VolumeId": "vol-027b77beb837467a8",
  9. "Iops": 100,
  10. "Tags": [],
  11. "VolumeType": "gp2",
  12. "MultiAttachEnabled": false
  13. }

We get the volume id when creating the additional volume. For attaching our volume we provide the following line.

  1. aws ec2 attach-volume --volume-id vol-027b77beb837467a8
  2. --instance-id i-049e0957acea10d47 --device /dev/sdf

Output

  1. {
  2. "AttachTime": "2021-08-19T07:28:49.947Z",
  3. "Device": "/dev/sdf",
  4. "InstanceId": "i-049e0957acea10d47",
  5. "State": "attaching",
  6. "VolumeId": "vol-027b77beb837467a8"
  7. }

In order to SSH to our EC2 Instance we need to get the Public IP, we can provide the following command to fetch the public IP of our instance.

  1. aws ec2 describe-instances --instance-ids i-049e0957acea10d47
  2. --query 'Reservations[0].Instances[0].PublicIpAddress'

Finally, we can connect to our EC2 instance.

  1. ssh -i cli-web-keypair.pem ec2-user@YOUR_PUBLIC_IP

Package Installation

  1. yum install httpd -y
  2. systemctl restart httpd
  3. systemctl enable httpd

Creating Partition and Mounting it

  1. # fdisk /dev/xvdf
  2. Command (m for help): n
  3. Partition type
  4. p primary (0 primary, 0 extended, 4 free)
  5. e extended (container for logical partitions)
  6. Select (default p):
  7. Using default response p.
  8. Partition number (1-4, default 1):
  9. First sector (2048-4194303, default 2048):
  10. Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303):
  11. Created a new partition 1 of type 'Linux' and of size 2 GiB.
  12. Command (m for help): w

We have created the partition now we format it with a filesystem and mount the partition.

  1. mkfs -t xfs /dev/xvdf1
  2. mount /dev/xvdf1 /var/www/html
  3. df -h
  4. Filesystem Size Used Avail Use% Mounted on
  5. /dev/xvdf1 2.0G 6.0M 1.9G 1% /var/www/html

We can also add the necessary entries in the /etc/fstab file so the mount point persists even after a reboot.

  1. echo "/dev/xvdf1 /var/www/html xfs defaults 0 2" >> /etc/fstab

Now we add the web files to the document root, apply necessary permissions and map the domain name to our public Ip.

For Stopping our EC2 Instance

  1. aws ec2 stop-instances --instance-ids i-049e0957acea10d47

For Terminating our EC2 Instance

  1. aws ec2 terminate-instances --instance-ids i-049e0957acea10d47

Conclusion

We completed the project by using aws-cli commands and aws resources to achieve our goals.