<?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: Jake Howering</title>
    <description>The latest articles on DEV Community by Jake Howering (@jakehowering).</description>
    <link>https://dev.to/jakehowering</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%2F937551%2F694e8aec-9ac7-4956-9a1f-aeb3da563bb2.png</url>
      <title>DEV Community: Jake Howering</title>
      <link>https://dev.to/jakehowering</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jakehowering"/>
    <language>en</language>
    <item>
      <title>Use Terraform to update kubeconfig</title>
      <dc:creator>Jake Howering</dc:creator>
      <pubDate>Fri, 07 Oct 2022 15:17:22 +0000</pubDate>
      <link>https://dev.to/jakehowering/use-terraform-to-update-kubeconfig-3n2e</link>
      <guid>https://dev.to/jakehowering/use-terraform-to-update-kubeconfig-3n2e</guid>
      <description>&lt;p&gt;Once way to provision &lt;a href="https://aws.amazon.com/eks/"&gt;AWS EKS&lt;/a&gt; is by using Terraform and integrating EKS provisioning into your CI/CD build and pipeline workflows.&lt;/p&gt;

&lt;p&gt;When managing EKS, you may then want to use the &lt;a href="https://kubernetes.io/docs/reference/kubectl/"&gt;kubectl&lt;/a&gt; CLI....so you'll need to update your kubeconfig file.  &lt;/p&gt;

&lt;p&gt;Here's how to do it using Terraform&lt;/p&gt;

&lt;p&gt;1) In your Terraform output file, output 2 values to validate&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output "region" {
  description = "AWS region"
  value       = var.region
}

output "cluster_name" {
  description = "Kubernetes Cluster Name"
  value       = local.cluster_name
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two values are used in the kubeconfig file&lt;/p&gt;

&lt;p&gt;2) In your Terraform files, create a "null_resource" to run a  command on your computer that runs the Terraform files, in my case it's my Macbook Air.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "null_resource" "kubectl" {
    provisioner "local-exec" {
        command = "aws eks --region ${var.region} update-kubeconfig --name ${local.cluster_name}"
    }
}

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

&lt;/div&gt;



&lt;p&gt;That should be it - now when you run a kubectl command, you see see your AWS EKS objects.&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>security</category>
      <category>terraform</category>
      <category>kubernetes</category>
    </item>
    <item>
      <title>Simple Python Code to get all VM Names, IPs, and FQDN's in the AWS Organization</title>
      <dc:creator>Jake Howering</dc:creator>
      <pubDate>Thu, 06 Oct 2022 11:27:53 +0000</pubDate>
      <link>https://dev.to/jakehowering/simple-python-code-to-get-all-vm-names-ips-and-fqdns-in-the-aws-organization-1he7</link>
      <guid>https://dev.to/jakehowering/simple-python-code-to-get-all-vm-names-ips-and-fqdns-in-the-aws-organization-1he7</guid>
      <description>&lt;p&gt;Getting your AWS VM Name, IP address and FQDN from your AWS Organization should be simple. But...&lt;/p&gt;

&lt;p&gt;AWS has made it ridiculously hard to get a consolidated list of all Virtual Machine Names, IP's and Fully-Qualified Domain Names across your AWS Organization. There is no single command to do it. You must iterate through all accounts individually, making API calls within each account. It can be tricky.&lt;/p&gt;

&lt;p&gt;Using Python, I've made it simple. Keep reading to learn how I did it.&lt;/p&gt;

&lt;p&gt;Background: The AWS Organization must be setup to allow users to assume roles from the Management Account to the individual Member Accounts. Start &lt;a href="https://aws.amazon.com/premiumsupport/knowledge-center/organizations-member-account-access/"&gt;here&lt;/a&gt; with more information on this.&lt;/p&gt;

&lt;p&gt;Program Logic:&lt;br&gt;
1) Input the Management Account ID into the program. We'll use it later&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print("******************************************************************")
print("Welcome to the AWS Organization EC2 Name, IP and FQDN Report")
print("To get started, please enter the AWS Organization Main Account ID: ")
orgMainAccountID = input()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Get the list of all the accounts in the AWS Organization&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orgClient = boto3.client('organizations')
response = orgClient.list_accounts()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) We'll iterate over all the accounts found in the "response" variable. For each of the accounts, we'll use the AWS Security Token Service (STS) to create temporary tokens to access the Member accounts from the Management account. Except, we won't create a temporary token for the Management account since we are accessing it directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for account in response['Accounts']:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4) Using AWS STS, create the token and the session&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stsClient = boto3.client('sts')
roleArn = "arn:aws:iam::" + account['Id'] + ":role/OrganizationAccountAccessRole"
stsresponse = stsClient.assume_role(RoleArn=roleArn, RoleSessionName='newsession')
# Save the details from assumed role into vars
newsession_id = stsresponse["Credentials"]["AccessKeyId"]
newsession_key = stsresponse["Credentials"]["SecretAccessKey"]
newsession_token = stsresponse["Credentials"]["SessionToken"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5) Using the assume_role variables, create an ec2client to get information from our virtual machines and store in the "response" variable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the assumed session vars to create a new boto3 client with the assumed role creds
ec2Client = boto3.client('ec2', 
  region_name=region['RegionName'],
  aws_access_key_id=newsession_id, 
  aws_secret_access_key=newsession_key, 
  aws_session_token=newsession_token)
response = ec2Client.describe_instances()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6) Loop through all responses and get the VM Name, IP, and FDQN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for reservation in response["Reservations"]:
  for instance in reservation["Instances"]:
    try:
      if instance["State"]["Name"] == "running":
        print("Account Name:",account['Name']+",", "Region: 
        {}, Name: {}, Private IP: {}, Public IP: {}, FQDN: 
        {}".format( region['RegionName'],
        # get instance name from Tag Name
        [tag['Value'] for tag in instance['Tags'] if tag['Key'] == 'Name'][0], instance["PrivateIpAddress"],instance["PublicIpAddress"], instance["PublicDnsName"]))
     except KeyError as missing_key:
     # Used as missing_key for readability purposes only
       print(f"Trying to access a &amp;lt;dict&amp;gt; with a missing key {missing_key}") 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7) Back to the Management account ID we input in step 1. The code contains a condition to make sure we &lt;strong&gt;don't&lt;/strong&gt; try to "assume_role" into the Management account, since we are accessing it directly.&lt;/p&gt;

&lt;p&gt;A final note. The complete code can be found &lt;a href="https://github.com/jhowerin/AWS_Python_Scripts/tree/main/VM_IP_FQDN"&gt;here&lt;/a&gt;. Feel free to improve with better error handling, readability or execution speed.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>python</category>
      <category>cloud</category>
      <category>security</category>
    </item>
  </channel>
</rss>
