๐Ÿ™‚0629[AWS CLI]

๋ง์ง€ยท2022๋…„ 6์›” 29์ผ
0
post-custom-banner

๐Ÿ“Œ AWS CLI ํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

๐Ÿ“™ virtual Box์—์„œ centOS7 ์„ค์น˜


โœ”๏ธ ๋ฉ”๋ชจ๋ฆฌ : 4G

โœ”๏ธ ์ง€๊ธˆ ์ƒˆ ๊ฐ€์ƒ๋จธ์‹  ๋งŒ๋“ค๊ธฐ

โœ”๏ธ VDI

โœ”๏ธ ๋™์  ํ• ๋‹น ( ์‹ค๋ฌด์—์„œ๋Š” ๊ณ ์ • ํฌ๊ธฐ)

โœ”๏ธ 128GB-๋งŒ๋“ค๊ธฐ

โœ”๏ธ ํ”Œ๋กœํ”ผ ํ•ด์ œ

โœ”๏ธCPU 2๊ฐœ

โœ”๏ธ





์ง๋ ฌํฌํŠธ,๊ณต์œ ํด๋”,์‚ฌ์šฉ์ž์ธํ„ฐํŽ˜์ด์Šค ์ƒ๋žต

-> ์‹œ์ž‘

๐Ÿ“™ cento7์ดˆ๊ธฐ์„ค์ •

โœ”๏ธ english - Date & Time : seoul - installation destination

โœ”๏ธ network&host

โœ”๏ธ installํ•œ๋‹ค์Œ, rootpassword์ž…๋ ฅํ•ด์ฃผ๊ธฐ

โœ”๏ธ rebootํ•˜๊ณ  ์•„๋ž˜ ๋ช…๋ น์–ด ์ง„ํ–‰

--- CentOS7 ์„ค์ •
# yum install -y bash-completion wget unzip rdate
# rdate -s time.bora.net
# setenforce 0
# sed -i s/^SELINUX=.*$/SELINUX=disabled/ /etc/selinux/config
# cd /tmp
# systemctl disable --now firewalld
# yum update -y
# poweroff

๐Ÿ“™ ova ์ €์žฅํ•ด๋‘๊ธฐ

โœ”๏ธ ํŒŒ์ผ - ๊ฐ€์ƒ์‹œ์Šคํ…œ ๋‚ด๋ณด๋‚ด๊ธฐ - centos7์„ ํƒ - ๋‹ค์Œ - ๋‹ค์Œ - ๋‹ค์Œ - ๋‚ด๋ณด๋‚ด๊ธฐ ( ํŒŒ์ผ๋ช… cenOS-vbox.ova๋กœ ๋ฐ”๊ฟ”์ฃผ๊ธฐ ๊ฒฝ๋กœ '๋ฌธ์„œ'์— ์ €์žฅ๋˜์–ด์žˆ์Œ)

๐Ÿ“™ AWS CLI ์„ค์น˜

# cd /tmp
# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
# unzip awscliv2.zip
# ./aws/install
# aws --version
# aws configure



https://docs.aws.amazon.com/cli/latest/reference/

๐Ÿ“™ AWS ์ž๋™์™„์„ฑ

[root@localhost ~] vi .bash_profile
complete -C '/usr/local/bin/aws_completer' aws

๐Ÿ“Œ CLIํ™˜๊ฒฝ์—์„œ ๋กœ๊ทธ์ธ

๐Ÿ“™ aws CLI์—์„œ ๋กœ๊ทธ์ธ

โœ”๏ธ AWS -IAM - ์‚ฌ์šฉ์ž - ์‚ฌ์šฉ์ž ์ถ”๊ฐ€ - ์‚ฌ์šฉ์ž ์ด๋ฆ„ : mj - ์•ก์„ธ์Šค ํ‚ค - ๋‹ค์Œ

โœ”๏ธ ๊ธฐ์กด ์ •์ฑ… ์ง์ ‘ ์—ฐ๊ฒฐ - AdministratorAccess - ๋‹ค์Œ

โœ”๏ธ ํƒœ๊ทธ ์ƒ๋žต- ๋‹ค์Œ - ์‚ฌ์šฉ์ž ๋งŒ๋“ค๊ธฐ - .csv ๋‹ค์šด๋กœ๋“œ

โœ”๏ธ mobaxterm์—์„œ ์ ‘์†,์•„๋ž˜ ๋ช…๋ น์–ด ์ด์šฉํ•˜์—ฌ CLI์—์„œ ๋กœ๊ทธ์ธ

