1. Provision public IP addresses for EC2 instances automatically

    TypeScript

    When provisioning EC2 instances on AWS, you might want to assign public IP addresses to allow access to the internet or enable the instances to be reached from external networks. By default, when an instance is launched into a default VPC, it is assigned a public IP address. However, when launching instances into a non-default VPC, you need to ensure that the subnet's attribute MapPublicIpOnLaunch is set to true, so instances launched into this subnet will automatically be assigned a public IP.

    To accomplish this, we can use Pulumi's AWS package to create a new VPC with a subnet and modify the subnet's attribute. We'll then launch an EC2 instance into this subnet, ensuring it receives a public IP address. For this, we'll make use of the aws.ec2.Instance resource, which represents an EC2 instance, allowing us to specify configurations such as the instance type, AMI, subnet, and the associatePublicIpAddress property to ensure a public IP is assigned.

    Here's a complete Pulumi TypeScript program that creates a VPC, a subnet with MapPublicIpOnLaunch enabled, and an EC2 instance with a public IP address:

    import * as aws from "@pulumi/aws"; // Create a new VPC. const vpc = new aws.ec2.Vpc("my-vpc", { cidrBlock: "10.0.0.0/16", }); // Create a new Subnet with `MapPublicIpOnLaunch` enabled. // This will cause instances launched into this subnet to receive a public IP. const subnet = new aws.ec2.Subnet("my-subnet", { vpcId: vpc.id, cidrBlock: "10.0.1.0/24", mapPublicIpOnLaunch: true, // Important for automatic public IP assignment availabilityZone: "us-west-2a", // Specify your desired availability zone }); // Choose an AMI for the EC2 instance. const ami = aws.ec2.getAmi({ filters: [ { name: "name", values: ["amzn2-ami-hvm-*-x86_64-gp2"], // This will select the latest Amazon Linux 2 AMI }, ], owners: ["137112412989"], // This is the AWS account ID for Amazon Linux 2 mostRecent: true, }); // Create a new EC2 instance. const instance = new aws.ec2.Instance("my-instance", { ami: ami.id, instanceType: "t2.micro", // Select your instance type subnetId: subnet.id, associatePublicIpAddress: true, // Explicitly associate a public IP address tags: { Name: "my-instance", }, }); // Export the public IP of the EC2 instance so we can access it. export const publicIp = instance.publicIp;

    In this program, we begin by importing the AWS package for Pulumi, which provides us with classes and functions to interact with AWS resources.

    Next, we create a new VPC with a specified CIDR block that will contain all our infrastructure. The VPC acts as a virtual network within AWS that is isolated from other networks.

    Following the VPC, we create a subnet within the VPC, specifying the VPC ID it belongs to, its CIDR block, and enabling the mapPublicIpOnLaunch. This attribute, when set to true, ensures that any new instance launched into this subnet is automatically assigned a public IP address.

    To create an EC2 instance, we need to specify an AMI (Amazon Machine Image) that our instance will use. We use the getAmi function to find the latest Amazon Linux 2 AMI available.

    Finally, we create the EC2 instance in our subnet using the retrieved AMI ID, a t2.micro instance type (which is eligible for the AWS free tier), and enable the association of the public IP. We also add a tag for clarity.

    To make sure you can access the public IP once the instance is provisioned, we export the publicIp attribute of the instance. This will be printed to the console upon running pulumi up.

    Remember that running this Pulumi program will result in AWS charges if the resources provisioned are not covered by the AWS free tier. To remove the resources and avoid further charges, you can run pulumi destroy followed by pulumi stack rm.

    Before running this Pulumi program, ensure that you have set up the AWS CLI with appropriate credentials and have selected the correct region where you wish to provision resources.