AWS Command Line Interface (AWS CLI)

repository·develop·Indexed 12 days ago

https://github.com/aws/aws-cli

A unified command line interface to interact with Amazon Web Services. This documentation covers the AWS CLI (v1) command structure, global options, and the underlying Botocore client implementation, including ClientCreator for service client instantiation, endpoint resolution via ClientEndpointBridge, paginators, waiters, and event-driven request lifecycles.

Tokens
366.1K
Snippets
1.4K
Records
1.5K
Agent score
96%

What's inside AWS CLI

  1. Configure IAM roles for Amazon EMR

    develop

    When creating a cluster, you must ensure the EMR service has the necessary permissions. You can either:

    1. Use the --use-default-roles flag to automatically use the default service role and instance profile.
    2. Explicitly specify the --service-role and the --ec2-attributes (specifically the InstanceProfile key).
    3. Use --instance-fleets or --instance-groups which may require specific role configurations.
    # Using explicit roles
    aws emr create-cluster \
        --release-label emr-5.14.0 \
        --service-role EMR_DefaultRole \
        --ec2-attributes InstanceProfile=EMR_EC2_DefaultRole \
        --instance-groups InstanceGroupType=MASTER,InstanceCount=1,InstanceType=m4.large InstanceGroupType=CORE,InstanceCount=2,InstanceType=m4.large
    
    # Using default roles
    aws emr create-cluster \
        --release-label emr-5.9.0 \
        --use-default-roles \
        --instance-groups InstanceGroupType=MASTER,InstanceCount=1,InstanceType=m4.large InstanceGroupType=CORE,InstanceCount=2,InstanceType=m4.large \
        --auto-terminate
  2. Understand AWS CLI configuration precedence

    develop

    When multiple configuration sources are provided, the AWS CLI follows a specific order of precedence. Higher priority sources override lower ones.

    General Configuration Precedence:

    1. Command line options (e.g., --region)
    2. Environment variables (e.g., AWS_DEFAULT_REGION)
    3. Configuration file (~/.aws/config)

    Credentials Precedence:

    1. Environment variables (e.g., AWS_ACCESS_KEY_ID)
    2. Shared credentials file (~/.aws/credentials)
    3. AWS CLI config file (~/.aws/config)

    Note: If AWS_PROFILE is set, environment variables like AWS_ACCESS_KEY_ID will override the credentials found within that specific profile.

  3. Configure attributes.json for instance type selection

    develop

    When customizing your attributes.json file for get-instance-types-from-instance-requirements, you must provide values for ArchitectureTypes, VirtualizationTypes, VCpuCount, and MemoryMiB. Other attributes within the InstanceRequirements object can be omitted, in which case the service uses default values.

    {
        "ArchitectureTypes": [
            "x86_64"
        ],
        "VirtualizationTypes": [
            "hvm"
        ],
        "InstanceRequirements": {
            "VCpuCount": {
                "Min": 4,
                "Max": 6
            },
            "MemoryMiB": {
                "Min": 2048
            },
            "InstanceGenerations": [
                "current"
            ]
        }
    }
  4. Manage AWS CLI profiles

    develop

    You can define multiple profiles in both the shared credentials file and the configuration file.

    • To use a specific profile: Use the --profile option in your command.
    • Default behavior: If no --profile is specified, the default profile is used.

    Config File Syntax for Profiles: In the ~/.aws/config file, you must prefix profile section headers with profile:

    [profile testing]
    aws_access_key_id=<testing access key>
    aws_secret_access_key=<testing secret key>
    region=us-west-2
    $ aws s3 ls --profile testing
  5. Understand the output of get-configuration

    develop

    The get-configuration command produces two distinct pieces of information:

    1. The Configuration Content: This is the actual data defined in your profile. In the example provided, it is saved to the file specified at the end of the command (e.g., configuration-output-file). Example content:

      { "Name": "ExampleApplication", "Id": ExampleID, "Rank": 7 }
    2. The Command Output: The CLI returns a JSON object containing metadata about the retrieved configuration, specifically the version and the content type. Example output:

      {
          "ConfigurationVersion": "1",
          "ContentType": "application/json"
      }
  6. How checksum validation works for S3 downloads

    develop

    The AWS CLI attempts to verify the integrity of downloaded objects when possible.

    If Amazon S3 returns a non-MD5 checksum with the downloaded object, the CLI will use that same algorithm to recalculate the checksum locally and verify it matches the checksum stored in S3.

    Important Behavior:

    • If checksum validation fails, the CLI raises an error.
    • If validation fails, the request will NOT be retried.
    • This applies to commands such as aws s3api get-object.
  7. Security considerations for aws cloudformation package

    develop

    The package command treats the provided CloudFormation template as trusted build input.

    Warning: Do not run aws cloudformation package against templates from untrusted sources. Because the command reads files from the local filesystem based on paths defined in the template (including relative paths that traverse above the template directory, e.g., CodeUri: ../src/my-function), a malicious template can be designed to read and upload sensitive files from your machine (such as credentials or private keys) to the configured S3 bucket.

  8. Understand the structure of Access Analyzer validation findings

    develop

    The output of validate-policy is a JSON object containing a findings array. Each finding includes:

    • findingDetails: A human-readable description of the issue.
    • findingType: The severity/nature of the finding (e.g., SUGGESTION, ERROR).
    • issueCode: A machine-readable code representing the specific check that failed (e.g., EMPTY_SID_VALUE, MISMATCHED_ACTION_FOR_PRINCIPAL, UNSUPPORTED_ACTION_FOR_CONDITION_KEY).
    • learnMoreLink: A URL to the AWS documentation for more details on the specific check.
    • locations: An array of objects describing the exact path and character span within the JSON document where the issue was detected.
  9. Determine if AWS CLI v1 requires a Python upgrade

    develop

    To check if your current AWS CLI v1 installation is running on an unsupported or deprecated version of Python, run the aws --version command.

    Examine the second portion of the output string (starting with Python/). If the version is lower than Python/3.10.x, you should upgrade to Python 3.10 or later to ensure continued support and security updates.

    Note: AWS CLI v2 is not affected by this transition because it bundles its own copy of Python 3.

    $ aws --version
    aws-cli/1.18.191 Python/2.7.18 Darwin/19.6.0 botocore/1.19.31
  10. Configure Amazon EBS volumes for an ECS Fargate task

    develop

    To run a Fargate task with an attached Amazon EBS volume, you must:

    1. Have an Amazon ECS infrastructure role configured with the AmazonECSInfrastructureRolePolicyForVolumes managed policy attached.
    2. Specify a task definition that contains a volume with the same name as the one provided in the run-task request.

    It is recommended to use the --cli-input-json option to pass the complex configuration required for volume settings.

    Example using ebs.json:

    aws ecs run-task --cli-input-json file://ebs.json

    Contents of ebs.json:

    {
       "cluster": "default",
       "taskDefinition": "mytaskdef",
       "launchType": "FARGATE",
       "networkConfiguration":{
            "awsvpcConfiguration":{
                "assignPublicIp": "ENABLED",
                "securityGroups": ["sg-12344321"],
                "subnets":["subnet-12344321"]
            }
        },
       "volumeConfigurations": [
            {
                "name": "myEBSVolume",
                "managedEBSVolume": {
                    "volumeType": "gp3",
                    "sizeInGiB": 100,
                    "roleArn":"arn:aws:iam::1111222333:role/ecsInfrastructureRole",
                    "encrypted": true,
                    "kmsKeyId": "arn:aws:kms:region:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
                }
            }
        ]
    }
  11. Query and format EC2 instance output with `--query`

    develop

    Use the --query parameter (JMESPath) and --output to extract specific fields from the describe-instances response and format them as json, text, or table.

    Extract specific fields as JSON

    To get a list of objects containing only the InstanceId and SubnetId:

    aws ec2 describe-instances \
        --query 'Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}' \
        --output json

    Extract specific fields as plain text

    To get a simple list of instance IDs:

    aws ec2 describe-instances \
        --filters "Name=instance-type,Values=t2.micro" \
        --query "Reservations[*].Instances[*].[InstanceId]" \
        --output text

    Create a formatted table with custom columns

    To display the Instance ID, Availability Zone, and the value of the Name tag in a table:

    Linux/macOS:

    aws ec2 describe-instances \
        --filters Name=tag-key,Values=Name \
        --query 'Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value}' \
        --output table

    Windows:

    aws ec2 describe-instances ^
        --filters Name=tag-key,Values=Name ^
        --query "Reservations[*].Instances[*].{Instance:InstanceId,AZ:Placement.AvailabilityZone,Name:Tags[?Key=='Name']|[0].Value}" ^
        --output table
  12. Install an on-premises AWS CodeDeploy Agent

    develop

    To install an on-premises instance of the AWS CodeDeploy Agent, use the aws deploy install command. This process copies a provided on-premises configuration file to the location expected by the agent and installs the agent itself.

    Important Limitations:

    • This command does not create IAM users.
    • This command does not register the on-premises instance with AWS CodeDeploy.
    • This command does not associate any on-premises instance tags in AWS CodeDeploy.

    Required flags:

    • --override-config: Indicates that a custom configuration file should be used.
    • --config-file: The local path to the codedeploy.onpremises.yml configuration file.
    • --region: The AWS region where the installation is being managed.
    • --agent-installer: The S3 URI for the agent installer (e.g., an .msi file for Windows).
    aws deploy install \
        --override-config \
        --config-file C:\temp\codedeploy.onpremises.yml \
        --region us-west-2 \
        --agent-installer s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.msi