1. Answers
  2. Configuring an AWS EC2 Launch Configuration with Pulumi

How do I configure an AWS EC2 Launch Configuration with Pulumi?

In this guide, we will configure an AWS EC2 Launch Configuration using Pulumi in TypeScript. Launch Configurations are used to define the settings for launching EC2 instances in an Auto Scaling group. This includes specifying the AMI, instance type, key pair, security groups, and other instance settings.

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

// Define the AMI to use for the instances
const amiId = "ami-0c55b159cbfafe1f0"; // Update with your preferred AMI ID

// Define the instance type
const instanceType = "t2.micro";

// Define the key pair name
const keyName = "my-key-pair"; // Update with your key pair name

// Define the security group
const securityGroup = new aws.ec2.SecurityGroup("web-sg", {
    description: "Allow HTTP and SSH",
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
    ],
    egress: [
        { protocol: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Define the launch configuration
const launchConfiguration = new aws.ec2.LaunchConfiguration("web-lc", {
    imageId: amiId,
    instanceType: instanceType,
    keyName: keyName,
    securityGroups: [securityGroup.name],
    userData: `#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &
`,
    // Additional optional settings
    ebsOptimized: false,
    enableMonitoring: true,
    rootBlockDevice: {
        volumeSize: 8,
        volumeType: "gp2",
    },
    ebsBlockDevices: [
        {
            deviceName: "/dev/xvdb",
            volumeSize: 20,
            volumeType: "gp2",
        },
    ],
    metadataOptions: {
        httpTokens: "optional",
        httpEndpoint: "enabled",
        httpPutResponseHopLimit: 1,
    },
});

Key Points

  • AMI ID: Specifies the Amazon Machine Image to use for the instances.
  • Instance Type: Defines the type of instance to launch.
  • Key Pair: The name of the key pair to use for SSH access.
  • Security Group: Allows HTTP and SSH access to the instances.
  • User Data: A script to run when the instance launches.
  • EBS Optimized: Whether the instance is optimized for EBS I/O.
  • Enable Monitoring: Enables detailed monitoring.
  • Root Block Device: Configuration for the root volume.
  • EBS Block Devices: Additional EBS volumes to attach to the instance.
  • Metadata Options: Configures the instance metadata service.

Summary

In this guide, we configured an AWS EC2 Launch Configuration using Pulumi in TypeScript. We specified the AMI, instance type, key pair, security group, and additional settings such as user data, EBS optimization, monitoring, block devices, and metadata options. This configuration can be used to launch instances in an Auto Scaling group.

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