How to configure the AWS CLI

Problem

We want to be able to use AWS services with a command-line interface.

The AWS CLI

The AWS CLI is a tool that allows us to control our AWS resources from the command line.

To install the AWS CLI locally (this is only necessary for running the CLI locally, the EC2 instances already have the CLI installed by default), follow the instructions at https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-linux.html.

To configure the AWS CLI after it’s been installed (this needs to be done on the EC2 instances and also locally if you wish to run locally):

  • SSH into the EC2 instance
  • Run the following commands:
$ sudo su
$ aws configure
  • The aws configure command will ask for some values such as the access key id and secret access key.
  • In default output format, enter json.
  • Note we have run aws configure with root permissions. This is important for some use cases. For example, if you are going to use the AWS CLI to query AWS Secrets Manager from a CodeDeploy stop script, then it’s important that when you run the aws configure in the EC2 instance you do it with the root user, because that is the user with which the CodeDeploy agent runs the start and stop scripts. If you ran aws configure with the normal user ec2-user, then the stop script would not be able to use the AWS CLI.

Leave a comment