January 10, 2022 By Dimitri Prosper 4 min read

How to use these two services to dynamically update the authorized SSH keys on a Linux instance running inside a VPC.

Deploying an application to a Virtual Server Instance (VSI) may require some configuration based on information that is specific to the VSI. An example would be configuring the application to interact with a cloud service based on the region or availability zone of the VSI. The most common approach would be to use user data to perform the configurations. This approach, however, requires supplying the instance-specific information in the user data field during the initial provisioning of the VSI. 

In addition, it may be necessary to modify the configuration across multiple VSIs based on changes to the environment. Although user data is configurable to run on a reboot, it continues to have a few drawbacks.  One example would be an application that requires discovering changes to the hostname of the load balancer deployed in front of it. 

IBM Cloud Virtual Private Cloud (VPC) and Identity and Access Management (IAM) provide a set of complementary features that can help in these situations: 

  • VPC Instance Metadata is a service that provides instance-specific information — for example, the availability zone in which the instance is running, the instance ID, the subnet and more. It can also provide an IAM token used to access cloud resources.
  • IAM Trusted Profiles provide the ability to configure fine-grained authorization for computing resources running on IBM Cloud: Kubernetes, Red Hat OpenShift and VPC Virtual Server Instance (VSI). With IAM Trusted Profiles, a compute resource is assigned access to IAM-enabled resources.

The availability of these two features opens up opportunities for the contextual configuration of an application running on the compute instance — both during initial deployment and for dynamic updates. Managing the lifecycle of SSH keys on a VSI is often required when a new administrator joins the team or there is a security event that requires a wholesale update of keys. This blog post introduces an example application that will take advantage of these two services to dynamically update the authorized SSH keys on a Linux instance running inside a VPC.

As of this writing, the VPC Instance Metadata service and the IAM Trusted Profiles for VPC VSI are in Select Availability. Contact IBM support or your IBM Sales representative if you’re interested in getting early access.

Deployment architecture and application code

To demonstrate these two features, we will deploy a VSI inside of a VPC with appropriate security groups. A small application — in this case, a bash script ssh-authorized-keys.sh — is added as a running service on the instance:

As stated above, the application is a simple bash script running inside the virtual server that interacts with the IAM Trusted Profiles and the Metadata APIs to retrieve and parse the information required in order to configure the authorized_keys. Let’s review the main parts of the code:

Deploying and configuring the components

This associated code repository contains the infrastructure as code instructions based on Terraform to create and configure the resources as depicted above. Here is a high-level overview of the code repository structure:

  • The file main.tf contains the instructions for creating the base infrastucture constructs (i.e., a VPC, Subnets, Security Groups). If you have an existing VPC you can modify this file to supply data resources instead.
  • The instance.tf file contains the instructions for creating and configuring the Virtual Server Instance. It will create an SSH key that will be used to configure the instance and call various shell scripts, as needed.
  • The variables.tf file contains all of the input required by the Terraform template. Don’t modify this file, as the number of values that you need to modify is more limited. Use the `terraform.tfvars` to supply your values as described in the repository.
  • The scripts folder contains the various shell scripts that are invoked during the run of the Terraform template.

After deploying the environment, you can test the dynamic configuration of SSH Keys on the VSI by updating the access policies in the IAM Trusted Profiles. As an enhancement, you can also configure the IBM Cloud Logging agent on the instance to capture the logs generated by the ssh-authorized-keys service to an existing IBM Cloud Log Analysis instance if desired. The logs are stored under /var/log.

Getting started

To get started, try the sample code on GitHub. Instructions are provided in the README.md to create and use this template. We also cover how to clean up resources when you no longer need them.

Questions and feedback

The GitHub repository for this scenario has an Issues tab where you can comment on the content and code. If you have suggestions or issues, please submit your feedback.

Was this article helpful?
YesNo

More from Cloud

How a US bank modernized its mainframe applications with IBM Consulting and Microsoft Azure

9 min read - As organizations strive to stay ahead of the curve in today's fast-paced digital landscape, mainframe application modernization has emerged as a critical component of any digital transformation strategy. In this blog, we'll discuss the example of a US bank which embarked on a journey to modernize its mainframe applications. This strategic project has helped it to transform into a more modern, flexible and agile business. In looking at the ways in which it approached the problem, you’ll gain insights into…

The power of the mainframe and cloud-native applications 

4 min read - Mainframe modernization refers to the process of transforming legacy mainframe systems, applications and infrastructure to align with modern technology and business standards. This process unlocks the power of mainframe systems, enabling organizations to use their existing investments in mainframe technology and capitalize on the benefits of modernization. By modernizing mainframe systems, organizations can improve agility, increase efficiency, reduce costs, and enhance customer experience.  Mainframe modernization empowers organizations to harness the latest technologies and tools, such as cloud computing, artificial intelligence,…

Modernize your mainframe applications with Azure

4 min read - Mainframes continue to play a vital role in many businesses' core operations. According to new research from IBM's Institute for Business Value, a significant 7 out of 10 IT executives believe that mainframe-based applications are crucial to their business and technology strategies. However, the rapid pace of digital transformation is forcing companies to modernize across their IT landscape, and as the pace of innovation continuously accelerates, organizations must react and adapt to these changes or risk being left behind. Mainframe…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters