1. Answers
  2. Creating Shielded VMs in GCP with Pulumi

How Do I Create a Shielded VM in GCP?

Introduction

Shielded virtual machines (VMs) are an enhanced security feature in Google Cloud that protect virtual machine instances from various threats such as rootkits and bootkits. This guide aims to demonstrate how to create a Shielded VM instance in Google Cloud Platform using Pulumi with TypeScript. By following this guide, you will learn how to set up a Google Cloud project and configure a virtual machine with Shielded VM features enabled.

Step-by-Step Guide to Creating a Shielded VM

Step 1: Set Up Your Environment

Before you begin, ensure you have a Google Cloud account and have installed Pulumi and the necessary GCP plugins.

Step 2: Create a Google Cloud Project

Start by creating a Google Cloud project. This project will host your Shielded VM and associated resources.

Step 3: Define the Network

Create a virtual private cloud (VPC) network that your VM will use. This network will facilitate communication with other resources.

Step 4: Configure the Shielded VM Instance

Below is the code to create a Shielded VM instance:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const vpcNetwork = new gcp.compute.Network("vpc_network", {name: "my-vpc-network"});
const shieldedVmInstance = new gcp.compute.Instance("shielded_vm_instance", {
    networkInterfaces: [{
        accessConfigs: [{}],
        network: vpcNetwork.name,
    }],
    name: "shielded-vm-instance",
    machineType: "e2-medium",
    zone: "us-central1-a",
    bootDisk: {
        initializeParams: {
            image: "debian-cloud/debian-10",
        },
    },
    shieldedInstanceConfig: {
        enableSecureBoot: true,
        enableVtpm: true,
        enableIntegrityMonitoring: true,
    },
    metadataStartupScript: `#!/bin/bash
echo "Hello, World!" > /var/www/html/index.html
`,
});
export const instanceName = shieldedVmInstance.name;
export const instanceZone = shieldedVmInstance.zone;
export const instanceSelfLink = shieldedVmInstance.selfLink;

Explanation of the Code

  • The vpcNetwork resource creates a VPC network.
  • The shieldedVmInstance resource defines the Shielded VM instance with key security features enabled: secure boot, vTPM, and integrity monitoring.
  • The metadataStartupScript runs a simple script upon VM startup.
  • The export statements provide outputs for the VM instance name, zone, and self-link.

Key Points

  • Security Features: Ensure that secure boot, vTPM, and integrity monitoring are enabled for enhanced security.
  • Network Configuration: Properly configure your VPC to allow necessary communications.
  • Startup Script: Customize the startup script to automate tasks during VM initialization.

Conclusion

In this guide, we successfully set up a Google Cloud project, configured a network, and deployed a Shielded VM instance with enhanced security features using Pulumi. By following these steps, you can ensure your virtual machines are protected against various security threats, providing a robust environment for your applications.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up