
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>EC2 on Bastab C</title>
    <link>https://bastabc.com/</link>
    <description>Recent content in EC2 on Bastab C</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 17 Mar 2025 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://bastabc.com/tags/ec2/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Connect to AWS SSO and SSH into EC2 Instance</title>
      <link>https://bastabc.com/posts/connect-to-aws-sso-ssh-into-ec2-instance/</link>
      <pubDate>Mon, 17 Mar 2025 00:00:00 +0000</pubDate>
      
      <guid>https://bastabc.com/posts/connect-to-aws-sso-ssh-into-ec2-instance/</guid>
      <description>&lt;p&gt;This how-to article explains steps required to configure AWS account with and without need of AWS SSO and then SSH into an EC2 instance. The steps are generic in nature and can be customized to apply to any environment or any project.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;AWS CLI v2&lt;/li&gt;
&lt;li&gt;AWS SSO access&lt;/li&gt;
&lt;li&gt;Terminal&lt;/li&gt;
&lt;li&gt;AWS IAM user credentials (Access Key ID and Secret Access Key)&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&#34;directory-structure&#34;&gt;Directory structure&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;~/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;├── .aws/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   ├── config
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;│   └── credentials
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;└── .ssh/
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    ├── ec2-key.pem
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    └── config
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id=&#34;setup-aws-config-without-sso&#34;&gt;Setup .aws config without SSO&lt;/h2&gt;
&lt;p&gt;Create .aws folder if it doesn’t exist.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;mkdir -p ~/.aws
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Create credentials file.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# ~/.aws/credentials
[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Create config file.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# ~/.aws/config
[default]
region = ap-southeast-2
output = json
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Test your config.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;aws sts get-caller-identity
&lt;/code&gt;&lt;/pre&gt;&lt;hr&gt;
&lt;h2 id=&#34;setup-aws-config-with-sso&#34;&gt;Setup .aws config with SSO&lt;/h2&gt;
&lt;p&gt;This is more applicable for enterprise setup rather than individual setup. Prerequisite is have a valid and working AWS SSO Start URL (e.g. &lt;a href=&#34;https://company.awsapps.com/start&#34;&gt;https://company.awsapps.com/start&lt;/a&gt;) which allows to complete authentication in the browser.
Add AWS config entry (~/.aws/config) in your OS user root location. Add section per required spec e.g. new profiles each for aws-dev, aws-prod.&lt;/p&gt;
&lt;p&gt;Sample .aws folder structure is below. Ignore &amp;lsquo;amplify&amp;rsquo; and &amp;lsquo;cli&amp;rsquo; folders to start with.
&lt;img loading=&#34;lazy&#34; src=&#34;sso.png&#34; alt=&#34;aws-folder&#34;  /&gt;
&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-ssh&#34; data-lang=&#34;ssh&#34;&gt;[profile aws-prod]
sso_session = aws-prod
sso_account_id = 123456789123
sso_role_name = aws-prod-SystemAdmin
region = ap-southeast-4
output = json
credential_process = aws configure export-credentials --profile aws-prod
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Connect by SSO by running following command.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-ssh&#34; data-lang=&#34;ssh&#34;&gt;aws sso login --profile aws-prod
&lt;/code&gt;&lt;/pre&gt;&lt;hr&gt;
&lt;h2 id=&#34;setup-ssh-config&#34;&gt;Setup .ssh config&lt;/h2&gt;
&lt;p&gt;The .pem files are keypairs created while launching ec2 instance ec2-prod.&lt;/p&gt;
&lt;p&gt;Create .ssh folder if it doesn’t exist.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;mkdir -p ~/.ssh
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Move downloaded .pem file to .ssh folder.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;mv ~/Downloads/ec2-prod.pem ~/.ssh/ec2-prod.pem
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Provide necessary permissions to key pair.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;chmod 400 ~/.ssh/ec2-prod.pem
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Add an entry to SSH config.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;nano ~/.ssh/config
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Add following to the conf file.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Host ec2-prod
    HostName 1.2.3.4             # Replace with your EC2&amp;#39;s public IP
    User ec2-user                # Use &amp;#39;ubuntu&amp;#39; for Ubuntu instances
    IdentityFile ~/.ssh/ec2-prod.pem
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Sample .ssh folder structure is below.
&lt;img loading=&#34;lazy&#34; src=&#34;ssh.png&#34; alt=&#34;ssh-folder&#34;  /&gt;
&lt;/p&gt;
&lt;p&gt;Alternatively, add SSH config entry (~/.ssh/.sshd_config.d/profile.conf) in your OS user root location. Add conf files per required profile e.g. new conf files each for aws-dev, aws-prod.&lt;/p&gt;
&lt;p&gt;Add following to aws-prod.conf file.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;Host ec2-prod
  User ec2-user
  IdentityFile ~/.ssh/ec2-prod.pem
  ProxyCommand aws ssm start-session --target i-01xyz7659824a123q --profile aws-prod --document-name AWS-StartSSHSession --parameters portNumber=22
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This creates separate config files for each profile. It&amp;rsquo;s more applicable if you&amp;rsquo;re using AWS profile with SSO.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id=&#34;connect-to-ec2-instance&#34;&gt;Connect to ec2 instance&lt;/h2&gt;
&lt;p&gt;SSH into ec2 instance.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-ssh&#34; data-lang=&#34;ssh&#34;&gt;ssh ec2-prod
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Sample output indicating you&amp;rsquo;re connected to the ec2 instance.
&lt;img loading=&#34;lazy&#34; src=&#34;ec2.png&#34; alt=&#34;ec2-connect&#34;  /&gt;
&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>