aws-cli Zelfstudie
Aan de slag met aws-cli
Zoeken…
Opmerkingen
Beschrijving
De AWS Command Line Interface (CLI) is een uniform hulpmiddel om uw AWS-services te beheren. Met slechts één tool om te downloaden en te configureren, kunt u meerdere AWS-services bedienen vanaf de opdrachtregel en deze automatiseren via scripts.
De AWS CLI introduceert een nieuwe reeks eenvoudige bestandsopdrachten voor efficiënte bestandsoverdracht van en naar Amazon S3.
Ondersteunde services
Zie Beschikbare services in de AWS CLI Command Reference voor een lijst met beschikbare services die u kunt gebruiken met de AWS-opdrachtregelinterface.
AWS-opdrachtregelinterface op GitHub
U kunt de broncode voor de AWS CLI op GitHub bekijken en vork in het https://github.com/aws/aws-cli project.
versies
Installatie en configuratie
Er zijn een aantal verschillende manieren om de AWS CLI op uw machine te installeren, afhankelijk van het besturingssysteem en de omgeving die u gebruikt:
Op Microsoft Windows - gebruik het MSI-installatieprogramma. Gebruik onder Linux, OS X of Unix pip (een pakketbeheerder voor Python-software) of installeer handmatig met het gebundelde installatieprogramma.
Installeren met pip:
U moet python installeren (versie 2, 2.6.5 +, 3 of 3.3+). Neem contact op met
python --version
pip --help
Aangezien beide zijn geïnstalleerd, gebruikt u de volgende opdracht om de aws cli te installeren.
sudo pip install awscli
Installeren op Windows De AWS CLI wordt ondersteund op Microsoft Windows XP of hoger. Voor Windows-gebruikers biedt het MSI-installatiepakket een vertrouwde en gemakkelijke manier om de AWS CLI te installeren zonder andere vereisten te installeren. Windows-gebruikers moeten het MSI-installatieprogramma gebruiken, tenzij ze al pip gebruiken voor pakketbeheer.
Voer het gedownloade MSI-installatieprogramma uit. Volg de instructies die verschijnen.
De AWS CLI installeren met behulp van het gebundelde installatieprogramma
Vereisten:
- Linux, OS X of Unix
- Python 2 versie 2.6.5+ of Python 3 versie 3.3+
Download het AWS CLI Gebundelde installatieprogramma met behulp van wget of curl.
Pak het pakket uit.
Voer het uitvoerbare bestand uit.
Op Linux en OS X zijn hier de drie opdrachten die overeenkomen met elke stap:
$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
$ unzip awscli-bundle.zip
$ sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
Installeren met HomeBrew op OS X:
Een andere optie voor OS X
brew install awscli
Test de AWS CLI-installatie
Bevestig dat de CLI correct is geïnstalleerd door het Help-bestand te bekijken. Open een terminal, shell of opdrachtprompt, voer aws help in en druk op Enter:
$ aws help
De AWS CLI configureren
Nadat u de installatie hebt voltooid, moet u deze configureren. U hebt uw toegangssleutel en geheime sleutel nodig die u krijgt wanneer u uw account aanmaakt op aws. U kunt ook een standaard te specificeren naam (| table | json tekst) en een type standaard output.
$ aws configure
AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: us-west-2
Default output format [None]: ENTER
De CLI-tool bijwerken
Amazon brengt periodiek nieuwe versies van de AWS Tool uit. Als de tool is geïnstalleerd met behulp van de Python Pip-tool, controleert de volgende opdracht de externe repository op updates en past deze toe op uw lokale systeem.
$ pip install awscli --upgrade
Een nieuw profiel maken
Om een nieuw referentieprofiel met de naam myprofile
:
$ aws configure --profile myprofile AWS Access Key ID [None]: ACCESSKEY AWS Secret Access Key [None]: SECRETKEY Default region name [None]: REGIONNAME Default output format [None]: text | table | json
Voor de AWS-toegangssleutel-ID en geheim maakt u een IAM-gebruiker in de AWS-console en genereert u hiervoor sleutels.
Regio is de standaardregio voor opdrachten in de indeling eu-west-1
of us-east-1
.
Het standaarduitvoerformaat kan text
, table
of json
.
U kunt de profielnaam nu in andere opdrachten gebruiken met de optie --profile
, bijvoorbeeld:
$ aws ec2 describe-instances --profile myprofile
AWS-bibliotheken voor andere talen (bijv. aws-sdk
voor Ruby of boto3
voor Python) hebben opties om het profiel dat u met deze methode maakt ook te gebruiken. Het aanmaken van een nieuwe sessie in boto3
kan bijvoorbeeld op deze manier, boto3.Session(profile_name:'myprofile')
en het zal de inloggegevens gebruiken die u voor het profiel hebt gemaakt.
De details van uw aws-cli-configuratie zijn te vinden in ~/.aws/config
en ~/.aws/credentials
(op linux en mac-os). Deze details kunnen vanaf daar handmatig worden bewerkt.
Aws cli-opdrachten gebruiken
De syntaxis voor het gebruik van de aws cli is als volgt:
aws [options] <command> <subcommand> [parameters]
Enkele voorbeelden met de opdracht 'ec2' en de subopdracht 'descript-instances':
aws ec2 describe-instances
aws ec2 describe-instances --instance-ids <your-id>
Voorbeeld met een nep-ID:
aws ec2 describe-instances --instance-ids i-c71r246a
Lijst S3-emmers
aws s3 ls
Gebruik een benoemd profiel
aws --profile myprofile s3 ls
Maak een lijst van alle objecten in een bucket, inclusief objecten in mappen, met de grootte in een voor mensen leesbare indeling en een samenvatting van de eigenschappen van de buckets aan het einde -
aws s3 ls --recursive --summarize --human-readable s3://<bucket_name>/
AWS-completer voor Ubuntu met Bash
Het volgende hulpprogramma kan worden gebruikt voor het automatisch uitvoeren van opdrachten:
$ which aws_completer
/usr/bin/aws_completer
$ complete -C '/usr/bin/aws_completer' aws
Overweeg dit voor toekomstige shell-sessies toe te voegen aan uw ~ / .bashrc
$ echo "complete -C '/usr/bin/aws_completer' aws" >> ~/.bashrc
Typ het volgende om te controleren:
$ aws ec
Druk op de [TAB] -toets, deze zou automatisch 2 moeten toevoegen:
$ aws ec2
AWS CLI Cheat sheet - Lijst met alle CLI-opdrachten
Opstelling
Installeer AWS CLI
AWS CLI is een veelgebruikte CLI-tool voor het beheer van de AWS-bronnen. Met deze enkele tool kunnen we alle aws-bronnen beheren
sudo apt-get install -y python-dev python-pip sudo pip install awscli aws --version aws configure
Bash one-liners
cat <file> # output a file tee # split output into a file cut -f 2 # print the 2nd column, per line sed -n '5{p;q}' # print the 5th line in a file sed 1d # print all lines, except the first tail -n +2 # print all lines, starting on the 2nd head -n 5 # print the first 5 lines tail -n 5 # print the last 5 lines expand # convert tabs to 4 spaces unexpand -a # convert 4 spaces to tabs wc # word count tr ' ' \\t # translate / convert characters to other characters sort # sort data uniq # show only unique entries paste # combine rows of text, by line join # combine rows of text, by initial column value
Cloudtrail - loggen en controleren
http://docs.aws.amazon.com/cli/latest/reference/cloudtrail/ 5 routes in totaal, met ondersteuning voor machtigingen op bronniveau
# list all trails aws cloudtrail describe-trails # list all S3 buckets aws s3 ls # create a new trail aws cloudtrail create-subscription \ --name awslog \ --s3-new-bucket awslog2016 # list the names of all trails aws cloudtrail describe-trails --output text | cut -f 8 # get the status of a trail aws cloudtrail get-trail-status \ --name awslog # delete a trail aws cloudtrail delete-trail \ --name awslog # delete the S3 bucket of a trail aws s3 rb s3://awslog2016 --force # add tags to a trail, up to 10 tags aws cloudtrail add-tags \ --resource-id awslog \ --tags-list "Key=log-type,Value=all" # list the tags of a trail aws cloudtrail list-tags \ --resource-id-list # remove a tag from a trail aws cloudtrail remove-tags \ --resource-id awslog \ --tags-list "Key=log-type,Value=all"
IK BEN
gebruikers
https://blogs.aws.amazon.com/security/post/Tx15CIT22V4J8RP/How-to-rotate-access-keys-for-IAM-users http://docs.aws.amazon.com/IAM/latest/UserGuide /reference_iam-limits.html Limieten = 5000 gebruikers, 100 groepen, 250 rollen, 2 toegangstoetsen / gebruiker
http://docs.aws.amazon.com/cli/latest/reference/iam/index.html
# list all user's info aws iam list-users # list all user's usernames aws iam list-users --output text | cut -f 6 # list current user's info aws iam get-user # list current user's access keys aws iam list-access-keys # crate new user aws iam create-user \ --user-name aws-admin2 # create multiple new users, from a file allUsers=$(cat ./user-names.txt) for userName in $allUsers; do aws iam create-user \ --user-name $userName done # list all users aws iam list-users --no-paginate # get a specific user's info aws iam get-user \ --user-name aws-admin2 # delete one user aws iam delete-user \ --user-name aws-admin2 # delete all users # allUsers=$(aws iam list-users --output text | cut -f 6); allUsers=$(cat ./user-names.txt) for userName in $allUsers; do aws iam delete-user \ --user-name $userName done
Wachtwoord beleid
http://docs.aws.amazon.com/cli/latest/reference/iam/
# list policy # http://docs.aws.amazon.com/cli/latest/reference/iam/get-account-password-policy.html aws iam get-account-password-policy # set policy # http://docs.aws.amazon.com/cli/latest/reference/iam/update-account-password-policy.html aws iam update-account-password-policy \ --minimum-password-length 12 \ --require-symbols \ --require-numbers \ --require-uppercase-characters \ --require-lowercase-characters \ --allow-users-to-change-password # delete policy # http://docs.aws.amazon.com/cli/latest/reference/iam/delete-account-password-policy.html aws iam delete-account-password-policy
Toegangstoetsen
http://docs.aws.amazon.com/cli/latest/reference/iam/
# list all access keys aws iam list-access-keys # list access keys of a specific user aws iam list-access-keys \ --user-name aws-admin2 # create a new access key aws iam create-access-key \ --user-name aws-admin2 \ --output text | tee aws-admin2.txt # list last access time of an access key aws iam get-access-key-last-used \ --access-key-id AKIAINA6AJZY4EXAMPLE # deactivate an acccss key aws iam update-access-key \ --access-key-id AKIAI44QH8DHBEXAMPLE \ --status Inactive \ --user-name aws-admin2 # delete an access key aws iam delete-access-key \ --access-key-id AKIAI44QH8DHBEXAMPLE \ --user-name aws-admin2
Groepen, beleid, beheerd beleid
http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html http://docs.aws.amazon.com/cli/latest/reference/iam/
# list all groups aws iam list-groups # create a group aws iam create-group --group-name FullAdmins # delete a group aws iam delete-group \ --group-name FullAdmins # list all policies aws iam list-policies # get a specific policy aws iam get-policy \ --policy-arn <value> # list all users, groups, and roles, for a given policy aws iam list-entities-for-policy \ --policy-arn <value> # list policies, for a given group aws iam list-attached-group-policies \ --group-name FullAdmins # add a policy to a group aws iam attach-group-policy \ --group-name FullAdmins \ --policy-arn arn:aws:iam::aws:policy/AdministratorAccess # add a user to a group aws iam add-user-to-group \ --group-name FullAdmins \ --user-name aws-admin2 # list users, for a given group aws iam get-group \ --group-name FullAdmins # list groups, for a given user aws iam list-groups-for-user \ --user-name aws-admin2 # remove a user from a group aws iam remove-user-from-group \ --group-name FullAdmins \ --user-name aws-admin2 # remove a policy from a group aws iam detach-group-policy \ --group-name FullAdmins \ --policy-arn arn:aws:iam::aws:policy/AdministratorAccess # delete a group aws iam delete-group \ --group-name FullAdmins
EC2
sleutelparen
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html
# list all keypairs # http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-key-pairs.html aws ec2 describe-key-pairs # create a keypair # http://docs.aws.amazon.com/cli/latest/reference/ec2/create-key-pair.html aws ec2 create-key-pair \ --key-name <value> # create a new private / public keypair, using RSA 2048-bit ssh-keygen -t rsa -b 2048 # import an existing keypair # http://docs.aws.amazon.com/cli/latest/reference/ec2/import-key-pair.html aws ec2 import-key-pair \ --key-name keyname_test \ --public-key-material file:///home/apollo/id_rsa.pub # delete a keypair # http://docs.aws.amazon.com/cli/latest/reference/ec2/delete-key-pair.html aws ec2 delete-key-pair \ --key-name <value>
Beveiligingsgroepen
http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html
# list all security groups aws ec2 describe-security-groups # create a security group aws ec2 create-security-group \ --vpc-id vpc-1a2b3c4d \ --group-name web-access \ --description "web access" # list details about a securty group aws ec2 describe-security-groups \ --group-id sg-0000000 # open port 80, for everyone aws ec2 authorize-security-group-ingress \ --group-id sg-0000000 \ --protocol tcp \ --port 80 \ --cidr 0.0.0.0/24 # get my public ip my_ip=$(dig +short myip.opendns.com @resolver1.opendns.com); echo $my_ip # open port 22, just for my ip aws ec2 authorize-security-group-ingress \ --group-id sg-0000000 \ --protocol tcp \ --port 80 \ --cidr $my_ip/24 # remove a firewall rule from a group aws ec2 revoke-security-group-ingress \ --group-id sg-0000000 \ --protocol tcp \ --port 80 \ --cidr 0.0.0.0/24 # delete a security group aws ec2 delete-security-group \ --group-id sg-00000000
instanties
http://docs.aws.amazon.com/cli/latest/reference/ec2/index.html
# list all instances (running, and not running) # http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html aws ec2 describe-instances # create a new instance # http://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html aws ec2 run-instances \ --image-id ami-f0e7d19a \ --instance-type t2.micro \ --security-group-ids sg-00000000 \ --dry-run # stop an instance # http://docs.aws.amazon.com/cli/latest/reference/ec2/terminate-instances.html aws ec2 terminate-instances \ --instance-ids <instance_id> # list status of all instances # http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-status.html aws ec2 describe-instance-status # list status of a specific instance aws ec2 describe-instance-status \ --instance-ids <instance_id>
Tags
# list the tags of an instance # http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-tags.html aws ec2 describe-tags # add a tag to an instance # http://docs.aws.amazon.com/cli/latest/reference/ec2/create-tags.html aws ec2 create-tags \ --resources "ami-1a2b3c4d" \ --tags Key=name,Value=debian # delete a tag on an instance # http://docs.aws.amazon.com/cli/latest/reference/ec2/delete-tags.html aws ec2 delete-tags \ --resources "ami-1a2b3c4d" \ --tags Key=Name,Value=
CloudWatch
Logboekgroepen
http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html http://docs.aws.amazon.com/cli/latest/reference/logs/index.html#cli-aws-logs
een groep makenhttp://docs.aws.amazon.com/cli/latest/reference/logs/create-log-group.html
aws logs create-log-group \ --log-group-name "DefaultGroup"lijst alle loggroepen
http://docs.aws.amazon.com/cli/latest/reference/logs/describe-log-groups.html
aws logs describe-log-groups aws logs describe-log-groups \ --log-group-name-prefix "Default"verwijder een groep
http://docs.aws.amazon.com/cli/latest/reference/logs/delete-log-group.html
aws logs delete-log-group \ --log-group-name "DefaultGroup"
Logstromen
# Log group names can be between 1 and 512 characters long. Allowed # characters include a-z, A-Z, 0-9, '_' (underscore), '-' (hyphen), # '/' (forward slash), and '.' (period). # create a log stream # http://docs.aws.amazon.com/cli/latest/reference/logs/create-log-stream.html aws logs create-log-stream \ --log-group-name "DefaultGroup" \ --log-stream-name "syslog" # list details on a log stream # http://docs.aws.amazon.com/cli/latest/reference/logs/describe-log-streams.html aws logs describe-log-streams \ --log-group-name "syslog" aws logs describe-log-streams \ --log-stream-name-prefix "syslog" # delete a log stream # http://docs.aws.amazon.com/cli/latest/reference/logs/delete-log-stream.html aws logs delete-log-stream \ --log-group-name "DefaultGroup" \ --log-stream-name "Default Stream"