How to find instance Id from the instance metadata

For instances which use version 2 of instance metadata, IMDSv2, run the following 2 commands: [ec2-user@ip-172-31-45-35 ~]$ TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` [ec2-user@ip-172-31-45-35 ~]$ curl -w "\n" -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id i-04bd6066612345678 If you use IMDsv1, then there is no need generate the TOKEN value first, just query the instance-id directly: [ec2-user@ip-172-31-45-35 ~]$ curl -w "\n" http://169.254.169.254/latest/meta-data/instance-id In my case, it returned nothing as my EC2 instances uses IMDsV2....

July 7, 2024 · 1 min · 108 words · kenno

How to initiate Session Manager session using CLI

Tonight I want to connect to an EC2 instance that resides in a private subnet. I currently don’t have a bastion host I can use as an intermediary to jump to this instance. Well, as you might have guessed from this post’s title, the AWS Session Manager is something that might be helpful in this case. So, join me in this journey to configure and set up the Session Manager, using AWS CLI only....

July 5, 2024 · 4 min · 767 words · kenno

Delete ElastiCache Redis Cluster Using AWS CLI

Once upon a time there exist an ElastiCache Redis cluster that I no longer needed. I know that the cluster could be deleted via ElastiCache web console. However, as a command-line addict as myself, I wanted to use AWS CLI to delete the cluster instead. Well, actually this is my first time trying to delete an ElastiCache cluster using the CLI. I’m going to try to document how I do this, and what mistakes I’d make a long the way....

July 3, 2024 · 2 min · 383 words · kenno

Get connection status for a managed node with AWS CLI

I kept forgetting about the AWS CLI which is quite handy to quickly check if SSM is working on an EC2 instance. So here it is again for my future-self. Use get-connection-status to return the connection status of the specified managed instance. $ aws ssm get-connection-status --target i-092af3b3c1234567 { "Status": "connected", "Target": "i-092af3b3c1234567" } Reference: get-connection-status

June 26, 2024 · 1 min · 56 words · kenno

Delete unused ENIs on AWS with AWS CLI

So I want to clean up Elastic Network Interfaces (ENIs) that were created for testing and no longer needed, and I want to use AWS CLI to do that. For me, I know that all unused ENIs will have their status as “available”. So I will find all those ENIs and will delete them. ➜ aws ec2 describe-network-interfaces --filters Name=status,Values=available \ --query 'NetworkInterfaces[].{NetworkInterfaceId:NetworkInterfaceId,Description:Description}' [ { "NetworkInterfaceId": "eni-0dc616df583312345", "Description": "2nd nic in 2b az" }, { "NetworkInterfaceId": "eni-0bf98b53cf1b12345", "Description": "2nd nic" }, { "NetworkInterfaceId": "eni-034bcff8ceef12345", "Description": "test-eni" }, { "NetworkInterfaceId": "eni-09adbc3a20c912345", "Description": "Test 2nd eni on custom subnet 2a AZ" } ] Next, I need to feed each of the values of “NetworkInterfaceId” to the delete command aws ec2 delete-network-interface, which takes only 1 NetworkInterfaceId at a time....

June 9, 2024 · 2 min · 271 words · kenno

How to generate Let's Encrypt SSL cert for Bitnami Lightsail

So you just setup a new shiny WordPress website on a Lightsail (WordPress) instance. Now, you’ve been told to setup an SSL certificate for your website domain. What to do? The following steps show how to generate a free SSL certificate provided by Let’s Encrypt. bitnami@ip-172-26-12-25:~$ sudo /opt/bitnami/bncert-tool ---------------------------------------------------------------------------- Welcome to the Bitnami HTTPS Configuration tool. ---------------------------------------------------------------------------- Domains Please provide a valid space-separated list of domains for which you wish to configure your web server....

May 22, 2024 · 2 min · 410 words · kenno

How to attach instance profile on EC2 with AWS CLI

This is a quick note to remind me on how to attach or associate an instance profile (IAM role) to an EC2 instance using AWS CLI. There is a bonus section at the end to show how to replace one IAM role with another one directly. In this example, I already have a running EC2 instance i-0357ecc1111111111 running. I want to attach a new instance profile called SSMInstanceProfile to this instance....

March 5, 2024 · 3 min · 436 words · kenno

Upgrading RHEL 8 to RHEL 9 on AWS

So I have an EC2 instance running RHEL 8.9 which was recently upgraded from RHEL 7.9. Now, I want to upgrade it all the way to RHEL 9. Here is the worklog which recording the steps I performed to achieve that goal. Step 0, perform a snapshot of the root volume of the instance, or create an AMI from it. In my case, this is just a test instance, so I skip taking the snapshot....

November 22, 2023 · 9 min · 1876 words · kenno

No support for RHEL with high availability in-place upgrade

Well, if you know you know, otherwise, you could have wasted the whole evening trying to upgrade RHEL 7.9 with High Availability to RHEL 8.x, and failed! If you read this post, head to the link at [1] and you’d see that Red Hat does not support i-place upgrades or rolling-upgrades of cluster nodes from one major release of RHEL to another. Here is an extract from [1]: Red Hat does not support in-place upgrades or rolling-upgrades of cluster nodes from one major release of RHEL to another....

November 15, 2023 · 2 min · 377 words · kenno

How to delete Lightsail resources using AWS CLI

In this post, I’ll document how to use AWS CLI to delete a few Lightsail resources. I have never done this before, so this would be a good exercise for me. To make it less boring, I’m writing this post while I’m deleting these resources. First step, I need to look for the instance name [1]. ❯ aws lightsail get-instances --query 'instances[].name' [ "WordPress-1" ] The above output is the name of the instance I’d like to delete....

October 5, 2023 · 3 min · 464 words · kenno