aws:aws-cli
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
aws:aws-cli [28/05/2025 09:52] – [Setting up profiles] andrew | aws:aws-cli [30/05/2025 09:36] (current) – removed andrew | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Amazon Web Services CLI ====== | ||
- | |||
- | |||
- | ===== Initial install ===== | ||
- | |||
- | AWS cli tool is written in python, and as python3 is the most recent, this is what will be installed. The awscli tool is installed through pip3. Consider installing this in a virtual environment ([[python: | ||
- | |||
- | < | ||
- | # yum install python3 | ||
- | |||
- | ... edited... | ||
- | |||
- | Install | ||
- | |||
- | Total download size: 11 M | ||
- | Installed size: 51 M | ||
- | Is this ok [y/d/N]: y | ||
- | Downloading packages: | ||
- | (1/4): python3-3.7.0-0.20.rc1.amzn2.0.1.x86_64.rpm | ||
- | (2/4): python3-pip-9.0.3-1.amzn2.0.1.no | ||
- | (3/4): python3-setuptools-38.4.0-3.amzn2.0.6.noarch.rpm | ||
- | (4/4): python3-libs-3.7.0-0.20.rc1.amzn2.0.1.x86_64.rpm | ||
- | ------------------------------------------------------------------------------------------------------------ | ||
- | Total 487 kB/s | 11 MB 00: | ||
- | |||
- | # pip3 install awscli | ||
- | WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. | ||
- | Collecting awscli | ||
- | Downloading https:// | ||
- | 100% |████████████████████████████████| 1.3MB 981kB/ | ||
- | ...edited... | ||
- | </ | ||
- | |||
- | |||
- | |||
- | AWS linux 2 does have a awscli tool in the linux repo, based on python2, but it is not as recent as the pip installed one, this is the python3 based version, check if you need py3 or py2 before installing: | ||
- | |||
- | < | ||
- | # aws --version | ||
- | aws-cli/ | ||
- | [root@amazonlinux02 ~]# | ||
- | </ | ||
- | |||
- | |||
- | Compare to the pip3 installed version:- | ||
- | < | ||
- | # / | ||
- | aws-cli/ | ||
- | [root@amazonlinux02 ~]# | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ===== Setting up profiles ===== | ||
- | |||
- | The '' | ||
- | |||
- | config | ||
- | < | ||
- | [default] | ||
- | output = text | ||
- | |||
- | |||
- | [profile admin1] | ||
- | role_arn = arn: | ||
- | source_profile = default | ||
- | region = eu-west-1 | ||
- | |||
- | |||
- | [profile profile2] | ||
- | region = eu-west-2 | ||
- | source_profile = default | ||
- | output = text | ||
- | </ | ||
- | |||
- | credentials | ||
- | < | ||
- | [default] | ||
- | aws_access_key_id = QWERTYUIOPASDFGHKEYQ | ||
- | aws_secret_access_key = HaMPb65IFf0bVoEiLSKEJtuCUo3490nWlrJBES9n | ||
- | |||
- | |||
- | [profile2] | ||
- | aws_access_key_id = QWERTYUIOPASDFGHKEYA | ||
- | aws_secret_access_key = wifisUMegS9pY_tpOnQpSY0YJYSiqgeKneMWqqIa | ||
- | </ | ||
- | |||
- | |||
- | FIXME | ||
- | |||
- | aws configure list | ||
- | |||
- | aws iam get-user | ||
- | |||
- | ==== get-caller-identity ==== | ||
- | |||
- | Returns details about the IAM user or role whose credentials are used to call the operation. No permissions are required to perform this operation. | ||
- | |||
- | This can be used to test AWS CLI setup and network connectivity. | ||
- | |||
- | < | ||
- | $ aws sts get-caller-identity | ||
- | { | ||
- | " | ||
- | " | ||
- | " | ||
- | } | ||
- | $ | ||
- | </ | ||
- | |||
- | |||
- | ===== Setting up roles ===== | ||
- | |||
- | Roles allow proviledge escalation for a user to perform specific tasks. For this to be used in the cli, an extra section is added to the '' | ||
- | |||
- | < | ||
- | [default] | ||
- | output = json | ||
- | region = eu-west-1 | ||
- | |||
- | |||
- | [profile sandbox] | ||
- | role_arn = arn: | ||
- | source_profile = default | ||
- | region = eu-west-1 | ||
- | </ | ||
- | |||
- | When a cli command is run, the '' | ||
- | |||
- | < | ||
- | server: | ||
- | [ | ||
- | " | ||
- | " | ||
- | ] | ||
- | server: | ||
- | </ | ||
- | |||
- | |||
- | ==== Errors ==== | ||
- | |||
- | < | ||
- | $ aws ec2 describe-instances --profile nonprod_admin | ||
- | |||
- | An error occurred (InvalidClientTokenId) when calling the AssumeRole operation: The security token included in the request is invalid. | ||
- | $ | ||
- | </ | ||
- | |||
- | This was solved by updating the '' | ||
- | As it said "The security token included in the request is invalid." | ||
- | ===== Using roles and profiles with Boto3 ===== | ||
- | |||
- | '' | ||
- | |||
- | A client needs to be set up, and for local cli usage, this needs to be linked with a profile as set above. | ||
- | |||
- | <code python> | ||
- | # | ||
- | |||
- | import boto3 | ||
- | |||
- | profile = ' | ||
- | |||
- | # Create ec2 client | ||
- | session = boto3.session.Session(profile_name=profile) | ||
- | ec2 = session.client(' | ||
- | |||
- | # Create SQS client | ||
- | session = boto3.session.Session(profile_name=profile) | ||
- | sqs = session.client(' | ||
- | |||
- | </ | ||
- | |||
- | This client (ec2, sqs etc) can be used to set or retreive information as the user in the profile:- | ||
- | |||
- | <code python> | ||
- | AMIResponse = ec2.describe_images(Filters=[{' | ||
- | </ | ||
- | |||
- | ===== AWS CodeCommit ===== | ||
- | |||
- | It's worth pointing out that Code Commit repos are tied to a particular AWS account, so if you operate in a multiple account environment, | ||
- | |||
- | See:- [[https:// | ||
- | |||
- | AWS CodeCommit is a git compatible repository. It uses the git command locally, but if you are using roles, there is a restriction on using only https, not ssh to communticate to the remote repo. Also, there is a tie in with the aws command line which is why CodeCommit is here and not under [[rb: | ||
- | |||
- | AWS's IAM requires '' | ||
- | Also, you will be **required** to configure a Credential Helper, the name of this sounds like it is optional, but it isn' | ||
- | |||
- | IAM periodically resets the password used with the git credentials (above) and the Credential Helper is used to call out to IAM to get the updates password which is then used in the git command. | ||
- | |||
- | < | ||
- | git config --global credential.helper '!aws codecommit credential-helper $@' | ||
- | git config --global credential.UseHttpPath true | ||
- | </ | ||
- | |||
- | Profiles can be defined per repository by using '' | ||
- | |||
- | |||
- | ==== Roles with CodeCommit ==== | ||
- | |||
- | In the .gitconfig file, the commands above add the helper line, but to use it with roles, it needs the '' | ||
- | |||
- | < | ||
- | $ more / | ||
- | [credential] | ||
- | helper = !aws --profile sandbox codecommit credential-helper $@ | ||
- | UseHttpPath = true | ||
- | $ | ||
- | </ | ||
- | |||
- | My understanding is that '' | ||
- | |||
- | |||
- | If you get an error similar to '' | ||
- | |||
- | $ export AWS_PROFILE=shared-services | ||
- | |||
- | |||
- | |||
- | ==== Creating a new repo ==== | ||
- | |||
- | This is shown with the role option '' | ||
- | |||
- | |||
- | < | ||
- | $ aws codecommit create-repository --repository-name CIS-Hardening --repository-description "Repo for ansible code to harden aws Linux2 image." | ||
- | { | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | } | ||
- | } | ||
- | $ | ||
- | $ aws codecommit list-repositories --profile sandbox | ||
- | { | ||
- | " | ||
- | { | ||
- | " | ||
- | " | ||
- | }, | ||
- | { | ||
- | " | ||
- | " | ||
- | } | ||
- | ] | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | See the [[rb: | ||
- | |||
- | |||
- | ==== 403 error with git push ==== | ||
- | |||
- | This is an error encountered on a Mac:- | ||
- | |||
- | < | ||
- | me (ajs/ | ||
- | fatal: unable to access ' | ||
- | me (ajs/ | ||
- | </ | ||
- | |||
- | It's caused (probably) by the " | ||
- | |||
- | Solution is to search for and delete CodeCommit credentials in Keychain Access. | ||
- | |||
- | < | ||
- | me (ajs/ | ||
- | Enumerating objects: 11, done. | ||
- | Counting objects: 100% (11/11), done. | ||
- | Delta compression using up to 4 threads | ||
- | Compressing objects: 100% (6/6), done. | ||
- | Writing objects: 100% (6/6), 988 bytes | 988.00 KiB/s, done. | ||
- | Total 6 (delta 4), reused 0 (delta 0) | ||
- | To https:// | ||
- | * [new branch] | ||
- | Branch ' | ||
- | me (ajs/ | ||
- | </ | ||
- | |||
- | |||
- | ==== Get repos ==== | ||
- | |||
- | Used '' | ||
- | |||
- | get_repos.sh: | ||
- | <code bash> | ||
- | #!/bin/bash | ||
- | # List repos from AWS codecommit | ||
- | |||
- | export AWS_DEFAULT_OUTPUT=" | ||
- | |||
- | aws codecommit list-repositories | jq -r ' | ||
- | </ | ||
- | |||
- | < | ||
- | credential-age-check | ||
- | pyside6-stuff | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | ===== Embedding a BASH variable in JSON ===== | ||
- | |||
- | <code bash> | ||
- | STANDARD=' | ||
- | |||
- | aws securityhub | ||
- | --standards-control-association-updates \ | ||
- | --profile OrgDeployRole \ | ||
- | '[ | ||
- | { | ||
- | " | ||
- | " | ||
- | " | ||
- | " | ||
- | } | ||
- | ]' | ||
- | |||
- | |||
- | </ | ||
- | |||
- | ==== Explanation of variable substitution==== | ||
- | |||
- | < | ||
- | " | ||
- | </ | ||
- | |||
- | JSON keys and values need to be quoted, so that's the outside double quotes, STANDARD cannot be expanded by the aws command so it is single quoted, but lastly as BASH sees it, it is double quoted so BASH can expand the variable '' | ||
- | |||
- | I think.... Not 100% sure on the single quotes usage | ||
- | |||
- | |||
- | |||
- | |||
- | |||
aws/aws-cli.1748425960.txt.gz · Last modified: by andrew