diff --git a/content/AWS/05_intro_to_cli.ipynb b/content/AWS/05_intro_to_cli.ipynb index 089c3b2..025b5d3 100644 --- a/content/AWS/05_intro_to_cli.ipynb +++ b/content/AWS/05_intro_to_cli.ipynb @@ -49,12 +49,12 @@ "source": [ "## Using the CLI\n", "\n", - "To check which file directory you are working in" + "Change to the home directory and check which file directory you are working in" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "id": "55024290-8843-4ae2-a81f-9ba6c5bdec13", "metadata": {}, "outputs": [ @@ -62,53 +62,330 @@ "name": "stdout", "output_type": "stream", "text": [ - "/home/ec2-user\n" + "/home/cloudshell-user\n" ] } ], "source": [ + "cd ~\n", "pwd" ] }, { "cell_type": "markdown", - "id": "47bdf3d8-3772-4f95-a119-f1f5eba717a5", + "id": "33d9fba8-c7d1-4266-9714-9093b04fcf8b", "metadata": {}, "source": [ - "To use the AWS command to list your s3 buckets:\n", - "\n", - "```bash\n", - "aws s3 ls\n", - "\n", - "```\n", - "\n", - "Let's list the contents of your s3 bucket:\n", - "\n", - "```bash\n", - "aws s3 ls s3://bucketname\n", - "\n", - "```\n", - "\n", - "Looks like there is nothing there right now. So let us create a text file called `hemingway.txt` and populate it with a nonsensical quote. \n", - "\n", - "```bash\n", - "echo \"The world breaks everyone\" > hemingway.txt\n", - "```\n", - "\n", - "Now let's move the file into our s3 bucket. \n", - "\n", - "```bash\n", - "aws s3 mv ./hemingway.txt s3://bucketname\n", + "Check what is there (probably nothing)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "17edba1b-49e2-4378-9a85-ac9326c0f056", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hemingway.txt\n" + ] + } + ], + "source": [ + "ls" + ] + }, + { + "cell_type": "markdown", + "id": "57f2bf71-60fe-4635-bb8b-df1e46347f0d", + "metadata": {}, + "source": [ + "Set the time and date to your local time" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "fcded343-770b-49e7-851c-825b66ff2ee1", + "metadata": {}, + "outputs": [], + "source": [ + "export TZ=US/Central" + ] + }, + { + "cell_type": "markdown", + "id": "627831ea-7c22-4073-af33-bbc508bd03b4", + "metadata": {}, + "source": [ + "To use the AWS command to list your s3 buckets:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a1e47882-e2a7-4dd8-beb4-1a5fcde2ff52", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2022-02-02 17:53:39 essentials-test-learner-2022-02-02\n" + ] + } + ], + "source": [ + "aws s3 ls" + ] + }, + { + "cell_type": "markdown", + "id": "d3798517-272c-4710-817a-3b8ea9ff3443", + "metadata": {}, + "source": [ + "## Using S3 with the CLI\n", "\n", - "```" + "Use a variable for the bucket name and display it. Be sure to replace `learner` with your user username (for example `user1766969`)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2a3fb47f-3032-4b1a-92e1-885b3b3daefb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "essentials-test-learner-2022-02-02\n" + ] + } + ], + "source": [ + "BUCKET=essentials-test-learner-$(date +%F)\n", + "echo $BUCKET" + ] + }, + { + "cell_type": "markdown", + "id": "985dd2a1-5191-4166-8e8f-ae7f47d5a983", + "metadata": {}, + "source": [ + "Let's list the contents of your s3 bucket:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f0e7d0eb-af86-428b-8975-967099734116", + "metadata": {}, + "outputs": [], + "source": [ + "aws s3 ls s3://$BUCKET" ] }, { "cell_type": "markdown", - "id": "9ed2bb8f", + "id": "ae11d3f5-0c53-4e4c-a211-31ea00455e75", "metadata": {}, "source": [ - "![cli-commands-1](images/cli_commands_1.png)" + "Looks like there is nothing there right now. So let us create a text file called `hemingway.txt` and populate it with a nonsensical quote. " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "49cbd384-550f-4751-8a68-466d653c4ddf", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "echo \"The world breaks everyone\" > hemingway.txt" + ] + }, + { + "cell_type": "markdown", + "id": "52a1baf9-1161-4d9b-a6bc-16c970d6a7fa", + "metadata": {}, + "source": [ + "Check the file just to be sure" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "e9bd3f82-35ef-4676-8101-5e0b282104ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hemingway.txt\n" + ] + } + ], + "source": [ + "ls" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1ef90f2d-bf70-4012-ab9c-9ff67e72d98c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The world breaks everyone\n" + ] + } + ], + "source": [ + "cat hemingway.txt" + ] + }, + { + "cell_type": "markdown", + "id": "c41dae7d-d5f9-46e6-a5ac-52fe777a44e6", + "metadata": {}, + "source": [ + "Now let's move the file into our s3 bucket. " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "672312e5-c532-4ff9-8d29-2e4bf056e88e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "move: ./hemingway.txt to s3://essentials-test-learner-2022-02-02/hemingway.txt\n" + ] + } + ], + "source": [ + "aws s3 mv ./hemingway.txt s3://$BUCKET" + ] + }, + { + "cell_type": "markdown", + "id": "213da888-c131-4732-9739-b5a149e1b3b0", + "metadata": {}, + "source": [ + "Check the it was a move (not a copy) by checking the file is gone" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "4c66a71e-9e34-4178-9559-3af88cc73503", + "metadata": {}, + "outputs": [], + "source": [ + "ls" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3d8888ca-1ef3-4c82-8ac5-f21e556b089e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "download: s3://essentials-test-learner-2022-02-02/hemingway.txt to ./hemingway.txt\n" + ] + } + ], + "source": [ + "aws s3 cp s3://$BUCKET/hemingway.txt ." + ] + }, + { + "cell_type": "markdown", + "id": "c56d3469-1b92-4fd6-aaae-547bd9db2331", + "metadata": {}, + "source": [ + "Check that we got the file back" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9edabccc-1793-4225-9ef5-ae83f15a51c8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hemingway.txt\n" + ] + } + ], + "source": [ + "ls" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "9f110d51-24cb-48c0-9d4d-2b98b44cbf96", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The world breaks everyone\n" + ] + } + ], + "source": [ + "cat hemingway.txt" + ] + }, + { + "cell_type": "markdown", + "id": "84f4876d-0bb2-4987-9821-2a1d172f98af", + "metadata": {}, + "source": [ + "Cleanup after ourselves" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "704f3842-3f94-47f8-922d-ac9e69573460", + "metadata": {}, + "outputs": [], + "source": [ + "rm hemingway.txt" + ] + }, + { + "cell_type": "markdown", + "id": "78aa0823-bdb7-4edc-8296-6367e9452563", + "metadata": {}, + "source": [ + "```{admonition} Danger\n", + ":class: Danger\n", + "\n", + "The `aws s3 mv` command permanently deletes files. They cannot be recovered!\n", + "```" ] }, { @@ -119,9 +396,38 @@ "```{admonition} Exercise\n", ":class:\n", "\n", - "* How can you see the contents of your bucket through the CLI? \n", - "* How can you see the contents of your bucket through the AWS console? \n", - "```\n" + "* How could you copy the original file instead of move it? \n", + "* How can you see the contents of your bucket through the AWS console?\n", + "* How would you remove the object `hemingway.txt` from your bucket?\n", + "```" + ] + }, + { + "cell_type": "markdown", + "id": "8d9a34a4-2a3a-4351-b2ae-73b8a519f442", + "metadata": {}, + "source": [ + "## Cleanup\n", + "\n", + "Remove the object from the bucket" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "536e1b46-e1e1-4158-b323-4d4d8c8e83ff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "delete: s3://essentials-test-learner-2022-02-02/hemingway.txt\n" + ] + } + ], + "source": [ + "aws s3 rm s3://$BUCKET/hemingway.txt" ] } ],