AWS Certified SysOps Administrator Official Study Guide. Cole Stephen
Чтение книги онлайн.

Читать онлайн книгу AWS Certified SysOps Administrator Official Study Guide - Cole Stephen страница 13

СКАЧАТЬ help to the end of nearly every AWS CLI command to determine a list of available options. For example, executing aws help will return a list of all of the services available to use as options. Running aws s3 help will return a list of the valid parameters to pass as part of a command-line call to Amazon Simple Storage Service (Amazon S3).

Autocompletion

      Support for tab completion– the ability to start typing a command and have a list of valid options to complete your command appear when you press tab – is a feature built into the AWS CLI but not enabled by default. You can enable autocompletion for the bash shell (Linux or Mac) by typing complete – C aws_completer aws.

Source Code

      AWS makes the AWS CLI source code available within the terms of the Apache License, Version 2.0. If you remain within the license, you can review the code before using it or adapt it into a new tool for your own project. There is an active community involved with the source code in which you are encouraged to participate. Find the code and more information on the user community at https://github.com/aws/aws-cli.

      images AWS publishes code on GitHub, not only for the AWS CLI, but also for the AWS SDKs and many other tools. Doing so helps give customers access to the code behind these tools to help them incorporate the code into their projects and spend more time innovating rather than creating building blocks. Take a look at some of the tools available at https://github.com/aws/.

Working with Services

      Executing an AWS CLI command is as simple as typing aws and then a command string followed by a list of options.

      The format of your command will generally take the form of the following:

      aws service parameter1

      parameter2 … parameterN

      For example, aws ec2 describe-instances will return a list of your Amazon Elastic Compute Cloud (Amazon EC2) instances, along with their properties, running in your configured region. aws s3 ls s3://mycertification/ will return an object listing of an Amazon S3 bucket you own named mycertification.

Output Types

      In the Configuration section, we mentioned that you can represent the data retrieved using the AWS CLI in three output formats: “JSON,” “text,” or “table.” Each format can provide a number of benefits to the user depending on the use case in question.

      JSON is the default format, and it provides data in a form that is easily parsed and ingested by applications. This format is commonly used in other AWS Cloud services (for example, AWS CloudFormation), and it is a standard in which operations personnel should become well versed if they want to excel. Text output allows the operator to output data in a tab-delimited format that can be parsed by tools like grep and other text parsers. (If you happen to be a Linux systems administrator, you’re likely very familiar with this tool.) Table format is often more easily human readable than JSON or text.

Avoiding Unwieldy Lines

      As you gain more experience using the AWS CLI, you will find that your command lines can become increasingly difficult to manage effectively as your parameters become more complex. There are several strategies to deal with this problem.

      First, in Linux or Mac, you can use the backslash character to separate a command into several lines. For example, this command:

      aws rds download-db-log-file-portion – db-instance-identifier awstest1 – log-file-name "error/postgres.log"

      is equivalent to the following command, parsed with backslashes:

      aws rds \download-db-log-file-portion \-db-instance-identifier awstest1 \-log-file-name "error/postgres.log"

      Using backslashes makes the command more easily comprehensible to a human reader, thus assisting with troubleshooting when errors occur.

      Next, some AWS CLI commands take a JSON-formatted string as part of the input. For example, the aws ec2 create-security-group command has a parameter -cli-input-json that takes a JSON-formatted string as an input. As an alternative to entering the string via the command line, you can refer to a local file as follows:

      aws ec2 create-security-group – cli-input-json file://filename.json

      where filename.json is the file containing the JSON string.

      Additionally, you can store the JSON string as an object in Amazon S3 or another web-hosted location and access the file as a URL:

      aws ec2 create-security-group \-cli-input-json \https://s3.amazonaws.com/cheeeeessseeee/filename.json

      This gives you the ability to reuse more easily the JSON string that you’ve created for one environment in another.

Using query to Filter Results

      As you explore using the AWS CLI, you will find that there is a wealth of information about your AWS environment that can be retrieved using the tool. Command-line output is comprehensive. Running the command aws ec2 describe-instances returns dozens of values describing each instance running: InstanceId, PublicDnsName, PrivateDnsName, InstanceType, and much more. There are times when you don’t want to return all of those values, though. What do you do if you want to retrieve only a list of the Amazon Machine Image (AMI) IDs that your instances are running so that you can make sure that your fleet is running your preferred image?

      That’s where the -query option comes in. This option allows you to filter results so that only the output with the parameters you specify are returned. Query uses the JMESPath query language as its input for filtering to the results you specify.

      images You can find a tutorial for the JMESPath query language at http://jmespath.org/tutorial.html.

      Here are some examples of query in practical use cases. Perhaps you want to obtain the metadata for your Amazon Relational Database Service (Amazon RDS) instances, but only those that are running in the us-east-1e Availability Zone:

      aws rds describe-db-instances \ – query 'DBInstances[?AvailabilityZone==`us-east-1e`]' \ – output text

      Maybe you want a list of your AWS IoT things that are Intel Edison devices:

      aws iot list-things – query 'things[?thingTypeName==`IntelEdison`]' – output text

      Or maybe you’ve been tasked with identifying a list of the instances with their associated instance type that are running in your environment so that they can be targeted as candidates for upgrades to newer generation types:

      aws ec2 describe-instances \ – query 'Reservations[*].Instances[*].[InstanceId, LaunchTime, InstanceType]' \ – output text

      That last one is a bit different than what we’ve executed in the previous examples. Note that we are working our way down the JSON hierarchy. First we specify that everything under Reservations and then everything under Instances is in scope for our query (the * character works as our wildcard here). In the final set of brackets, we specify what specific fields at that level we want to return – InstanceId, LaunchTime, and InstanceType in this example, allowing us to see only which fields are useful to us for our task.

СКАЧАТЬ