{"id":1167,"date":"2022-08-31T14:21:25","date_gmt":"2022-08-31T14:21:25","guid":{"rendered":"https:\/\/www.geekmungus.co.uk\/?p=1167"},"modified":"2022-11-05T10:53:18","modified_gmt":"2022-11-05T10:53:18","slug":"aws-cli-quick-start-linux","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=1167","title":{"rendered":"AWS CLI Quick Start (Linux)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A quick guide to getting started with AWS CLI from your Linux machine. Amazon provide some good documentation (see links below), but I&#8217;ve summerised the key steps here for brevity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install or Update the AWS CLI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Firstly install the AWS CLI package with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl \"https:\/\/awscli.amazonaws.com\/awscli-exe-linux-x86_64.zip\" -o \"awscliv2.zip\"\nunzip awscliv2.zip\nsudo .\/aws\/install<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once installed, we now need to get the AWS CLI configured.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Configuration of AWS CLI<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can create the credentials file (which will be stored in <strong>~\/.aws\/credentials<\/strong>), just make sure you&#8217;re running as your user (and not root for example) when you perform this, or the <strong>credentials <\/strong>file is created in the wrong location.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In my case I&#8217;m using an Federated Access IAM account, i.e. i&#8217;m logging in with our IDaaS solution and assuming a role to perform these tasks. Via the AWS web interface I&#8217;ve gone in and got hold of the Access Key ID and Secret Access Key. You should change the region name to whatever you are using.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get these keys you need to use the &#8220;Command line or programmatic access&#8221; link against the particular role of a particular account you are using. If you have a specific IAM account you&#8217;ll find this information within the AWS IAM application (I think).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ aws configure\nAWS Access Key ID &#91;None]: BLAHBLAHBLAH7EXAMPLE\nAWS Secret Access Key &#91;None]: \/K7MDENwJalrXUtnFRfiCYEXAMPLEKEY\nDefault region name &#91;None]: eu-west-2\nDefault output format &#91;None]: json<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should now find the <strong>~\/.aws\/credentials<\/strong> file has been created with a &#8220;default&#8221; profile, the default profile is used when you run the <strong>aws <\/strong>command and don&#8217;t specify any particular named profile.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What about Federated IAM, i.e. an AWS SSO User?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So in my environment we have SSO Users (federated IAM), what this means is that if you attempt the above by finding the Access Key ID and Secret Access Key, you&#8217;ll have issue like the below when trying to access your EC2 instances:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ aws ec2 describe-instances\n\nAn error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What it appears to be missing is the <strong>aws_session_token<\/strong>, so if you edit the <strong>~\/.aws\/credentials<\/strong> file and add to the bottom the <strong>aws_session_token<\/strong> that you can get from the AWS <em>&#8220;Command line or programmatic access&#8221;<\/em> link along side where you got the Access Key ID and Secret Access Key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can obtain the Session Token and paste it into your credentials file such as the below, but this isn&#8217;t a long term solution, so see the section &#8220;Using a Federated IAM (SSO User)&#8221;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;default]\naws_access_key_id = &lt;accesskeyID&gt;\naws_secret_access_key = &lt;secretaccesskey&gt;\naws_session_token = &lt;sessiontokenkey&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using a Federated IAM (SSO User)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So a quick way to get started with this is to create the ~\/.aws\/config file and add the following configuration to the file. You&#8217;ll need to swap the various entries for your environment. Don&#8217;t include the pointy brackets these are just for clarity of the variables you need to substitute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Otherwise you can use the following command that will take you through:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws configure sso &#91;--profile myproject]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">MYPROFILE &#8211; An identifier you wish to use to identify this configuration when you&#8217;re either using it in environment variables (see later in the article) or including it with the &#8211;profile argument when running commands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NAME &#8211; The part of the SSO Start URL you may need to substitute, your URL may be completely different, either way this is the SSO URL that you&#8217;ll be redirected to as you attempt to authenticate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">REGIONNAME &#8211; e.g. &#8220;eu-west-2&#8221; You can have a different SSO Region to the Region where your resources are located.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ACCOUNTID &#8211; Your Account ID that you can find from your AWS account you wish to login to.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">AWSROLE &#8211; The name of the AWS Role you wish to utilise during your logged in session:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;profile &lt;MYPROFILE&gt;]\nsso_start_url = https:\/\/&lt;NAME&gt;.awsapps.com\/start\nsso_region = eu-west-2\nsso_account_id = 409901950218\nsso_role_name = AWSAdministratorAccess\nregion = eu-west-2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So with some example values we would have something like the following in the file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;profile londonaws]\nsso_start_url = https:\/\/turnip.awsapps.com\/start\nsso_region = eu-west-2\nsso_account_id = 450218014560\nsso_role_name = AWSAdministratorAccess\nregion = eu-west-2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now that is all in order you can attempt to logon with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws sso login --profile londonaws<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All being well your web browser will open and take you to the AWS SSO URL, you authenticate yourself as you normally would, then if it works, you&#8217;ll see your browser show a message saying authenticated and your command window will then show success and return you to the command prompt.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;re then logged in as your SSO User (Federated IAM).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of course it can be a pain to have to enter the &#8220;profile&#8221; you wish to use on the end of each command, you have a couple of option here: <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. instead of calling the profile a specific name add it into the &#8220;default&#8221; profile, this means any command run without specifying the profile argument will use this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Or the recommended approach is to add an environment variable that specifies the profile you wish to use, so you don&#8217;t have to specify it on the end of each command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export AWS_PROFILE=londonaws<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using this method can be helpful when running scripts, although you can specify such variables within the script if you so wish.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing it Out!<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Irrespective of if you have used the Federated IAM or just plain AWS authentication, let&#8217;s list the EC2 instances:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aws ec2 describe-instances<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now we find its working fine:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ aws ec2 describe-instances\n{\n    \"Reservations\": &#91;\n        {\n            \"Groups\": &#91;],\n            \"Instances\": &#91;\n                {\n                    \"AmiLaunchIndex\": 0,\n                    \"ImageId\": \"ami-00785cf48356acf64\",\n                    \"InstanceId\": \"i-08b13909e5343c167\",\n\n.....<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Useful Links<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/getting-started-install.html\">https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/getting-started-install.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/cli-configure-sso.html\">https:\/\/docs.aws.amazon.com\/cli\/latest\/userguide\/cli-configure-sso.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/unsplash.com\/@pericakalimerica\" data-type=\"URL\" data-id=\"https:\/\/unsplash.com\/@pericakalimerica\" target=\"_blank\" rel=\"noreferrer noopener\">Image Attribution<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick guide to getting started with AWS CLI from your Linux machine. Amazon provide some good documentation (see links below), but I&#8217;ve summerised the key steps here for brevity. Install or Update the AWS CLI Firstly install the AWS CLI package with: Once installed, we now need to get the AWS CLI configured. Quick &#8230; <a title=\"AWS CLI Quick Start (Linux)\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=1167\" aria-label=\"Read more about AWS CLI Quick Start (Linux)\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":1171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,3],"tags":[],"class_list":["post-1167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aws","category-cloud"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1167"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1167\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}