[root@localhost ~]# aws configure
AWS Access Key ID [None]: ์•ก์„ธ์Šค ํ‚ค ID ์ž…๋ ฅ
AWS Secret Access Key [None]: .csv์—ด์–ด์„œ secret key ์ž…๋ ฅ
Default region name [None]: ap-northeast-2
Default output format [None]: json

!--ํ™•์ธ ์ž‘์—…--!
[root@localhost ~]# aws s3 ls

๐Ÿ“Œ CLIํ™˜๊ฒฝ์—์„œ ๋„คํŠธ์›Œํฌ ์„ค์ •

๐Ÿ“™ VPC์™€ ์„œ๋ธŒ๋„ท ๋งŒ๋“ค๊ธฐ

# aws ec2 create-vpc --cidr-block 192.168.0.0/16 --tag-specification "ResourceType=vpc,Tags=[{Key=Name,Value=NEW-VPC}]" --output text

!--๊ฒฐ๊ณผ๊ฐ’์˜ vpc id๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# NEW_VPC=vpc-0eb8de2efd8ea573d
# echo $NEW_VPC

!--์„œ๋ธŒ๋„ท ์ƒ์„ฑ ํ›„ JSONํ˜•ํƒœ์˜ ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ(A)--!
# aws ec2 create-subnet --vpc-id $NEW_VPC --cidr-block 192.168.0.0/20 --availability-zone ap-northeast-2a --tag-specification "ResourceType=subnet,Tags=[{Key=Name,Value=NEW-PUBLIC-SUBNET-2A}]"

!-- ์„œ๋ธŒ๋„ท ์ƒ์„ฑ ํ›„ tableํ˜•ํƒœ์˜ ๊ฒฐ๊ณผ๊ฐ’ ์ถœ๋ ฅ(B)--!
# aws ec2 create-subnet --vpc-id $NEW_VPC --cidr-block 192.168.16.0/20 --availability-zone ap-northeast-2b --tag-specification "ResourceType=subnet,Tags=[{Key=Name,Value=NEW-PUBLIC-SUBNET-2B}]" --output table
# aws ec2 create-subnet --vpc-id $NEW_VPC --cidr-block 192.168.32.0/20 --availability-zone ap-northeast-2c --tag-specification "ResourceType=subnet,Tags=[{Key=Name,Value=NEW-PUBLIC-SUBNET-2C}]"
# aws ec2 create-subnet --vpc-id $NEW_VPC --cidr-block 192.168.48.0/20 --availability-zone ap-northeast-2d --tag-specification "ResourceType=subnet,Tags=[{Key=Name,Value=NEW-PUBLIC-SUBNET-2D}]"

!--๋งŒ๋“  ๊ฒƒ ํ™•์ธ --!
# aws ec2 describe-subnets --filters "Name=vpc-id,Values=$NEW_VPC" --query 'Subnets[*].{AZ:AvailabilityZone,CIDR:CidrBlock}'

๐Ÿ“™ ์„œ๋ธŒ๋„ท์„ ํผ๋ธ”๋ฆญ ์„œ๋ธŒ๋„ท์œผ๋กœ ๋งŒ๋“ค๊ธฐ(์ธํ„ฐ๋„ท ๊ฒŒ์ดํŠธ์›จ์ด)


