AWS CLI

0. Reference

AWS CLI Command Reference v2

https://awscli.amazonaws.com/v2/documentation/api/latest/index.html

Installing or updating the latest version of the AWS CLI

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html


1. Installation

1.1. Download

cd /tmp && mkdir awscli && cd awscli

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv3.zip"

unzip awscliv3.zip

1.2. New install

sudo ./aws/install

1.3. Update existing install

sudo ./aws/install --update

1.4. Verify installed version

aws --version

2. Configuration

~/.aws/config

[default]

region = eu-west-1

output = json


[profile myp1]

region = eu-west-1

output = json


~/.aws/credentials

[default]

aws_access_key_id = AKIAWKIV2VD2NS65NS27

aws_secret_access_key = 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


[myp1]

aws_access_key_id = AKIAXKMPJSDELFEYRT8L

aws_secret_access_key = ktvhg42zN8qnBaGF/Ops4A28LFtoJ7mmJGBHwqTI


3. Login

Sample:

aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 274374682878.dkr.ecr.eu-west-1.amazonaws.com


4. Force new deployment (ECS task definition)

Sample:

aws ecs update-service --cluster ecs-cluster-myapp-test --service myapp_back-service-test  --task-definition myapp_back-test --force-new-deployment > /dev/null


5. Update code (Lambda function)

Sample:

# CONFIG

tech_comp=myapp_calendar

lambda_name=calendar-myapp-dev


#

aws lambda update-function-code \

    --function-name  $lambda_name \

    --zip-file fileb://$tech_comp.zip > /dev/null


6. S3

5.1. Remove directory content (S3)

aws s3 rm s3://$(s3_bucket_name)/$(dir_name) --recursive

5.2. Upload directory content (S3)

aws s3 cp ./dist s3://$(s3_bucket_name) --recursive

Note: If needed, flag: --acl public-read


7. CloudFront cache invalidation

aws cloudfront create-invalidation --distribution-id $(cf_dist) --paths \

"/$(tech_comp)/en/index.html" \

"/$(tech_comp)/es/index.html" \

"/$(tech_comp)/pl/index.html" \

> /dev/null


8. SSM

8.1.  Create a parameter

aws ssm put-parameter \

    --name parameter-name \

    --value "parameter-value" \

    --type "String" \

    --description "Only read, not created nor modified, by the Serverless framework" \

    --tags '[{"Key":"env","Value":"PRE"},{"Key":"ci", "Value":"sixqueue"}]'

8.2.  Retrieve parameters for given path

Note: --profile allows to provide the 'config' and 'credentials' profile to be used

aws ssm get-parameters-by-path --profile myp1 --path /dev/myapp