<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Libert S</title>
    <description>The latest articles on DEV Community by Libert S (@libertxyz).</description>
    <link>https://dev.to/libertxyz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33619%2Fce207c18-0f46-4832-b567-d1b6fb99c2d9.jpg</url>
      <title>DEV Community: Libert S</title>
      <link>https://dev.to/libertxyz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/libertxyz"/>
    <language>en</language>
    <item>
      <title>Yubikey meets an EC2 instance</title>
      <dc:creator>Libert S</dc:creator>
      <pubDate>Fri, 01 Apr 2022 19:27:31 +0000</pubDate>
      <link>https://dev.to/libertxyz/yubikey-meets-an-ec2-instance-5c3c</link>
      <guid>https://dev.to/libertxyz/yubikey-meets-an-ec2-instance-5c3c</guid>
      <description>&lt;p&gt;I recently got a Yubikey to lock some of my most critical accounts. In the process, I discovered that the yubikey also supports OpenPGP with the capabilities of Sign, Encrypt and Authenticate!. &lt;/p&gt;

&lt;p&gt;This is a game-changer in terms of security since I no longer need to have my private RSA keys in my computer (hot environment because it touches the internet).&lt;/p&gt;

&lt;p&gt;The process of how to generate the OpenPGP keys and burn them in the yubikey is well explained here: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtube.com/playlist?list=PLmoQ11MXEmahVl_uJVH0-a3XJtMV59PBu"&gt;https://youtube.com/playlist?list=PLmoQ11MXEmahVl_uJVH0-a3XJtMV59PBu&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The EC2 way
&lt;/h3&gt;

&lt;p&gt;When we launch a new EC2 instance we have to choose a pem file (private key) to SSH.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; keypair.pem ubuntu@ec2-&lt;span class="k"&gt;*********&lt;/span&gt;.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I used to do is to load the pem key into my ssh-agent to ssh to the instance without providing the key as a parameter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-add keypair.pem
ssh ubuntu@ec2-&lt;span class="k"&gt;*********&lt;/span&gt;.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  EC2 + Yubikey
&lt;/h3&gt;

&lt;p&gt;Assuming that your private key is already in the yubikey all you have to do is to plug the device into your computer and update the &lt;code&gt;ssh-agent&lt;/code&gt; socket to communicate with pgp agent socket. &lt;/p&gt;

&lt;p&gt;After doing that your &lt;code&gt;ssh-agent&lt;/code&gt; will use OpenPGP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SSH_AUTH_SOCK&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;gpgconf &lt;span class="nt"&gt;--list-dirs&lt;/span&gt; agent-ssh-socket&lt;span class="si"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Copy your public pgp key to the EC2 instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh-add 
ssh-copy-id ubuntu@ec2-&lt;span class="k"&gt;*********&lt;/span&gt;.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything is in place to SSH into your ec2 instance with your yubikey.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh ubuntu@ec2-&lt;span class="k"&gt;*********&lt;/span&gt;.com

&lt;span class="k"&gt;**&lt;/span&gt;waits &lt;span class="k"&gt;for &lt;/span&gt;confirmation &lt;span class="k"&gt;in &lt;/span&gt;the yubikey&lt;span class="k"&gt;**&lt;/span&gt;

ubuntu@ec2-local:/home/ubuntu 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;And you are connected!.&lt;br&gt;
Using this method removes the need to use the pem file provided by AWS. You can also use the same method for other servers Just copy your public key and you yubikey is your Authenticator. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>yubikey</category>
      <category>opengpg</category>
      <category>ec2</category>
    </item>
    <item>
      <title>AWS Solutions Architect Professional tips and tricks</title>
      <dc:creator>Libert S</dc:creator>
      <pubDate>Tue, 27 Apr 2021 17:43:41 +0000</pubDate>
      <link>https://dev.to/libertxyz/aws-solutions-architect-professional-tips-and-tricks-2obc</link>
      <guid>https://dev.to/libertxyz/aws-solutions-architect-professional-tips-and-tricks-2obc</guid>
      <description>&lt;p&gt;Is the time of the year to renew my AWS Solutions Architect certification as they are only valid for three years. &lt;/p&gt;

&lt;h3&gt;
  
  
  hands-on experience
&lt;/h3&gt;