# aws ec2 create-internet-gateway --tag-specification "ResourceType=internet-gateway,Tags=[{Key=Name,Value=NEW-IGW}]" --output text
!--๊ฒฐ๊ณผ๊ฐ’์˜ igw id๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# NEW_IGW=igw-027e8afbbc878150b
# aws ec2 attach-internet-gateway --vpc-id $NEW_VPC --internet-gateway-id $NEW_IGW
# aws ec2 describe-internet-gateways --output table
# aws ec2 describe-route-tables --filter "Name=vpc-id,Values=$NEW_VPC"
# NEW_RTB=rtb-00b7e49c4988758d4
# aws ec2 create-route --route-table-id $NEW_RTB --destination-cidr-block 0.0.0.0/0 --gateway-id $NEW_IGW
# aws ec2 create-tags --resources $NEW_RTB --tags "Key=Name,Value=NEW-PUBLIC-SUBNET-RTB"
# aws ec2 describe-route-tables --route-table-id $NEW_RTB --output table
# aws ec2 describe-subnets --filters "Name=vpc-id,Values=$NEW_VPC" --query 'Subnets[*].{ID:SubnetId,CIDR:CidrBlock}'
!--๊ฒฐ๊ณผ๊ฐ’์˜ subnet id๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# NEW_SID1=subnet-0513c8a4fa9377070
# NEW_SID2=subnet-04cfef78ec33e5e5f
# NEW_SID3=subnet-0ba09b125dd7b36d1
# NEW_SID4=subnet-0d71da42aad4e3515
# aws ec2 associate-route-table  --subnet-id $NEW_SID1 --route-table-id $NEW_RTB
# aws ec2 associate-route-table  --subnet-id $NEW_SID2 --route-table-id $NEW_RTB
# aws ec2 associate-route-table  --subnet-id $NEW_SID3 --route-table-id $NEW_RTB
# aws ec2 associate-route-table  --subnet-id $NEW_SID4 --route-table-id $NEW_RTB
!-- ํผ๋ธ”๋ฆญ IP์ฃผ์†Œ ์ž๋™ ํ• ๋‹น ํ™œ์„ฑํ™” --!
# aws ec2 modify-subnet-attribute --subnet-id $NEW_SID1 --map-public-ip-on-launch
# aws ec2 modify-subnet-attribute --subnet-id $NEW_SID2 --map-public-ip-on-launch
# aws ec2 modify-subnet-attribute --subnet-id $NEW_SID3 --map-public-ip-on-launch
# aws ec2 modify-subnet-attribute --subnet-id $NEW_SID4 --map-public-ip-on-launch
!-- ํผ๋ธ”๋ฆญdns๊ธฐ๋Šฅํ—ˆ์šฉ --!
# aws ec2 modify-vpc-attribute --vpc-id $NEW_VPC --enable-dns-hostnames

๐Ÿ“Œ CLIํ™˜๊ฒฝ์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์„ค์ •

๐Ÿ“™ ํ‚คํŽ˜์–ด, ๋ณด์•ˆ๊ทธ๋ฃน ๋งŒ๋“ค๊ธฐ

# aws ec2 create-key-pair --key-name new-key --query 'KeyMaterial' --output text > new-key.pem
# chmod 400 new-key.pem
# aws ec2 create-security-group --group-name NEW-SG-WEB --description "Security group for HTTP_SSH access" --vpc-id $NEW_VPC
!--๊ฒฐ๊ณผ๊ฐ’์˜ subnet id๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# NEW_SG=sg-0116cd5ea641b5cd2
# aws ec2 authorize-security-group-ingress --group-id $NEW_SG --protocol tcp --port 22 --cidr 0.0.0.0/0
or
cidr 123.142.252.25/32 ( ๊ฐ•์˜์‹ค์—์„œ๋งŒ ์ ‘์† ๊ฐ€๋Šฅ - ๋ณด์•ˆ ๊ฐ•ํ™”)

# aws ec2 authorize-security-group-ingress --group-id $NEW_SG --protocol tcp --port 80 --cidr 0.0.0.0/0
# aws ec2 authorize-security-group-ingress --group-id $NEW_SG --protocol icmp --port -1 --cidr 0.0.0.0/0

๐Ÿ“™ ๋ณผ๋ฅจ ๋ฐ ์ธ์Šคํ„ด์Šค ๋งŒ๋“ค๊ธฐ

# vi mapping.json
[
    {
        "DeviceName": "/dev/xvda",
        "Ebs": {
            "VolumeSize": 8
        }
    },

    {
        "DeviceName": "/dev/xvdb",
        "Ebs": {
            "VolumeSize": 8
        }
    }
]

# vi my_script.txt
#!/bin/bash
yum install -y httpd
systemctl enable --now httpd
echo "<h1>Hello AWS CLI</h1>" > /var/www/html/index.html

# aws ec2 run-instances \
--image-id ami-0fd0765afb77bcca7 \
--count 1 \
--instance-type t2.micro \
--key-name new-key \
--security-group-ids $NEW_SG \
--subnet-id $NEW_SID1 \
--block-device-mappings file://mapping.json \
--user-data file://my_script.txt \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=NEW-WEB}]' 'ResourceType=volume,Tags=[{Key=Name,Value=NEW-ROOT}]'
!--๊ฒฐ๊ณผ๊ฐ’์˜ InstanceId"๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# NEW_IID=i-0f9227878ecf00546
# aws ec2 describe-instances --instance-id $NEW_IID | grep PublicIp
!--๊ฒฐ๊ณผ๊ฐ’์˜ IP๋ณต์‚ฌํ•ด์„œ ์•„๋ž˜์— ๋„ฃ๊ธฐ (๋ณ€์ˆ˜ ์ƒ์„ฑ)--!
# ssh -i "new-key.pem" ec2-user@3.34.191.74
# curl 3.34.191.74