&lt;p&gt;The AWS SA Professional exam (SAP-C01) is considered one of the most difficult exams from the AWS certification program, mostly because it covers multiple AWS services, all the way from IAM to AWS direct connect. Also, the questions are based on real case scenarios compared with most theoretical questions in the AWS Solutions Architect &lt;strong&gt;Associate&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;In the following weeks, I'll add a series of posts about my learning path and share some useful tips and tricks along the way to get certified. &lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloud</category>
    </item>
    <item>
      <title> Ansible EC2 dynamic inventories</title>
      <dc:creator>Libert S</dc:creator>
      <pubDate>Mon, 15 Mar 2021 22:56:12 +0000</pubDate>
      <link>https://dev.to/libertxyz/ansible-ec2-dynamic-inventories-4801</link>
      <guid>https://dev.to/libertxyz/ansible-ec2-dynamic-inventories-4801</guid>
      <description>&lt;p&gt;One of the steps of creating and testing ansible roles is verifying that the role does what it was intended to do in real EC2 instances. &lt;/p&gt;

&lt;p&gt;To automate this workflow I usually launch 5 instances (&lt;code&gt;centOS 7&lt;/code&gt;, &lt;code&gt;centOS 8&lt;/code&gt;, &lt;code&gt;Ubuntu 18&lt;/code&gt;, and &lt;code&gt;Ubuntu 20&lt;/code&gt;) using python and boto3. &lt;/p&gt;

&lt;h3&gt;
  
  
  LAUNCH EC2
&lt;/h3&gt;

&lt;p&gt;Here is a snippet of my script, you can find the complete script &lt;a href="https://gist.github.com/libert-xyz/e7e60c04f695ad9ca77de5ff61359bc2"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The script adds the tags &lt;code&gt;env: ansible&lt;/code&gt; and &lt;code&gt;distro: AMI['distro']&lt;/code&gt; to the instances. This comes handy for grouping the ansible dynamic inventories&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    instances = ec2.create_instances(
        ImageId = AMIS[i]["ami"],
        MinCount = 1,
        MaxCount = 1,
        InstanceType = 't3.micro',
        KeyName = KEY,
        SecurityGroupIds=[SG],
        SubnetId=SUBNET,
        TagSpecifications=[
            {
                'ResourceType': 'instance',
                'Tags': [
                    {
                        'Key': 'Name',
                        'Value': AMIS[i]["os"]
                    },
                    {
                        'Key' : 'env',
                        'Value': 'ansible'
                    },
                                        {
                        'Key' : 'distro',
                        'Value': AMIS[i]["distro"]
                    }
                ]
            }
        ]
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  DYNAMIC INVENTORY
&lt;/h3&gt;

&lt;p&gt;Once the instances are running instead of updating the &lt;code&gt;inventory&lt;/code&gt; file manually we can use the ansible &lt;code&gt;plugin: aws_ec2&lt;/code&gt; to make our inventory dynamic.&lt;/p&gt;

&lt;p&gt;Here you can see the inventory file that groups our EC2 instances by their tags, in this case, &lt;code&gt;distro&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The name of the file is important and needs to be &lt;code&gt;inventory_aws_ec2.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugin: aws_ec2
boto_profile: default
regions:
  - us-east-1
filters:
  tag:env:
    - ansible
keyed_groups:
  - key: tags.distro
    separator: ''

hostnames:
  - network-interface.association.public-ip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  OUTPUT
&lt;/h3&gt;

&lt;p&gt;Notice that the output groups the distribution name with the public IP address assigned to the EC2 instance.&lt;/p&gt;

&lt;p&gt;You can use this command to list the dynamic inventory:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-inventory -i inventory_aws_ec2.yml --list&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output is going to look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "aws_ec2": {
        "hosts": [
            "18.232.x.x",
            "34.228.x.x",
            "34.230.x.x",
            "54.226.x.x",
        ]
    },
    "centos": {
        "hosts": [
            "18.232.x.x",
            "34.228.x.x"
        ]
    },
    "ubuntu": {
        "hosts": [
            "54.226.x.x",
            "54.234.x.x"
        ]
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  RUN THE PLAYBOOK
&lt;/h3&gt;

&lt;p&gt;And finally, you can run your playbook using the dynamic inventory:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ansible-playbook -u ubuntu play.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--------
- hosts: ubuntu
  become: true
  roles:
    - ansible-role-update

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to terminate your instances after testing.&lt;/p&gt;

</description>
      <category>ansible</category>
      <category>aws</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