โœ”๏ธ ์ถ”๊ฐ€ํ•œ ๋ธ”๋กํ† ๋ฆฌ์ง€ ์—ฐ๊ฒฐํ•˜๊ธฐ - ํฌ๋งท

โœ”๏ธ ์ถ”๊ฐ€ํ•œ ๋ธ”๋กํ† ๋ฆฌ์ง€ ์—ฐ๊ฒฐํ•˜๊ธฐ - ๋งˆ์šดํŠธ

๐Ÿ“Œ CLIํ™˜๊ฒฝ์—์„œ ์ž์›๋“ค์„ ์ •๋ฆฌํ•˜๊ธฐ

๐Ÿ“™ ์ •๋ฆฌํ•˜๊ธฐ(์ข…๋ฃŒ)

# aws ec2 terminate-instances --instance-id $NEW_IID
# aws ec2 delete-security-group --group-id $NEW_SG
# aws ec2 delete-subnet --subnet-id $NEW_SID1
# aws ec2 delete-subnet --subnet-id $NEW_SID2
# aws ec2 delete-subnet --subnet-id $NEW_SID3
# aws ec2 delete-subnet --subnet-id $NEW_SID4
# aws ec2 detach-internet-gateway --internet-gateway-id $NEW_IGW --vpc-id $NEW_VPC
# aws ec2 delete-internet-gateway --internet-gateway-id $NEW_IGW
# aws ec2 delete-vpc --vpc-id $NEW_VPC

๐Ÿ“Œ ๊ธฐํƒ€

โญ๏ธ DevOps ์นดํ…Œ๊ณ ๋ฆฌ

โœ”๏ธ 1. IaC (Infrastructure as Code ) - ๊ตฌ์„ฑ ๋ฐ ์„ค์ • ์ž๋™ํ™” - Terraform, Ansible
โœ”๏ธ 2. Container ๊ธฐ์ˆ  ๋ฐ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜ - Docker, Kubernetes
โœ”๏ธ 3. CI/CD (Continuous Integeration/ Continuous Deployment(Delivery))- Jenkins,Git, Gitlab

โญ๏ธ ์‹œ๊ฐ„ํ™•์ธ ๋ช…๋ น์–ด

[root@localhost ~]# date
Wed Jun 29 11:07:17 KST 2022
[root@localhost ~]# timedatectl
      Local time: Wed 2022-06-29 11:07:23 KST
  Universal time: Wed 2022-06-29 02:07:23 UTC
        RTC time: Wed 2022-06-29 02:07:18
       Time zone: Asia/Seoul (KST, +0900)
     NTP enabled: n/a
NTP synchronized: no
 RTC in local TZ: no
 
!--ํƒ€์ž„์กด ์„ค์ • ๋ช…๋ น์–ด --!
 # timedatectl set-timezone Asia/Seoul 

โญ๏ธ CLIํ™˜๊ฒฝ ์ฐธ๊ณ  ๋งํฌ

https://docs.aws.amazon.com/cli/latest/reference/

โญ๏ธ AMI ID์ฐพ๊ธฐ

โญ๏ธ ๊ธฐ๋ณธ ๋ฆฌ์ „, output ํผ ๋ฐ”๊พธ๊ธฐ

[root@localhost ~]# ls .aws
config  credentials
[root@localhost ~]# cat .aws/config
[default]
region = ap-northeast-2
output = json
[root@localhost ~]# vi .aws/config
[root@localhost ~]# cat .aws/config
[default]
region = ap-northeast-1
output = table

โญ๏ธ .aws์˜ ํด๋”์˜ credentials ํŒŒ์ผ

[root@localhost ~]# cat .aws/credentials

์•ˆ์— key id์™€ key ์žˆ์Œ. ๋ณด์•ˆ ์ƒ๊ฐํ•˜๋ฉด ์ข…๋ฃŒํ•  ๋•Œ ์ด ํŒŒ์ผ ์ง€์šฐ๋Š” ๊ฒƒ์ด ์ข‹์Œ.

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€